从 androidx 和 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 (请参阅 给你了解我使用的替换工具) ,但是 com.android.support库仍然在使用,即使我删除了这些库(它们只是在我再次尝试编译时被重新制作)。

这里有一个指向我得到的完整错误的链接。

92464 次浏览

Add these lines to your gradle.properties

android.useAndroidX=true
android.enableJetifier=true

If gradle.properties is absent, create the file in the root directory of your project.

It looks like you have some support relics anywhere in your dependencies, maybe from a lib? To investigate and see the dependencies tree, please run:

./gradlew :yourApp:dependencies

and then you see from where it comes.

In your app build.gradle you are using a mix of old support library and new support library artifacts. Remove one of them from your build.gradle to resolve this issue.

Old artifact: com.android.support:support-compat

Corresponding new artifact: androidx.core:core:1.0.0

See this page for more information on migrating to AndroidX.

This solution from here worked the best for me. Migrating to 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.

Like others says, the solution is to migrating to AndroidX, it works for me. However, it isn´t an easy way and it requires a bit of pacience... These are the steps that I did:

  • First, is very important that you do all this changes in a different branch or you make a backup of your project.

  • You need to have the Android Gradle Plugin Version 3.5.1. So, in build.gradle set:

dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
}
  • Migrate to AndroidX using Android Studio Tool : Refactor --> Migrate to AndroidX...
  • When it finishes, it has done all pertinents modification, but posibly you can´t deploy the project correctly because you find any errors. These are the problems that I found and the solutions:

  • If you use Kotlin, in build.gradle set:

buildscript {
ext.kotlin_version = '1.3.10'
}

and

compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  • If you use destination method, add "file" parameter: destination file("$reportsDir/checkstyle/checkstyle.xml")
  • If you use Butterknife, use 10.0.0 version
  • Finally, Build --> Clean Project and Build --> Rebuild Project

If you face any issue regarding duplicate classes after using Facebook mediation and google ad libraries so follow these steps:

1- Add this in build.gradle(Module) in Top

apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.google.gms.google-services'

2- Add this in build.gradle(Module) -> android

 packagingOptions {
exclude "META-INF/DEPENDENCIES"
}

3- Add this in 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- Add this in 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- Add this in 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