迁移到androidX-无法导入/查找androidx.databinding.DatabindingUtil [英] Migration to androidX - can't import/find androidx.databinding.DatabindingUtil

查看:1825
本文介绍了迁移到androidX-无法导入/查找androidx.databinding.DatabindingUtil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Android MVVM应用程序.我只是手动切换到androidx库.这是我的build.gradle中的库:

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'

我更改了代码和布局文件中的所有内容,所有导入内容等. Gradle可以成功构建.

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

在MainActivity中,我有以下代码:

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);

这是MainActivity中唯一不显示androidx版本的软件包:

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

import android.databinding.DataBindingUtil;

import android.databinding.DataBindingUtil;

其他所有软件包都显示了自己的androidx版本.删除导入时,我只能选择android版本,而不能选择androidx.

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.

我收到以下错误:binding.setLifecycleOwner(this); 这是因为DatabindingUtil仍引用android.databinding包而不是androidx.databinding包.我的Android Studio告诉我,传递给方法setLifecycleOwner的参数必须为类型:anndroid.arch.lifecycle.LifecycleOwner.

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.

我不知道为什么我不能导入androidx.databinding.DatabindingUtil软件包.有谁知道我该如何解决这个问题?

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.

推荐答案

问题是我尝试手动进行操作.所以我升级到了Android Studio 3.2 RC 2. 之后,我使用了Android Studio的迁移功能.

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.

要获取Android Studio中的迁移功能,请转到>重构>迁移到AndroidX ...

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

此后,如果Android Studio给您错误,请尝试重建或使用>文件>无效的缓存&重新启动.

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

正如@cchcc所指出的,不要忘记将其添加到您的gradle.properties:

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

android.useAndroidX=true
android.enableJetifier=true

这篇关于迁移到androidX-无法导入/查找androidx.databinding.DatabindingUtil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆