0

Gradle:

buildscript {
ext.kotlin_version = '1.2.10'
repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

=========================

ext {
support_version = '27.0.2'
dagger_version = '2.14.1'
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    //support
    implementation "com.android.support:appcompat-v7:$support_version"
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    //rx
    implementation 'io.reactivex.rxjava2:rxjava:2.1.8'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    //test
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    //Dagger 2
    implementation "com.google.dagger:dagger:$dagger_version"
    kapt "com.google.dagger:dagger-compiler:$dagger_version"
    provided 'org.glassfish:javax.annotation:10.0-b28'
}

It's work well for me, but if I enable DataBinding:

dataBinding {
    enabled = true
}

I got a warning com.android.support:appcompat-v7:

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 27.0.2, 21.0.3. Examples include com.android.support:animated-vector-drawable:27.0.2 and com.android.support:support-v4:21.0.3 more... (Ctrl+F1)

and lost method checkSelfPermission in ContextCompat:

ContextCompat.checkSelfPermission(context, android.Manifest.permission.READ_SMS)

Unresolved reference: checkSelfPermission

Gradle file

Why enabling DataBinding leads to such an effect?

Alon Parker
  • 11
  • 1
  • 1
  • 5
  • Possible duplicate of [All com.android.support libraries must use the exact same version specification](https://stackoverflow.com/questions/42374151/all-com-android-support-libraries-must-use-the-exact-same-version-specification) – Zoe Jan 05 '18 at 12:57
  • It's other problem – Alon Parker Jan 09 '18 at 15:42

2 Answers2

6

Why enabling DataBinding leads to such an effect?

Behind the scenes, dataBinding { enabled = true } adds some dependencies for runtime libraries that support the generated data binding code:

  • com.android.databinding:adapters
  • com.android.databinding:baseLibrary
  • com.android.databinding:library

Those dependencies, in turn, currently have a dependency on an old version of support-v4 (21.0.3). That, in turn, triggers the build error that you are seeing, as Google is trying to enforce that all Support Library artifacts are on the same version.

FWIW, I filed an issue to get this fixed in the data binding framework. I hope that it will be fixed sometime before the heat death of the universe.

The workaround is to add your own dependency on support-v4:

implementation "com.android.support:support-v4:$support_version"

This will cause Gradle to pull in your requested version, which is newer than the one that data binding is seeking, and so Gradle assumes that it will be OK. In truth, it might not be OK, but so far, in my work, I haven't run into any problems.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Also, if you get Gradle Sync Error *"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"*, try **api 'com.android.support:support-v4:27.0.2'** instead of **implementation 'com.android.support:support-v4:$support_version'** – Zon Mar 06 '18 at 08:58
  • @Zon Thank you your comment solved my problem. Please do post it as a separate answer. Now that I got it to work, I'll be spending time understanding why it worked. – Abhinav Manchanda May 10 '18 at 06:27
  • @CommonsWare Where did you see what `dataBinding { enabled = true }` is doing? Is it in open source code somewhere? I am trying to figure out how to create the same behavior in an AOSP makefile. – Matt Oct 01 '19 at 21:13
  • @MattD: "Where did you see what dataBinding { enabled = true } is doing?" -- in the specific case of this question, I think that I looked at the External Libraries tree of a simple project to see the effects of toggling that setting. Crude, but effective. :-) "Is it in open source code somewhere?" -- [yes it is!](https://android.googlesource.com/platform/tools/base/+/studio-master-dev/build-system/README.md). – CommonsWare Oct 01 '19 at 22:11
1

If you get Gradle Sync Error...

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

... try api com.android.support:support-v4:27.0.2 instead of implementation com.android.support:support-v4:$support_version.

Zon
  • 18,610
  • 7
  • 91
  • 99