Android Studio 从 2.3 升级到3.2以后,对于旧工程的引入,需要如下处理:
1. 工程下的引入studio 版本和 gradle 的版本需要跟进,如studio 3.2.1和gradle 4.6:
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
2. project的build.gradle文件中删除
1)classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
module的build.gradle文件中删除
2)apply plugin: 'com.neenbedankt.android-apt'
module的build.gradle文件中替换
3)//apt 'com.jakewharton:butterknife-compiler:8.0.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.0.1'
3. buildVersionCode 可以删除掉
高版本中,buildVersionCode可以省略不些,系统会自动选用合适的版本进行编译。
如果不去掉,要查看兼容的最低版本是多少,相应提高。
4. compile 相关关键词替换
Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
Configuration 'androidTestCompile' is obsolete and has been replaced with 'androidTestImplementation' .
Configuration 'testCompile' is obsolete and has been replaced with 'testImplementation' .
Configuration 'provided' is obsolete and has been replaced with 'compileOnly'.
‘implementation’ and 'api’的区别,详情可参考链接 https://www.jianshu.com/p/8962d6ba936e
这是因为implementation只能用于当前的module,倘若在库中用这种方式设置依赖,我们在app的module就引用不到。但是api可以。
如果遇到有些api无法识别的,可以替换 “implementation”试试。
另外有两点需要注意:
Configuration 'testApi' is obsolete and has been replaced with 'testImplementation'.
It will be removed at the end of 2018.
Configuration 'androidTestApi' is obsolete and has been replaced with 'androidTestImplementation'.
It will be removed at the end of 2018.
5. All flavors must now belong to a named flavor dimension.
在主app的build.gradle里面的
defaultConfig {
targetSdkVersion:***
minSdkVersion :***
versionCode:***
versionName :***
//版本名后面添加一句话,意思就是flavor dimension 它的维度就是该版本号,这样维度就是都是统一的了
flavorDimensions "versionCode"
}
6. manifest中不能再指定 minSdk version,可在对应的build.gradle文件中配置。
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.
7. AndroidStudio3.0 以上版本,之前项目的butter knife报错,用到注解的应该都会报错
Error:Execution failed for task ':app: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.
- butterknife-7.0.1.jar
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.
在app的build中,添加如下语句:
android {
...
defaultConfig {
...
//添加如下配置就OK了
javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
}
...
}
8. 没有google相关文件报的错
Could not find com.android.tools.build:gradle:3.2.1.
Searched in the following locations:
https://jcenter.bintray.com/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.pom
https://jcenter.bintray.com/com/android/tools/build/gradle/3.2.1/gradle-3.2.1.jar
Required by:
project :
Add Google Maven repository and sync project
Open File
在项目的gradle文件中,添加如下maven:
repositories {
...
maven {
url 'https://maven.google.com/'
name 'Google'
}
}
9. Error:Unable to resolve dependency for ‘:@debug/compileClasspath’,Could not resolve project
造成这个错误的原因是app下build.gradle里面的buildtypes配置和module下build.gradle中的
buildtypes配置不一样。
解决方案:app下build.gradle里面的buildtypes配置和module下build.gradle中的buildtypes配置要一样。
PS:升级后会遇到各种问题,一定要尽量正确解决问题,否则可能会出现错误方式导致的错误问题,最终导致无解。。。