升级SDK和Gradle后Android应用报错:DependencySubstitutions异常
问题描述
时隔两年重新启用Android应用,升级SDK和Gradle版本后遇到如下报错:
Failed to notify dependency resolution listener. 'void org.gradle.api.artifacts.DependencySubstitutions$Substitution.with(org.gradle.api.artifacts.component.ComponentSelector)'
现有配置
App模块build.gradle
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' android { compileSdk 31 defaultConfig { applicationId "com.example.myapp" minSdkVersion 17 targetSdkVersion 31 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } flavorDimensions "default" productFlavors { free { applicationId "com.example.myapp" } paid { applicationId "com.example.myapp.paid" } pro { applicationId "com.example.myapp.pro" } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } buildToolsVersion '33.0.1' /*packagingOptions { pickFirst 'META-INF/LICENSE.txt' // picks the JavaMail license file }*/ } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.2.1' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'androidx.recyclerview:recyclerview:1.1.0' testImplementation 'junit:junit:4.13.1' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' implementation 'com.loopeer.library:cardstack:1.0.2' implementation 'androidx.cardview:cardview:1.0.0' implementation 'com.mutualmobile.android:cardstack:0.5.3' implementation 'com.savvi.datepicker:rangepicker:1.2.0' implementation 'com.github.badoualy:datepicker-timeline:c6dcd05737' implementation 'com.github.lecho:hellocharts-android:v1.5.8' implementation 'com.ramotion.foldingcell:folding-cell:1.2.3' implementation 'com.github.Binary-Finery:EZDialog:2.0' implementation 'com.michaelmuenzer.android:ScrollableNumberPicker:0.2.2' implementation 'com.shuhart.bubblepagerindicator:bubblepagerindicator:1.1.2' //for lifecycle and LiveData and ViewModel implementation 'io.reactivex.rxjava2:rxandroid:2.0.1' implementation 'io.reactivex.rxjava2:rxjava:2.1.1' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation "androidx.activity:activity-ktx:1.1.0" implementation 'com.github.Binary-Finery:EZDialog:2.0' def room_version = "2.2.5" /* // https://mvnrepository.com/artifact/joda-time/joda-time implementation group: 'joda-time', name: 'joda-time', version: '2.3' */ implementation "androidx.room:room-runtime:$room_version" annotationProcessor "androidx.room:room-compiler:$room_version" // optional - RxJava support for Room implementation "androidx.room:room-rxjava2:$room_version" // optional - Guava support for Room, including Optional and ListenableFuture implementation "androidx.room:room-guava:$room_version" // optional - Test helpers testImplementation "androidx.room:room-testing:$room_version" implementation "androidx.core:core-ktx:+" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" /* implementation 'com.sun.mail:android-mail:1.6.2' implementation 'com.sun.mail:android-activation:1.6.2' */ } repositories { mavenCentral() }
Project模块build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = '1.4.30' repositories { mavenCentral() google() } dependencies { classpath 'com.android.tools.build:gradle:4.2.1' //classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { mavenCentral() google() //maven { url 'https://jitpack.io' } } } task clean(type: Delete) { delete rootProject.buildDir }
已尝试更换Gradle版本,但问题未解决。
解决步骤
这个报错核心原因是Gradle插件、Kotlin版本、第三方依赖三者不兼容,或依赖仓库配置缺失,按以下步骤解决:
修复Gradle插件与Kotlin版本兼容性
当前Gradle插件4.2.1与Kotlin 1.4.30的组合对compileSdk 31支持不足,建议升级到适配的稳定版本:
修改Project的build.gradle:buildscript { ext.kotlin_version = '1.5.31' repositories { mavenCentral() google() } dependencies { classpath 'com.android.tools.build:gradle:7.0.4' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }同时修改
gradle/wrapper/gradle-wrapper.properties中的Gradle版本:distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip恢复JitPack仓库配置
依赖中包含多个com.github.*开头的第三方库,这些库必须依赖JitPack仓库才能解析,恢复Project的build.gradle中注释掉的配置:allprojects { repositories { mavenCentral() google() maven { url 'https://jitpack.io' } } }清理重复依赖
删除dependencies中重复引入的com.github.Binary-Finery:EZDialog:2.0,避免依赖冲突。更新核心依赖版本
将老旧AndroidX依赖升级到适配compileSdk 31的版本,同时替换动态版本号为具体稳定版本:implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' implementation 'androidx.recyclerview:recyclerview:1.2.1' implementation "androidx.activity:activity-ktx:1.4.0" implementation "androidx.core:core-ktx:1.7.0"注:
lifecycle-extensions已废弃,后续可逐步替换为lifecycle-viewmodel和lifecycle-livedata的单独依赖。同步并重建项目
执行File -> Sync Project with Gradle Files,再依次执行Build -> Clean Project和Build -> Rebuild Project。
内容的提问来源于stack exchange,提问作者Adam

