Gradle升級3.1問題總結
背景:項目早期用的Gradle插件版本2.3.0,由于版本較老,無法使用Gradle新版本的特性,故需要升級至最新的Gradle版本。
有關Gradle3.0新版本的提升可參考官方介紹如下:
Android plugin for Gradle 3.0.0 includes a variety of changes that aim to address performance issues of large projects.
For example, on a sample skeleton project with ~130 modules and a large number of external dependencies (but no code or resources), you can experience performance improvements similar to the following:
| Android plugin version + Gradle version | Android plugin 2.2.0 + Gradle 2.14.1 | Android plugin 2.3.0 + Gradle 3.3 | Android plugin 3.0.0 + Gradle 4.1 |
|---|---|---|---|
| Configuration (e.g. running ./gradlew --help) | ~2 mins | ~9 s | ~2.5 s |
| 1-line Java change (implementation change) | ~2 mins 15 s | ~29 s | ~6.4 s |
以上可以看出,新版本的Gradle在編譯速度上提升很多,作為一名研發(fā),亦是一名用戶,是時候做一下升級,改善下用戶體驗了。
Gradle版本及Gradle插件版本的對應關系
| Plugin version | Required Gradle version |
|---|---|
| 1.0.0 - 1.1.3 | 2.2.1 - 2.3 |
| 1.2.0 - 1.3.1 | 2.2.1 - 2.9 |
| 1.5.0 | 2.2.1 - 2.13 |
| 2.0.0 - 2.1.2 | 2.10 - 2.13 |
| 2.1.3 - 2.2.3 | 2.14.1+ |
| 2.3.0+ | 3.3+ |
| 3.0.0+ | 4.1+ |
| 3.1.0+ | 4.4+ |
最新版本的Gradle插件版本是3.1.0,本著少踩坑的原則,采用次新版本3.0.0,所對應的Gradle版本是4.1。
跟著套路來吧,先把版本修改成目標版本,重新Sync Project,天吶,一堆錯誤,直接編譯失敗,問題整理如下:
問題1:
Gradle sync failed: Could not set unknown property 'enforceUniquePackageName' for object of type com.android.build.gradle.AppExtension. (5s 857ms)
解決方案:該插件強制所有庫依賴項都有一個唯一的包名稱,一般是設置false來禁用該選項。
enforceUniquePackageName = false 已經不支持,注釋掉。
問題2:
Gradle sync failed: Could not get unknown property '輸出apk名稱為DwdRider_2015' for object of type com.android.build.gradle.internal.api.ApplicationVariantImpl.
Consult IDE log for more details (Help | Show Log) (1s 899ms)
解決方案:
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "rider_${android.defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
改為如下***********************************>
applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "rider_${android.defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
}
}
參考:Android Gradle 3.0.0-alpha2 plugin, Cannot set the value of read-only property 'outputFile'
As Android plugin 3.0 migration guide suggests:
- Use all() instead of each()
- Use outputFileName instead of output.outputFile if you change only file name (that is your case)
Example from the guide:
// If you use each() to iterate through the variant objects,
// you need to start using all(). That's because each() iterates
// through only the objects that already exist during configuration time—
// but those object don't exist at configuration time with the new model.
// However, all() adapts to the new model by picking up object as they are
// added during execution.
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "${variant.name}-${variant.versionName}.apk"
}
}
問題3:
The specified Android SDK Build Tools version (25.0.2) is ignored, as it is below the minimum supported version (26.0.2) for Android Gradle Plugin 3.0.0.
Android SDK Build Tools 26.0.2 will be used.
To suppress this warning, remove "buildToolsVersion '25.0.2'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.
解決方案:
提示已經很明確了,當前的構建工具版本號25.0.2低于最低支持版本26.0.2,根據提示修改即可。
buildToolsVersion = "25.0.2"
改為
buildToolsVersion = "26.0.2"
問題4:
All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html
解決方案:
插件 3.0.0 包含一項新的依賴項機制,這種機制可以在消費庫時自動匹配變體。 也就是說,應用的 debug 變體將自動消費庫的 debug 變體,等等。 這種機制也適用于使用風味的情況 - 應用的 redDebug 變體將消費庫的 redDebug 變體。 要使這種機制發(fā)揮作用,插件現在要求屬于給定風味維度的所有風味 - 即使您僅準備使用一個維度。
修改代碼如下(app/build.gradle)
flavorDimensions 'tier'
productFlavors {
"yingyongbao" {
dimension 'tier'
}
}
Example from the guide:
// Specifies a flavor dimension.
flavorDimensions "color"
productFlavors {
red {
// Assigns this product flavor to the 'color' flavor dimension.
// This step is optional if you are using only one dimension.
dimension "color"
...
}
blue {
dimension "color"
...
}
}
問題5
Error: Flag android.useDeprecatedNdk is no longer supported and will be removed in the next version of Android Studio. Please switch to a supported build system.
Consider using CMake or ndk-build integration. For more information, go to:
https://d.android.com/r/studio-ui/add-native-code.html#ndkCompile
To get started, you can use the sample ndk-build script the Android
plugin generated for you at:
/Users/suwantao/AndroidStudioProjects/trunk/flash/app/build/intermediates/ndk/yingyongbao/debug/Android.mk
Alternatively, you can use the experimental plugin:
https://developer.android.com/r/tools/experimental-plugin.html
To continue using the deprecated NDK compile for another 60 days, set
android.deprecatedNdkCompileLease=1532586669656 in gradle.properties
從提示可以看出,android.useDeprecatedNdk在新的版本將會被移除,請切換到新的構建系統,建議使用CMake o或者ndk-build ...如果想繼續(xù)60天過時的NDK構建系統,可以在gradle.properties文件設置android.deprecatedNdkCompileLease=1532586669656
為了跟上時代的步伐,還是切到最新的構建系統吧。參考官方文檔,可按照如下步驟執(zhí)行。
- 1.刪除android.useDeprecatedNdk = true
// Remove this line
android.useDeprecatedNdk = true
-
2.下載 NDK 和構建工具(若已經下載,跳過即可)
image.png - 3.手動配置 Gradle
要手動配置 Gradle 以關聯到您的原生庫,您需要將 externalNativeBuild {} 塊添加到模塊級 build.gradle 文件中,并使用 cmake {} 或 ndkBuild {} 對其進行配置:
android {
...
defaultConfig {
ndk {
abiFilters "armeabi" ...
}
...
}
buildTypes {...}
//ndk-build模式
externalNativeBuild {
ndkBuild {
// Provides a relative path to your ndkBuild script.
path file("src/main/jni/Android.mk")
}
}
}
- 4.重新sync project即可
問題6:
More than one file was found with OS independent path 'lib/armeabi/libencrytkey.so'
原項目在jniLibs目錄下已經存在libencrytkey.so文件,新的構建系統又重新生成libencrytkey.so,該文件目錄在如下路徑,解決該問題,把原libencrytkey.so文件刪除即可。

問題7:
ABIs [armeabi] are not supported for platform. Supported ABIs are [armeabi-v7a, arm64-v8a, x86, x86_64].
解決方案:新版本的as默認下載的ndk版本是17,該版本已經不支持armeabi了,無奈只能降級處理,筆者降到版本 android-ndk-r16b
參考:
Android Plugin for Gradle Release Notes
