'DataBindingUtil is not working after adding safe-args dependency

I'm adding the safe-args dependency:

project level:

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$version_kotlin"
    classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$version_navigation"

App level:

apply plugin: 'androidx.navigation.safeargs'

When I sync the project, my data binding stops working, for example:

val binding: FragmentGameOverBinding = DataBindingUtil.inflate(
        inflater, R.layout.fragment_game_over, container, false)
binding.tryAgainButton.setOnClickListener {view: View ->
    view.findNavController().navigate(R.id.action_gameOverFragment2_to_gameFragment2)
}
return binding.root

binding.root shows an unresolved reference, and when I go to DataBindingUtil.java, I get the error:

Library source does not match the bytecode for class DataBindingUtil

I'm following the exact steps in Google's udacity course, and yet it seems that adding the safe-args dependency collide somehow with the data binding library. What can I to to fix this?



Solution 1:[1]

Go to you gradle.properties file check that you have this inside or add it :

android.enableJetifier=true
android.useAndroidX=true

Then go to your build.gradle (app) and add this under android{

dataBinding {
    enabled = true
}
compileOptions {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8
}

The udacity courses are not updated here is the dependencies version I use

Take note of this part :

Instead of using this as Udacity suggest (in build.gradle app level) DO NOT USE android.arch

// Navigation
implementation "android.arch.navigation:navigation-fragment-ktx:$version_navigation"
implementation "android.arch.navigation:navigation-ui-ktx:$version_navigation"

Use

// Navigation

implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha04"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0-alpha04"

And in build.gradle (project level)

classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0-alpha04

Here is the full list of dependencies with version used (Just in case)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21"

    // Support libraries
    implementation "androidx.constraintlayout:constraintlayout:1.1.3"

    // Android KTX
    implementation "androidx.core:core-ktx:"1.2.0"

    // Room and Lifecycle dependencies
    implementation "androidx.room:room-runtime:$version_room"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    kapt "androidx.room:room-compiler:"2.0.0"
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:"1.1.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:"1.1.0"

    // Navigation
    implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha04"
    implementation "androidx.navigation:navigation-ui-ktx:2.3.0-alpha04"

    // Testing
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

Solution 2:[2]

Make sure you have same $version_navigation for both navigation component library in build.gradle(module) and safeArgs in build.gradle(project). I was getting similar error. My issue was having different $version_navigation for safeArgs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Nik Mohammad