Search code examples
androidgradlemvvmandroid-gradle-pluginandroidx

Migration to androidX - can't import/find androidx.databinding.DatabindingUtil


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.


Solution

  • 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