Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 234
476rick Avatar asked Sep 04 '18 20:09

476rick


1 Answers

I was missing the

android {
    dataBinding {
        enabled = true
    }
}
like image 92
Alberto M Avatar answered Nov 12 '22 23:11

Alberto M