[轉(zhuǎn)載]Gradle的Properties

轉(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,820評論 25 709
  • 轉(zhuǎn)載注明出處:http://www.itdecent.cn/p/5255b100930e 0. 前言 完全由個(gè)人翻...
    王三的貓阿德閱讀 2,730評論 0 4
  • Gradle對于很多開發(fā)者來說有一種既熟悉又陌生的感覺,他是離我們那么近,以至于我每天做項(xiàng)目都需要他,但是他又是離...
    阿_希爸閱讀 9,692評論 10 199
  • 本文已授權(quán)微信公眾號 Android技術(shù)經(jīng)驗(yàn)分享 獨(dú)家發(fā)布轉(zhuǎn)載請注明出處:Gradle從入門到了解 通過這篇文章,...
    hongjay閱讀 5,503評論 7 82
  • 國家編碼語言編碼貨幣符號貨幣編碼 =============currentLocale==============...
    我是小胡胡123閱讀 1,978評論 0 0

友情鏈接更多精彩內(nèi)容