Execution failed for task ‘:app:preDebugBuild’.
Android dependency ‘androidx.core:core’ has different version for the compile (1.0.0) and runtime (1.0.1) classpath. You should manually set the same version via DependencyResolution
Android代码使用Android.support库来确保向后兼容性老版本。目前support库已经停止维护,并替换为AndroidX,AndroidX具有与旧库想通过的功能和一些附加功能,但这两套库是不兼容的,所以会报错。未来都会为AndroidX为主,Android Support Library已经不再建议使用,并会慢慢停止维护。另外,从Android Studio 3.4.2开始,新建的项目已经强制勾选使用AndroidX架构了。
Launching lib/main.dart on Redmi Note 7 Pro in debug mode...
Initializing gradle...
Resolving dependencies...
Running Gradle task 'assembleDebug'...
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:preDebugBuild'.
> Android dependency 'androidx.core:core' has different version for the compile (1.0.0) and runtime (1.0.1) classpath. You should manually set the same version via DependencyResolution
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 6s
Finished with error: Gradle task assembleDebug failed with exit code 1
解决办法
目前网上主要有两种解决办法,第一种就是规避使用AndroidX的插件,避免使用AndroidX,显然这将做出很大的妥协,绝对不是最好的方式。另一种就是集成AndroidX,这种方式其实也不简单,我第一次通过AS去做的时候就不知道为什么没有成功,手动的话又很麻烦,还容易出错,这也许不太适合较大的老项目。
针对我的问题我找到了解决办法,在项目级build.gradle文件中增加下面的代码块便能解决问题
subprojects {
project.configurations.all {
resolutionStrategy.eachDependency { details ->
if (details.requested.group == 'com.android.support'
&& !details.requested.name.contains('multidex') ) {
details.useVersion "27.1.1"
}
if (details.requested.group == 'androidx.core'
&& !details.requested.name.contains('androidx') ) {
details.useVersion "1.0.1"
}
}
}
}
}
完整的build.gradle代码
buildscript {
ext.kotlin_version = '1.3.0'
repositories {
// google()
// jcenter()
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
subprojects {
project.configurations.all {
resolutionStrategy.eachDependency { details ->
if (details.requested.group == 'com.android.support'
&& !details.requested.name.contains('multidex') ) {
details.useVersion "27.1.1"
}
if (details.requested.group == 'androidx.core'
&& !details.requested.name.contains('androidx') ) {
details.useVersion "1.0.2"
}
if (details.requested.group == 'androidx.appcompat'
&& !details.requested.name.contains('androidx') ) {
details.useVersion "1.0.2"
}
}
}
}
}
allprojects {
repositories {
// google()
// jcenter()
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}