小菜因为种种原因需要升级 Android Gradle,Gradle > 3.0 时默认支持 Java 8;每次大版本升级都会涉及很多内容,小菜尽可能慎重,但还是简单记录一下升级过程中遇到的问题;
升级 classpath ‘com.android.tools.build:gradle:3.0.1’
小菜从 2.3.3 升级到 3.0.1 同步之后会有很多问题;
Q1:
Unable to resolve dependency for ':testsdk@debug/compileClasspath': Could not resolve project :testlibrary.
Unable to resolve dependency for ':testsdk@debugAndroidTest/compileClasspath': Could not resolve project :testlibrary.
...
A1:
Gradle 3.0.0 以后不能用 debugCompile project / debugCompile project 方式替换为 implementation project 即可;
debugCompile project(path: ':testlibrary', configuration: 'debug')
releaseCompile project(path: ':testlibrary', configuration: 'release')
替换为
implementation project(':testlibrary')
Q2:
All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html
A3:
所有类型都必须属于一个指定的类型维度,即一个产品特性组。必须将所有类型分配给类型维度;在需要修改的 Module.build 添加 flavorDimensions “versionCode” 即可;
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
flavorDimensions "versionCode"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
目前同步完之后没有异常,但是 debug 运行又会有新的问题;
Q3:
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.
- butterknife-7.0.1.jar (com.jakewharton:butterknife:7.0.1)
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.
A3:
小菜在项目中应用到 ButterKnife,需要添加注解处理器,使用 annotationProcessor 配置依赖项;
compile "com.jakewharton:butterknife:7.0.1"
替换为
implementation "com.jakewharton:butterknife:7.0.1"
annotationProcessor "com.jakewharton:butterknife:7.0.1"
Q4:
Execution failed for task ':test:javaPreCompileDebug'.
> 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.
- compiler-1.1.0.jar (android.arch.lifecycle:compiler:1.1.0)
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.
A4:
小菜在项目中使用了 Lifecycle,需要添加 Lifecycle 依赖项,删除以前 compile lifecycle 方式,将 Google Maven 代码库添加到项目中即可;小菜未使用 AndroidX 可以按需要自定义添加;
def lifecycle_version = "1.1.1"
// 包含ViewModel和LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
// 仅仅包含ViewModel
implementation "android.arch.lifecycle:viewmodel:$lifecycle_version"
// 仅仅包含LiveData
implementation "android.arch.lifecycle:livedata:$lifecycle_version"
// 仅仅包含Lifecycles
implementation "android.arch.lifecycle:runtime:$lifecycle_version"
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor
// 如果用Java8, 用于替代compiler
implementation "android.arch.lifecycle:common-java8:$lifecycle_version"
// 可选,ReactiveStreams对LiveData的支持
implementation "android.arch.lifecycle:reactivestreams:$lifecycle_version"
// 可选,LiveData的测试
testImplementation "android.arch.core:core-testing:$lifecycle_version"
Q5:
小菜目前运行打包都正常,但是同样的代码在其他开发同事上运行异常;
Type def recipe not found: /Users/gitspace/SogouNovel/commonlib/build/intermediates/typedefs.txt
A5:
小菜尝试之后发现,升级到 Gradle 3.0 之后,lamba 的版本也需要更新,将 lamba 的版本更新到 3.7.0 即可;
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.sogou.compress:compress-plugin:1.0.1'
classpath 'me.tatarka:gradle-retrolambda:3.7.0' //retrolambda
}
Tips:
小菜建议在升级过程中注意混淆文件的处理,尤其是借助三方 SDK 时,注意官网混淆文件的添加;
至此,小菜升级过程中遇到的小问题基本解决,大部分都可以在官网或参考各路大神的博客,但还是记录尝试一下,对以后遇到的问题进行扩展整理;如有问题,请多多指导!
来源: 阿策小和尚