AndroidX 依赖项和 XML 导致崩溃

AndroidX dependencies and XML causing crash

我最近更新到Android 3.3,现在很多依赖都改成了AndroidX。这导致应用程序崩溃,并且还需要在 XML 中完成依赖项更改。我遇到了与 CoordinatorLayout 类似的问题。我搜索了 Whosebug,关于这个主题的问题并不多,因为 AndroidX 相对较新。所以我觉得有必要提出一个这方面的问题。

我在哪里可以为 FloatingActionButton 获得 xml 类似地对于所有 XML 是否有一个网站可以让我们获得 androidX 依赖项的 XML。

XML

 <android.support.design.widget.FloatingActionButton

崩溃Logcat

android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class android.support.design.widget.FloatingActionButton Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class android.support.design.widget.FloatingActionButton Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.FloatingActionButton" on path: DexPathList[[zip file "/data/app/com.kc.unsplashdemo-kTZQS-p2845Xly7PcTuAmg==/base.apk", zip file "/data/app/com.kc.unsplashdemo-kTZQS-p2845Xly7PcTuAmg==/split_lib_dependencies_apk.apk", zip file

依赖关系

dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation project(':androidunsplash')
        implementation 'com.squareup.picasso:picasso:2.71828'
        implementation("com.squareup.okhttp3:okhttp:3.12.0")
        implementation 'androidx.recyclerview:recyclerview:1.0.0'
        implementation 'com.google.code.gson:gson:2.8.5'
    
        implementation 'com.google.android.material:material:1.0.0'
        implementation 'androidx.appcompat:appcompat:1.0.2'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.1'
    }

添加 androidX 的依赖项是不够的。要将您的组件迁移到 androidX,您必须手动更改它们。虽然搜索所有新包可能很耗时,但我建议您尝试删除 xml 中的旧名称,然后开始输入新名称。如果 androidX 依赖项确实添加到您的项目中,在您开始输入 Android 后,Studio 会建议您一个新的包名称。

此外,您可以查找其他信息here

您可以找到旧 android 依赖关系 与使用 新依赖关系 的映射androidx here

在那里你会找到你正在寻找的依赖项:

com.google.android.material.floatingactionbutton.FloatingActionButton

在布局上替换某些内容时,我建议使用 ctrl + r 在整个项目中更新它并节省您的时间,因为迁移到 [=25 时确实需要做很多手动工作=]x.

您将 Android Studio 升级到 3.3 时似乎遇到了问题。要解决此问题,可以执行以下操作:
a) 如果您已将代码迁移到 Androidx,请检查应用程序中的 gradle.properties 如果这两行不存在:

android.useAndroidX=true
android.enableJetifier=true

然后转到 android 工作室中的 Refactor -> Migrate to Androidx,将您的应用程序迁移到 Androidx,包括 xml 文件。此选项将在下面的终端中显示更改接受这些更改以迁移到 Androidx。

b) 如果这样做不起作用并且您仍然收到该错误,请替换此

android.support.design.widget.FloatingActionButton

com.google.android.material.floatingactionbutton.FloatingActionButton

取自 google 提供的映射文件,该文件将早期支持库 类 映射到 androidx 包 类 : Migrating to Androidx