12

I'm working on an Android MVVM application. I just switched to the androidx libraries manually. This are the libraries in my build.gradle:

implementation 'com.google.code.gson:gson:2.8.5'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0-rc02'
implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0-rc02'
implementation 'com.google.android.material:material:1.0.0-rc01'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
implementation 'com.airbnb.android:lottie:2.5.5'
implementation 'androidx.recyclerview:recyclerview:1.0.0-rc02'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-rc01'
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.0.0-rc01'
implementation 'androidx.lifecycle:lifecycle-livedata:2.0.0-rc01'
implementation 'androidx.lifecycle:lifecycle-runtime:2.0.0-rc01'
implementation "androidx.lifecycle:lifecycle-common-java8:2.0.0-rc01"
implementation "androidx.lifecycle:lifecycle-reactivestreams:2.0.0-rc01"
testImplementation "androidx.arch.core:core-testing:2.0.0-rc01"
implementation "androidx.room:room-runtime:2.0.0-rc01"
annotationProcessor "androidx.room:room-compiler:2.0.0-rc01"
implementation "androidx.room:room-rxjava2:2.0.0-rc01"
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

I changed everything in the code and layout files, all the imports and such. Gradle can build succesfully.

In my MainActivity I have the following code:

viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setVariable(BR.viewModel, viewModel);
binding.setLifecycleOwner(this);

This is the only package in my MainActivity that won't show the androidx version of it:

import android.databinding.DataBindingUtil;

Every other package showed the androidx version of himself. When I remove the import I can only choose the android version and not the androidx.

I get an error in: binding.setLifecycleOwner(this); That's because the DatabindingUtil still refers to the android.databinding package instead of the androidx.databinding package. My Android Studio tells me that the parameter passed to the method setLifecycleOwner must be of type: anndroid.arch.lifecycle.LifecycleOwner.

I don't know why I can't import the androidx.databinding.DatabindingUtil package. Does anyone know how I can fix this problem?

I did try to rebuild and invalidate cache & restart.

476rick
  • 2,764
  • 4
  • 29
  • 49

3 Answers3

13

I was missing the

android {
    dataBinding {
        enabled = true
    }
}
Alberto M
  • 1,608
  • 1
  • 18
  • 42
11

The problem was that I tried to do it manually. So I upgraded to Android Studio 3.2 RC 2. After that I used the migrate function of Android Studio.

For the migrate function in Android Studio go to > Refactor > Migrate to AndroidX...

When Android Studio gives you errors after that, try to Rebuild or maybe use > File > Invalidate Cache & Restart.

As @cchcc pointed out, don't forget to add this to your gradle.properties:

android.useAndroidX=true
android.enableJetifier=true
476rick
  • 2,764
  • 4
  • 29
  • 49
  • `gradle.properties` seem to be automatically updated by recent AS when doing `Migrate to AndroidX`, so no need to manual intervention. – Marcin Orlowski Feb 18 '19 at 17:19
  • 1
    This was driving me nuts, turned out the project started as a non migrated one and we manually added the required implementation stuff in gradle. Adding this fixed the issue. – Peterdk Nov 19 '19 at 15:14
  • @476rick can you add if you (or Android Studio) eventually changed the import `import android.databinding.DataBindingUtil;` to something else? I get the same error message but I can't find the namespace `androidx.databindng...` Did you change it eventually or not? – Bruno Bieri Nov 27 '19 at 15:57
  • 1
    @BrunoBieri The correct import is: import androidx.databinding.DataBindingUtil; I do not remember if Android Studio did it or that I did it manually. You can try to Rebuild or to: Invalidate Caches / Restart – 476rick Dec 05 '19 at 16:35
6
  1. Be sure you are using Android Studio 3.2 RC 2

  2. Add 2 lines below in gradle.properties file

    android.useAndroidX=true
    android.enableJetifier=true

cchcc
  • 335
  • 2
  • 6
  • 1
    Thanks, I just fixed it by doing it via the new Android Studio 3.2 RC2. The important thing was to do it by using > Refactor > Migrate to AndroidX... – 476rick Sep 07 '18 at 17:16