本次先后对两个项目中的 Gradle Android 插件进行了升级,在升级和编译打包测试过程中主要遇到 10 个问题,现按顺序记录问题及解决方案如下,供参考。
0. 升级配置
修改根目录的 build.gradle
buildscript {
repositories {
...
google() //增加该项
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1' //升级到 3.0.1
..
}
}
修改 gradle-wrapper.properties 配置的 gradle 版本
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
1. 多渠道打包插件先升级为 3.0.1 进行编译出包
详见 packer-ng-plugin 项目
2. 修改 each() 和 output.outputFile() 方法为 all() 和 outputFileName
出现问题:
Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=huaweiDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
修改前:
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputs.each {}
def file = output.outputFile
def filePath = new File(file.parent, output.name)
output.outputFile = new File(filePath, "app.apk")
}
}
修改后:
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "app.apk"
}
}
3. 增加 flavorDimensions,定义 flavor 的维度
出现问题:
Error:All flavors must now belong to a named flavor dimension.Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html
build.gradle 里增加配置,建议放在 productFlavors 上面:
android {
//...
flavorDimensions "versionCode"
}
4. 修复因引用不存在的 jar 包导致的编译失败
出现问题:
What went wrong:
Could not resolve all files for configuration ':PushSDK:releaseCompileClasspath'.
Failed to transform file 'utdid4all-1.1.5_proguard.jar' to match attributes {artifactType=android-classes} using transform JarTransform
Transform output file /Users/dylan/AndroidStudioProjects/Jiemian/PushSDK/libs/utdid4all-1.1.5_proguard.jar does not exist.
Failed to transform file 'com.umeng.message_v3.1.5a.jar' to match attributes {artifactType=android-classes} using transform JarTransform
Transform output file /Users/dylan/AndroidStudioProjects/Jiemian/PushSDK/libs/com.umeng.message_v3.1.5a.jar does not exist.
解决: 修正不正常的 jar 包的依赖
5. com.novoda:bintray-release 升级到 0.8.0
出现问题:
No such property: FOR_RUNTIME for class: org.gradle.api.attributes.Usage
解决:
6. 删除 shrinkResources 配置
出现问题:
Caused by: org.gradle.api.GradleException: Resource shrinker cannot be used for libraries.
解决:
Library Module 删除 shrinkResources 配置
7. 对库的依赖改为 implementation
出现问题:
Project :moer_finance declares a dependency from configuration 'releaseCompile' to configuration 'release' which is not declared in the descriptor for project :Libraries:core.
解决:
debugCompile project(path: ':Libraries:core', configuration: 'debug')
releaseCompile project(path: ':Libraries:core', configuration: 'release')
这两行修改为->
implementation project(':Libraries:core')
8. 修改间接依赖
出现问题:
Caused by: java.lang.NullPointerException
at com.alibaba.android.arouter.compiler.processor.InterceptorProcessor.init(InterceptorProcessor.java:107)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$ProcessorState.<init>(JavacProcessingEnvironment.java:500)
解决:implementation 对外不暴露依赖,所以依赖方如果还依赖这个项目,那么需要在依赖方写明依赖关系。比如说 image 依赖 gradle 是用 implementation 配置的,app 依赖 image,如果 app 又想依赖 gradle,那么需要在 app 的 build.gradle 中写明。
9. 增加 annotationProcessor ‘io.realm:realm-android:0.87.5’
出现问题:
Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor. Please add them to the annotationProcessor configuration.
- realm-android-0.87.5.jar (io.realm:realm-android:0.87.5)
Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true to continue with previous behavior. Note that this option is deprecated and will be removed in the future.
See https://developer.android.com/r/tools/annotation-processor-error-message.html for more details.
解决:
dependencies {
//compile fileTree(dir: 'libs', include: ['*.jar'])
//compile 'com.android.support:support-v4:19.+'
provided 'io.realm:realm-android:0.87.5' // "optional" dependency to realm-database API
annotationProcessor 'io.realm:realm-android:0.87.5' // fix
}
10. 修改混淆配置
出现问题:
Unexpected error while performing partial evaluation:
Class = [com/umeng/socialize/shareboard/UMActionFrame]
Method = [createShareboardLayout(Ljava/util/List;)Landroid/view/View;]
Exception = [java.lang.IllegalArgumentException] (Can't find common super class of [com/umeng/socialize/shareboard/IndicatorView] (with 3 known super classes) and [com/umeng/socialize/shareboard/SocializeMenuAdapter] (with 1 known super classes))
Unexpected error while preverifying:
Class = [com/umeng/socialize/shareboard/UMActionFrame]
Method = [createShareboardLayout(Ljava/util/List;)Landroid/view/View;]
Exception = [java.lang.IllegalArgumentException] (Can't find common super class of [com/umeng/socialize/shareboard/IndicatorView] (with 3 known super classes) and [com/umeng/socialize/shareboard/SocializeMenuAdapter] (with 1 known super classes))
Warning: Exception while processing task java.io.IOException: java.lang.IllegalArgumentException: Can't find common super class of [com/umeng/socialize/shareboard/IndicatorView] (with 3 known super classes) and [com/umeng/socialize/shareboard/SocializeMenuAdapter] (with 1 known super classes)
解决:混淆配置增加
-dontpreverify
总结
本次为了尽快完成升级,只是对必须要修改的地方进行了修改,并没有完全按照官方文档规范进行,比如说 complie 已被 api 和 implementation 替换,所以,这些更规范的用法以及编译速度的提升就是接下来可以继续优化的,敬请期待。