从 androidx 和 com.android.support 复制 类

Duplicate classes from androidx and com.android.support

我最近更新了我的 Android Studio(我很确定 Gradle 版本),现在我在尝试编译我的项目时遇到了一堆错误。这是目前困扰我的问题:

Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:26.1.0)

接下来还有很多类似的。

我尝试删除所有对 com.android.support 的使用以支持 androidx(请参阅 here 了解我使用的替换内容),但 com.android.support 库仍在使用,甚至当我删除库时(它们只是在我尝试再次编译时重新制作)。

Here's link 我得到的完整错误。

将这些行添加到您的 gradle.properties

android.useAndroidX=true
android.enableJetifier=true

如果 gradle.properties 不存在,请在项目的根目录中创建文件。

看起来您的依赖项中的任何地方都有一些 support 遗物,也许来自库?要调查并查看依赖关系树,请 运行:

./gradlew :yourApp:dependencies

然后你就知道它是从哪里来的了。

在您的应用程序 build.gradle 中,您混合使用了旧支持库和新支持库工件。从您的 build.gradle 中删除其中一个以解决此问题。

旧神器:com.android.support:support-compat

对应的新神器:androidx.core:core:1.0.0

this page for more information on migrating to AndroidX

这里的解决方案对我来说效果最好。迁移到 androidX

https://developer.android.com/jetpack/androidx/migrate

With Android Studio 3.2 and higher, you can migrate an existing project to AndroidX by selecting Refactor > Migrate to AndroidX from the menu bar.

The refactor command makes use of two flags. By default, both of them are set to true in your gradle.properties file:

android.useAndroidX=true The Android plugin uses the appropriate AndroidX library instead of a Support Library. android.enableJetifier=true The Android plugin automatically migrates existing third-party libraries to use AndroidX by rewriting their binaries.

正如其他人所说,解决方案是迁移到 AndroidX,它对我有用。然而,这不是一个简单的方法,它需要一点耐心......这些是我所做的步骤:

  • 首先,非常重要您在不同的分支中进行所有这些更改或者备份您的项目。

  • 您需要 Android Gradle 插件版本 3.5.1。所以,在 build.gradle 中设置:

dependencies {
    classpath 'com.android.tools.build:gradle:3.5.1'
}
  • 使用 Android Studio 工具迁移到 AndroidX:重构 --> 迁移到 AndroidX...
  • 当它完成时,它已经完成了所有相关的修改,但是您可能无法正确部署项目,因为您发现了任何错误。这些是我发现的问题和解决方案:

  • 如果你使用 Kotlin,在 build.gradle 中设置:

buildscript {
    ext.kotlin_version = '1.3.10'
}

compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  • 如果使用destination方法,添加"file"参数:destination file("$reportsDir/checkstyle/checkstyle.xml")
  • 如果使用Butterknife,请使用10.0.0版本
  • 最后,构建 --> 清理项目构建 --> 重建项目

如果您在使用 Facebook 中介和 google 广告库后遇到有关重复 类 的任何问题,请按照以下步骤操作:

1- 在 Top

的 build.gradle(模块)中添加
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.google.gms.google-services'

2- 在 build.gradle(模块) -> android

中添加
 packagingOptions {
        exclude "META-INF/DEPENDENCIES"
    }

3- 在 build.gradle(Module) -> dependencies

中添加
    //firebase bom
    implementation platform('com.google.firebase:firebase-bom:29.2.0')
    implementation 'com.google.firebase:firebase-crashlytics'
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-messaging:23.0.1'

    //ADS
    def ads_version = "20.6.0"
    implementation "com.google.android.gms:play-services-ads:$ads_version"

    //facebook ads
    implementation 'com.google.ads.mediation:facebook:6.8.0.0'

4- 在 build.gradle(Project)-> buildscript-> dependencies

中添加
classpath "com.android.tools.build:gradle:7.0.4"
classpath 'com.google.gms:google-services:4.3.10'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'

5- 在 gradle.wrapper.propertise

中添加
#Wed Dec 16 15:32:03 PKT 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-7.0.2-bin.zip