gradle 2.x升级到4.4
起因说来也神奇,是为了因为opus音频无法播放的问题
用的exoplayer的extension的解决方案,刚好项目之前用的也是google的ExoPlayer
由于项目足够老,用的gradle版本过低,ExoPlayer本地导入的时候需要的一些gradle语法不能识别,所以需要升级
现在记录一下gradle升级可能会遇到的错误
gradle 2.14.1 ->gradle-4.4
com.android.tools.build:gradle:2.2.3->3.1.2
首先你需要先解决网络问题,先确定问题是不是由于代理不通畅导致的
其次语法问题
问题一 compile project(':basic')
导致的 Cannot choose between the following configurations of project
Gradle sync failed: Cannot choose between the following configurations of project :ailib:
- debugApiElements
- debugRuntimeElements
- releaseApiElements
- releaseRuntimeElements
All of them match the consumer attributes:
- Configuration 'debugApiElements':
- Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required.
- Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
- Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
- Found org.gradle.api.attributes.Usage 'java-api' but wasn't required.
- Configuration 'debugRuntimeElements':
- Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required.
- Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
- Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
- Found ... (show balloon)
解决方案:修改 compile project(':basic')
为implementation project(':basic')
具体可能涉及比较多的compile关键字
compile project(':appstatistics')
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
修改为
implementation project(':appstatistics')
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
问题二 instrumentTest
被弃用导致的 The SourceSet ‘instrumentTest’ is not recognized by the Android Gradle Plugin
ERROR: The SourceSet 'instrumentTest' is not recognized by the
Android Gradle Plugin. Perhaps you misspelled something?
Affected Modules: XXXXX, XXXXX
instrumentTest.setRoot(‘tests’) ,你可能在升级 Android Studio 时更新了项目的 gradle 配置,这里的配置中 instrumentTest 已被弃用,不适用于现在的 gradle 版本。
解决办法
用 androidTest 替换 instrumentTest,编译运行即可。
问题三 ERROR: Resource shrinker cannot be used for libraries.
ERROR: Resource shrinker cannot be used for libraries.
Affected Modules: XXXX
原因是在类库的配置文件中使用了混淆的配置,删除掉即可
解决方案
buildTypes {
release {
minifyEnabled false
// shrinkResources true //删除此行即可
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
问题四 ERROR: android-apt plugin is incompatible with the Android Gradle plugin. Please use ‘annotationProcessor’ configuration instead.Affected Modules: XXXXX
解决方案
- 删除project/build.gradle中的apt插件声明
buildscript {
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
jcenter()
maven { url 'https://dl.bintray.com/umsdk/release' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
// classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
- 删除 app/build.gradle下的apt插件声明
apply plugin: 'com.android.application'
//apply plugin: 'com.neenbedankt.android-apt'
- 将apt 改为 annotationProcessor
dependencies {
...........
...........
//apt '**********'
annotationProcessor '**********'
..............
..........
}
问题五Android dependency ‘com.android.support:cardview-v7’ has different version for the compile (21.0.3) and runtime (27.1.0) classpath. You should manually set the same version via DependencyResolution
多个module引用了相同的库但是版本不同
解决办法
- 手动查找所有的cardview 修改为相同的版本
- Use this code in your buildscript (build.gradle root):
subprojects {project.configurations.all { resolutionStrategy.eachDependency { details -> if (details.requested.group == 'com.android.support' && !details.requested.name.contains('multidex') ) { details.useVersion "version which should be used - in your case 26.0.0-beta2" } } } }