Android Studio 项目 Gradle 升级经验总结

想必各位小伙伴早就都有收到各大应用市场下发的关于《软件高 API 公告》吧!
应用宝公告

升级我们项目的 API 版本等级已经是必须提上日程了,同时 AS 版本等级、Gradle 版本也是我们升级绕不开的一环,总不能 API 等级提高了,项目还基于低版本的 Gradle 吧。下文我们将简单讲讲,在升级 Gradle 版本时需要注意的一些问题,以及出现相关问题时的解决方法,愿各位小伙伴都愉快的升级。

环境为: MacOS + AS 3.1.3 + Gradle 4.4 + Gradle Plugin 3.1.3

1、修改 Gradle 版本

首先我们修改项目根目录下的 build.gradle 文件中 Gradle Plugin 的版本为 3.1.3:

1
2
3
4
5
6
7
8
9
10
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
...
}
}

修改根目录 gradle\wrapper 文件夹下 gradle-wrapper.properties 文件中

1
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zipg

一般会出现如下提示:
无法找到 gradle 插件

1
2
3
4
5
6
7
8
9
Could not find com.android.tools.build:gradle:3.1.3.
Searched in the following locations:
https://jcenter.bintray.com/com/android/tools/build/gradle/3.1.3/gradle-3.1.3.pom
https://jcenter.bintray.com/com/android/tools/build/gradle/3.1.3/gradle-3.1.3.jar
Required by:
project :
Add Google Maven repository and sync project
Open File
Enable embedded Maven repository and sync project

按照提示操作,添加 Google 仓库依赖即可。

2、修改因 Gradle 版本升级导致的异常

1. 修改输出apk文件名:

1
2
Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=uerblybUerb, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
Open File

该问题是因为我们在模块级的 build.gradle 文件中设置了对打包输出文件的文件名进行修改,我们只要将原来的代码改写成如下形式即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
android {
// 自定义文件名输出配置
applicationVariants.all { variant ->
variant.outputs.all { output ->
//要被替换的源字符串
def sourceFile = "app-${variant.flavorName}-${variant.buildType.name}"
//替换的字符串
def replaceFile = "XXX_V${variant.versionName}_c${variant.versionCode}_${variant.flavorName}_${variant.buildType.name}_${releaseTime()}"
outputFileName = output.outputFile.name.replace(sourceFile, replaceFile)
}
}
}

2. 因 apt 插件带来的问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Cannot choose between the following configurations of project :mainnavigatetabbar:
- debugApiElements
- debugRuntimeElements
- releaseApiElements
- releaseRuntimeElements
All of them match the consumer attributes:
- Configuration 'debugApiElements':
- Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
- Found org.gradle.usage 'java-api' but wasn't required.
- Configuration 'debugRuntimeElements':
- Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
- Found org.gradle.usage 'java-runtime' but wasn't required.
- Configuration 'releaseApiElements':
- Found com.android.build.api.attributes.BuildTypeAttr 'release' but wasn't required.
- Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
- Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
- Found org.gradle.usage 'java-api' but wasn't required.
- Configuration 'releaseRuntimeElements':
- Found com.android.build.api.attributes.BuildTypeAttr 'release' but wasn't required.
- Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
- Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
- Found org.gradle.usage 'java-runtime' but wasn't required.

解决办法:
project 的 build.gradle 文件中删除
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

module 的 build.gradle 文件中删除
apply plugin: 'com.neenbedankt.android-apt'

module 的build.gradle 文件中替换

1
2
implementation'com.jakewharton:butterknife:9.0.0-rc1'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1'

3. Android SDK Build Tools版本过低:

1
2
3
4
5
The specified Android SDK Build Tools version (23.0.3) is ignored, as it is below the minimum supported version (27.0.3) for Android Gradle Plugin 3.1.3.
Android SDK Build Tools 27.0.3 will be used.
To suppress this warning, remove "buildToolsVersion '23.0.3'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.
Update Build Tools version and sync project
Open File

解决: 修改模块下 build.gradle 文件中的 compileSdkVersion 28 、修改buildToolsVersion '28.0.3' 、修改 targetSdkVersion 28
如果项目中,以模块形式依赖了其他的项目,也需要一并修改。

4. 项目中存在 Flavor:

1
All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

解决方法:

1
2
3
4
5
6
7
android {
...
defaultConfig {
...
flavorDimensions "versionCode"
}
}

4. 修改各个模块的 BuildType
注意:如果你的项目包含了数个 Flavor,并且自定义了数个 buildType,同时又以模块的形式依赖了别的项目,那么你需要将每个模块的 buildTypes 修改成同 app 模块一致。
否则会出现如下错误:

1
2
3
Unable to resolve dependency for ':app@xxxxXXXX(flavor + buildtype)/compileClasspath': Could not resolve project :xxxxxx(依赖的模块).
Open File
Show Details

5. AAPT2 检查错误:

1
2
3
4
5
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeUerblUerbResources'.
> Error: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for details

解决方法:在项目根目录下的 gradle.properties 文件中增加:
android.enableAapt2=false 禁用 AAPT2。

其他错误:
其他的错误多出现在重复依赖,这里就不多赘述了,还有升级 API 等级也不多赘述了,这些都有大量文章可供参考。