轉(zhuǎn)自:http://blog.csdn.net/eastlhu/article/details/50206287 (注:該帖子也是轉(zhuǎn)帖)
問題背景
團(tuán)隊(duì)一起在開發(fā)一個(gè)Android項(xiàng)目,工程師有的使用Eclipse,有個(gè)使用Intellij IDEA,有的使用Android Studio。每個(gè)人安裝的Android SDK build-tools可能都不一樣,有的是19.0.3,有的是19.1.0,不同版本的build-tools對Gradle Plugin也有相應(yīng)的要求,如19.0.3對應(yīng)的是com.android.tools.build:gradle:0.10.+,19.1.0對應(yīng)的是com.android.tools.build:gradle:0.12.+,下面是一個(gè)典型的build.gradle配置文件。
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 19
buildToolsVersion 19.0.3
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
}
}
在合作開發(fā)中遇到的一個(gè)尷尬的問題是,IDEA最新版還不能很好的支持Gradle Plugin 0.12+,而Android Studio最新版則要求使用0.12+。大家又共用一個(gè)Git倉庫。可能的解決方案是,從Git checkout出來的項(xiàng)目需要有一個(gè)基礎(chǔ)的版本號,但是開發(fā)者可以在本地通過一處文件(不check in到git)來重載版本號。
解決方案
Gradle支持三種Properties, 這三種Properties的作用域和初始化階段都不一樣,下面分別列出了其部分特點(diǎn):
1.System Properties:
1.可通過gradle.properties文件,環(huán)境變量或命令行-D參數(shù)設(shè)置
2.可在setting.gradle或build.gradle中動(dòng)態(tài)修改,在setting.gradle中的修改對buildscript block可見;
3.所有工程可見,不建議在build.gradle中修改
4.多子工程項(xiàng)目中,子工程的gradle.properties會被忽略掉,只有root工程的gradle.properties有效;
2.Project Properties:
1.可通過gradle.properties文件,環(huán)境變量或命令行-P參數(shù)設(shè)置,優(yōu)先級是:
2.可在build.gradle中動(dòng)態(tài)修改,但引用不存在的project properties會立即拋錯(cuò)
3.動(dòng)態(tài)修改過的project properties在buildscript block中不可見
3.Project ext properties:
1.可在項(xiàng)目的build.gradle中聲明和使用,本工程和子工程可見
2.不能在setting.gradle中訪問
如果有多處設(shè)置,加載次序如下(注意:gradle 2.0是這樣的, 1.10~1.12有bug), 后面的覆蓋前面的設(shè)置
1.from gradle.properties located in project build dir.
2.from gradle.properties located in gradle user home.
3.from system properties, e.g. when -Dsome.property is used in the command line.
4.setting.gradle
5.build.gradle
根據(jù)其特點(diǎn),這里給出一個(gè)使用System Properties來解決問題的方案。
修改build.gradle使用變量設(shè)置版本號
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:'+System.properties['androidGradlePluginVersion']
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 19
buildToolsVersion System.properties['buildToolsVersion']
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
}
}
在setting.gradle中設(shè)置缺省的版本
//override your build tools version in project gradle.properties or ~/.gradle/gradle.properties
if (!System.properties['buildToolsVersion']) {
System.properties['buildToolsVersion'] = "19.0.3"
}
if (!System.properties['androidGradlePluginVersion']) {
System.properties['androidGradlePluginVersion'] = "0.10.+"
}
在gradle.properties文件中重載版本號
gradle.properties文件內(nèi)容如下:
systemProp.buildToolsVersion=19.1.0
systemProp.androidGradlePluginVersion=0.12.+
gradle.properties文件可以放在root project根目錄下,也可以放在用戶目錄下 ~/.gradle/gradle.properties,后者的優(yōu)先級更高。
命令行使用
還可以在命令行中設(shè)置參數(shù)
./gradlew build -DbuildToolsVersion=19.1.0 -DandroidGradlePluginVersion=0.12.+ -x lint
參考鏈接
http://www.gradle.org/docs/current/dsl/
http://www.gradle.org/docs/current/userguide/tutorial_this_and_that.html