数据绑定插件使用旧版本的支持库

Data Binding plugin uses old version of Support library

将支持库更新到 27.0.2 后出现此错误。

Error:Execution failed for task ':app:preDebugBuild'.
> Android dependency 'com.android.support:support-v4' has different version for the compile (21.0.3) and runtime (27.0.2) classpath. You should manually set the same version via DependencyResolution

我在我的项目中启用了数据绑定。

dataBinding.enabled = true

当我运行

gradlew -q dependencies app:dependencies --configuration debugAndroidTestCompileClasspath

这是我得到的:

+--- com.android.databinding:library:1.3.1
|    +--- com.android.support:support-v4:21.0.3
|    |    \--- com.android.support:support-annotations:21.0.3 -> 26.0.2
|    \--- com.android.databinding:baseLibrary:2.3.0-dev -> 3.0.1
+--- com.android.databinding:baseLibrary:3.0.1
+--- com.android.databinding:adapters:1.3.1
|    +--- com.android.databinding:library:1.3 -> 1.3.1 (*)
|    \--- com.android.databinding:baseLibrary:2.3.0-dev -> 3.0.1
+--- com.squareup.leakcanary:leakcanary-android:1.5.1
|    \--- com.squareup.leakcanary:leakcanary-analyzer:1.5.1
|         +--- com.squareup.haha:haha:2.0.3
|         \--- com.squareup.leakcanary:leakcanary-watcher:1.5.1
+--- project :general
|    +--- com.android.databinding:library:1.3.1 (*)
|    +--- com.android.databinding:baseLibrary:3.0.1
|    \--- com.android.databinding:adapters:1.3.1 (*)
+--- com.jakewharton:butterknife:8.8.1
|    \--- com.jakewharton:butterknife-annotations:8.8.1
+--- com.github.bumptech.glide:glide:4.2.0
|    +--- com.github.bumptech.glide:gifdecoder:4.2.0
|    |    \--- com.android.support:support-annotations:26.0.2
|    +--- com.github.bumptech.glide:disklrucache:4.2.0
|    \--- com.github.bumptech.glide:annotations:4.2.0
\--- com.github.apl-devs:appintro:v4.2.2

这清楚地表明 Android 数据绑定库正在使用版本 21.0.3 的 v4 支持,这会导致冲突。

有人能帮忙吗?

将此添加到您的 dependencies 闭包中:

  implementation "com.android.support:support-v4:26.0.2"

这将导致 Gradle 升级 support-v4 依赖项以匹配其余的支持库工件。

好的。我自己找到解决方案: 在您的应用程序模块中添加以下内容(不在库或任何其他模块中):

android {
...
    configurations.all {
            resolutionStrategy.force "com.android.support:support-v4:${supportLib}"
            resolutionStrategy.force "com.android.support:appcompat-v7:${supportLib}"
            resolutionStrategy.force "com.android.support:design:${supportLib}"
            resolutionStrategy.force "com.android.support:recyclerview-v7:${supportLib}"
            resolutionStrategy.force "android.arch.lifecycle:runtime:${lifecycleExtensions}"
        }
}

它将强制gradle使用更新的依赖项并解决冲突错误。