最近将 Android Studio 中的 Gradle 插件从 2.3.3 升级到 3.5.1,项目中出现很多报错,统计一下。
升级
修改 dependicies:
1 | buildscript { |
报错
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 | task showFlavors << { |
使用 doLast 来替代。
1 | task showFlavors { |
instrumentTest 无法识别
ERROR: The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin. Perhaps you misspelled something?
instrumentTest 被废弃。
1 | android { |
修改为 androidTest。
1 | android { |
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 | android { |
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 | <manifest ...> |
在 build.gradle 中添加:
1 | android { |
警告
variant.getAssemble()
1 | INFO: API 'variant.getAssemble()' is obsolete and has been replaced with 'variant.getAssembleProvider()'. |
variant.getAssemble() 即将被废弃,在 2019 年底会被移除。需要使用 variant.getAssembleProvider() 来替代。
参考链接:https://d.android.com/r/tools/task-configuration-avoidance
1 | android { |
修改为
1 | android { |
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 命令已过时,已经被 implementation 和 api 所代替。
api 与 compile 等价。 implementation 的区别在于将项目的依赖项隐藏在了内部,依赖该项目的无法访问。
例如:
lib_A 的 build.gradle:
1 | dependencies { |
lib_B 的 build.gradle:
1 | dependencies { |
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 | dependencies { |
修改为
1 | dependencies { |