![gradle](https://dn-zain.qbox.me/gradle.jpg)
依賴最新版本
GRADLE可以做到不依賴具體某個版本的庫肤粱,而是每次從repo拉取最新的庫到本地做編譯。
dependencies {
compile 'com.google.code.gson:gson:2.2.1'
}
如果不想依賴具體的庫抬驴,想每次拉取最新的庫炼七,那么,可以寫成這樣:
dependencies {
compile 'com.google.code.gson:gson:2.2.+'
}
還可以這樣寫:
dependencies {
compile 'com.google.code.gson:gson:+'
}
文件編碼配置
如果導入一個windows下編寫的項目布持,而代碼中有中文注釋豌拙,采用GBK, GB18030等編碼方式時,編譯會報錯题暖,可以采用如下方式統(tǒng)一項目的編碼
allprojects {
repositories {
jcenter()
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
}
配置簽名信息
簽名信息屬于敏感信息按傅,建議不要寫死放到gradle腳本中,而是寫到一個單獨的配置文件里胧卤,而且這個配置文件不要同步到版本管理系統(tǒng)上唯绍,而是由本地維護,防止在版本管理平臺上泄漏敏感信息枝誊。建議簽名信息內容寫到gradle.properties或者local.properties文件里况芒,這樣,gradle腳本可以直接引用叶撒,如果是放在一個自定義的文件中绝骚,gradle腳本需要提供相應的代碼來讀取文件的內容。 文件內容參考如下:
RELEASE_KEY_PASSWORD=android
RELEASE_KEY_ALIAS=androidreleasekey
RELEASE_STORE_PASSWORD=android
RELEASE_STORE_FILE=../resources/release.keystore
DEBUG_KEY_PASSWORD=android
DEBUG_KEY_ALIAS=androiddebugkey
DEBUG_STORE_PASSWORD=android
DEBUG_STORE_FILE=../resources/debug.keystore
gradle腳本引用代碼參考:
android {
signingConfigs {
debug {
storeFile file(DEBUG_STORE_FILE)
storePassword DEBUG_STORE_PASSWORD
keyAlias DEBUG_KEY_ALIAS
keyPassword DEBUG_KEY_PASSWORD
}
release {
storeFile file(RELEASE_STORE_FILE)
storePassword RELEASE_STORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
}
}
}
如果簽名信息沒有放到gradle.properties或者local.properties文件里祠够,那就需要自己寫代碼讀取咯压汪,假設簽名信息放在signing.properties文件中, 文件內容可以參考上面,讀取文件的代碼放入gradle腳本中就可以了哪审,參考代碼如下
def File propFile = new File('signing.properties')
if (propFile.canRead()) {
def Properties props = new Properties()
props.load(new FileInputStream(propFile))
if (props != null && props.containsKey('RELEASE_STORE_FILE') && props.containsKey('RELEASE_STORE_PASSWORD') &&
props.containsKey('RELEASE_KEY_ALIAS') && props.containsKey('RELEASE_KEY_PASSWORD')) {
android.signingConfigs.release.storeFile = file(props['RELEASE_STORE_FILE'])
android.signingConfigs.release.storePassword = props['RELEASE_STORE_PASSWORD']
android.signingConfigs.release.keyAlias = props['RELEASE_KEY_ALIAS']
android.signingConfigs.release.keyPassword = props['RELEASE_KEY_PASSWORD']
println 'all good to go'
} else {
android.buildTypes.release.signingConfig = null
println 'signing.properties found but some entries are missing'
}
} else {
println 'signing.properties not found'
android.buildTypes.release.signingConfig = null
}
構建參數設置
AndroidManifest占位符蛾魄,BuildConfig以及資源配置
根據版本類型和構建變種定義不同的變量值供程序引用
manifestPlaceholders = [APP_KEY:"release"]
buildConfigField "String", "EMAIL", "\"release@android.studio.com\""
resValue "string", "content_main", "Hello world from release!"
buildConfigField支持Java中基本數據類型,如果是字符串湿滓,記得加轉義后的雙引號 resValue支持res/values下的資源定義滴须,字符串無需加轉義后的雙引號
刪除unaligned apk
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 輸出apk名稱為yujia_v1.0_wandoujia.apk
def fileName = "yujia_v${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
if (output.zipAlign != null) {
output.zipAlign.doLast {
output.zipAlign.inputFile.delete()
}
}
}
}
lint選項開關
lint會按默認選項會做嚴格檢查,遇到包錯誤會終止構建過程叽奥,可以用如下開關關掉這個選項扔水,不過最好是重視下lint的輸出,有問題及時修復朝氓,避免潛在問題
android {
lintOptions {
abortOnError false
}
}
依賴庫按構建目標定制
不同的依賴庫可以按構建目標做定制魔市,比如freemium的變種集成了廣告,就可以這樣寫
dependencies {
freemiumCompile 'com.google.android.gms:ads:7.8.0'
}