'Can't resolve androidx.databinding

I'm trying to migrate my app to AndroidX, following the official doc. I run the refactor option: migrate to AndroidX.

At first I had errors that has been resolved by restarting and cleaning my project. I then had an issue STRING_TOO_LARGE (cf. this Stack Overflow question) that I resolved by downgrading my gradle build tools to 3.1.3.

But now I'm struggling with databinding. The migration tool replaced all my

import android.databinding.**

by

import androidx.databinding.**

but I have the error message

cannot resolve androidx.databinding.

Is their any things I should do to get it working?


I tried to get back to the old databinding by setting back

import android.databinding.**

Instead of the androidx one but I then have an error with LiveData used inside xml layout saying

cannot find the setter for attribute with parameter type androidx.lifecycle.MutableLiveData.



Solution 1:[1]

Write below code in gradle

android {
...
dataBinding {
    enabled = true
}
}

Solution 2:[2]

Androidx.databinding is the correct package, for future issues you can look up the mappings here: https://developer.android.com/jetpack/androidx/migrate#migrate.

Android Studio seems to be having a problem when switching branches or projects that are and aren't migrated though. At the moment the only fix working for me is to do a gradle clean and then restart android studio (either after switching branches or after migrating)

Solution 3:[3]

Data binding has very weird issue, that when you have some syntax error, or some imports error, it will show 100 of errors of binding, but not the actual error.

What you can do

Open each xml file then java file of your work, and see if there are unresolved imports or errors. If you find some error, resolve and build project, Data binding classes only generate with successful build.

I have explained it well in my @this answer.

Solution 4:[4]

I had a similar issue after migrating to AndroidX. For me, I got an error when setting the contentView for the activity, the rest of the Databinding code was deemed ok.

What worked for me, in the end, was to Invalidate Cache & Restart. Sadly, that seems to be a common need when using AndroidStudio. Hopefully, they can get this fixed in time.

Solution 5:[5]

Add dataBinding true in buildFeatures

Final Code:

buildFeatures { viewBinding true dataBinding true }

Inside android block

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 comnet tester
Solution 2 Nick Cardoso
Solution 3 Community
Solution 4 atschpe
Solution 5 Vikash Singh