3

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.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
MHogge
  • 5,408
  • 15
  • 61
  • 104

5 Answers5

5

Write below code in gradle

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

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)

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • I checked the migration map but the one that is causing an issue is `android.databinding.BaseObservable` and replacing it by `androidx.databinding.BaseObservable` does not resolve it. I also tryed a gradlew clean + a invalide cache and restart but without any successes. It seems that the package 'androidx.databinding' is empty (autocompletion doesn't offers anything when I type `import androidx.databinding`) – MHogge Oct 11 '18 at 12:12
  • Yes, autocomplete doesn't work until after a successful build when you have databinding in the project. You should fix the issue anyway and then look at the build console when you do a gradle build which should show you more issues. If you see any binding implementation classes (generated) then you can use the name to determine which xml file has problems (often binding variables are the issue) – Nick Cardoso Oct 11 '18 at 15:34
1

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.

Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • 1
    I know well those issues, I'm using databinding since its beginning but the project was well compiling before I migrate to `androidx` so I'm not sure this could be an error inside the code (and my base code is quite huge so it will be hard to find if their is any) – MHogge Oct 11 '18 at 12:15
  • Did you migrate after successful build. Migrate to androidx should be performed after successful build so that it can find all uses. – Khemraj Sharma Oct 11 '18 at 12:18
  • I think there is some unresolved path after migrating, so your build is failing. I also faced this kind of error, so I know manually checking is one way. Other way can be print all stack trace, See https://stackoverflow.com/a/37266244/6891563 – Khemraj Sharma Oct 11 '18 at 12:20
  • Yes I migrate after a successfull build (I just verify it by getting to the previous commit). I will try what you said and if it help me I will update my comments but it seems strange that a successfull build will then cause unresolved after migration, I'll let you know. – MHogge Oct 11 '18 at 13:21
  • You sure backed up project, try migrating again that old code. – Khemraj Sharma Oct 11 '18 at 13:24
  • Maybe I need to have gradle build tools set to 3.2.0 in order to have databinding? I downgraded to 3.1.3 because of an isse about `STRING_TOO_LARGE` mentionned in my question. – MHogge Oct 11 '18 at 13:53
  • They changed mechanism of data binding generation flow, If you use latest 3.2.0 then binding classes generate after building project. If you want take this benefit before 3.2 then set `android.databinding.enableV2=true` in `gradle.properties` – Khemraj Sharma Oct 11 '18 at 13:57
  • I've added the attribute but this doesn't change anything about the current issue. My code base is still in Java and not in Kotlin, maybe this matter? – MHogge Oct 11 '18 at 14:27
  • @MHogge did you get to the bottom of this issue in the end? – Eurig Jones Dec 18 '18 at 15:08
1

Add dataBinding true in buildFeatures

Final Code:

buildFeatures { viewBinding true dataBinding true }

Inside android block

Vikash Singh
  • 261
  • 2
  • 7
0

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.

atschpe
  • 121
  • 14