Board logo

标题: 多人协作时gradle配置优化(5) [打印本页]

作者: look_w    时间: 2019-3-7 19:19     标题: 多人协作时gradle配置优化(5)

将这些配置放到一个单独的配置文件中,如me.properties中,该文件不放到版本管理中,然后在项目根目录的build.gradle中加载该属性文件从里面取出配置。该做法和第一种做法的区别是修改me.properites之后android studio工具不会强制要求你做一次同步操作(sync project with gradle files)。具体参考做法如下:
    项目根目录的build.gradle文件

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
     
    buildscript {
        
        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath "com.android.tools.build:gradleandroidPluginVer"
            
     
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
     
    allprojects {
        repositories {
            google()
            jcenter()
        }
    }
     
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
     
    ext {
        loadPropertyFile = { name ->
            Properties properties = new Properties()
            InputStream is = null
            try {
                is = new FileInputStream(name)
                properties.load(is)
            } catch (Throwable tr) {
                tr.printStackTrace()
                println tr.getMessage()
            } finally {
                if (is != null) {
                    try {
                        is.close()
                    } catch (Throwable tr) {
                        tr.printStackTrace()
                    }
                }
            }
     
            return properties
        }
     
        Properties myProperties = loadPropertyFile('me.properties')
        androidPluginVer = '3.1.2'//版本库中的默认配置,一般只有管理员才能修改
        if (myProperties.hasProperty('androidPluginVer')) {//如果配置文件中定义了该属性,则使用,否则使用默认配置
            androidPluginVer = myProperties.getProperty('androidPluginVer')
        }
    }

me.properties文件,放在项目根目录下。

    #android插件版本号,一般与android studio版本号一致
    androidPluginVer=3.1.2




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0