Alon Parker
Alon Parker

Reputation: 11

DataBinding (libraries must use the exact same version specification)

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?

Upvotes: 0

Views: 1161

Answers (2)

Zon
Zon

Reputation: 19880

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.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006799

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.

Upvotes: 6

Related Questions