0%

Android Studio 工程升级Gradle笔记

最近将 Android Studio 中的 Gradle 插件从 2.3.3 升级到 3.5.1,项目中出现很多报错,统计一下。

升级

修改 dependicies

1
2
3
4
5
6
buildscript {
dependencies {
// classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.android.tools.build:gradle:3.5.1'
}
}

报错

Gradle 版本

Minimum supported Gradle version is 5.4.1. Current version is 3.3.

Gradle 版本需要升级为 5.4.1,点击 Fix Gradle wrapper and re-import project 修复,可以在 Project Structure 中查看。

leftShift 方法找不到

Could not find method leftShift() for arguments

leftShift() / << 方法被废弃。

参考:https://www.cuba-platform.com/discuss/t/leftshift-method-has-been-deprecated-in-gradle/6384

1
2
3
4
5
task showFlavors << {
productFlavors.all{ flavor ->
print(flavor.name + " ")
}
}

使用 doLast 来替代。

1
2
3
4
5
6
7
task showFlavors {
doLast {
productFlavors.all { flavor ->
print(flavor.name + " ")
}
}
}

instrumentTest 无法识别

ERROR: The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin. Perhaps you misspelled something?

instrumentTest 被废弃。

参考链接:https://stackoverflow.com/questions/49511200/the-sourceset-instrumenttest-is-not-recognized-by-the-android-gradle-plugin

1
2
3
4
5
6
android {
sourceSets {
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
}
}

修改为 androidTest

1
2
3
4
5
6
android {
sourceSets {
// Move the tests to tests/java, tests/res, etc...
androidTest.setRoot('tests')
}
}

flavorDimensions 设置

ERROR: All flavors must now belong to a named flavor dimension.

所有的 flavor 必须设置 dimension 属性。

参考链接:https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

添加 flavorDimensions

1
2
3
4
5
6
7
8
9
10
11
android {
// Specifies one flavor dimension.
flavorDimensions "version"
productFlavors{
base {
// Assigns this product flavor to the "version" flavor dimension.
// This property is optional if you are using only one dimension.
dimension "version"
}
}
}

minSdk 版本设置

ERROR: The minSdk version should not be declared in the android manifest file. You can move the version from the manifest to the defaultConfig in the build.gradle file.

minSdk 属性无需在 manifest 文件中设置了,应当在 build.gradle 文件的 defaultConfig 中设置。

删除 AndroidManifest.xml 中的属性:

1
2
3
4
5
<manifest ...>
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="26" />
</manifest>

build.gradle 中添加:

1
2
3
4
5
6
android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
}
}

警告

variant.getAssemble()

1
2
3
4
INFO: API 'variant.getAssemble()' is obsolete and has been replaced with 'variant.getAssembleProvider()'.
It will be removed at the end of 2019.
For more information, see https://d.android.com/r/tools/task-configuration-avoidance.
To determine what is calling variant.getAssemble(), use -Pandroid.debug.obsoleteApi=true on the command line to display more information.

variant.getAssemble() 即将被废弃,在 2019 年底会被移除。需要使用 variant.getAssembleProvider() 来替代。

参考链接:https://d.android.com/r/tools/task-configuration-avoidance

参考链接:https://stackoverflow.com/questions/54193510/while-android-studio-updated-to-v3-3-getting-api-variant-getassemble-is-obso

1
2
3
4
5
6
7
android {
variant.outputs.all { output ->
variant.assemble.doLast {
// ...
}
}
}

修改为

1
2
3
4
5
6
7
8
9
android {
variant.outputs.all { output ->
variant.getAssembleProvider().configure() {
it.doLast {
// ...
}
}
}
}

compile 命令过时

INFO: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'. It will be removed soon. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html

compile 命令已过时,已经被 implementationapi 所代替。

apicompile 等价。 implementation 的区别在于将项目的依赖项隐藏在了内部,依赖该项目的无法访问。

例如:

lib_Abuild.gradle

1
2
3
dependencies {
implementation project(':lib_B')
}

lib_Bbuild.gradle

1
2
3
dependencies {
implementation project(':lib_C')
}

lib_B 隐藏了 lib_C 中的实现,这样 lib_A 无法访问 lib_C

参考链接:https://stackoverflow.com/questions/44413952/gradle-implementation-vs-api-configuration

参考链接:https://blog.csdn.net/Next_Second/article/details/78428086

1
2
3
4
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':lib1')
}

修改为

1
2
3
4
dependencies {
implementation fileTree(dir: 'libs', include: '*.jar')
implementation project(':lib1')
}