【问题标题】:Android Studio complains duplicate classAndroid Studio 抱怨重复类
【发布时间】:2020-04-20 13:23:02
【问题描述】:

我正在尝试生成一个 apk 文件,但 android studio 显示以下错误:

重复类 com.google.android.gms.ads.identifier.AdvertisingIdClient 发现于 模块类.jar (com.google.android.gms:play-services-ads-identifier:17.0.0) 和 classes.jar (com.google.android.gms:play-services-analytics:7.8.0) 重复类 com.google.android.gms.ads.identifier.AdvertisingIdClient$Info 找到 在模块 classes.jar (com.google.android.gms:play-services-ads-identifier:17.0.0) 和 classes.jar (com.google.android.gms:play-services-analytics:7.8.0) 重复类 com.google.android.gms.ads.identifier.AdvertisingIdClient$zza 发现于 模块类.jar (com.google.android.gms:play-services-ads-identifier:17.0.0) 和 classes.jar (com.google.android.gms:play-services-analytics:7.8.0)

它以前工作过。我完全没有对代码进行任何更改。有人可以帮忙吗?

这是我的 build.gradle 文件:

apply plugin: "com.android.application"

import com.android.build.OutputFile

def jscFlavor = 'org.webkit:android-jsc:+'


def enableHermes = project.ext.react.get("enableHermes", false);

android {
compileSdkVersion rootProject.ext.compileSdkVersion

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

defaultConfig {
    applicationId "com.step_by_step.virq"
    minSdkVersion rootProject.ext.minSdkVersion
    targetSdkVersion rootProject.ext.targetSdkVersion
    versionCode 1
    versionName "1.0"
}
splits {
    abi {
        reset()
        enable enableSeparateBuildPerCPUArchitecture
        universalApk false  // If true, also generate a universal APK
        include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
    }
}
signingConfigs {
    debug {
        storeFile file('debug.keystore')
        storePassword 'android'
        keyAlias 'androiddebugkey'
        keyPassword 'android'
    }
}
buildTypes {
    debug {
        signingConfig signingConfigs.debug
    }
    release {
        // Caution! In production, you need to generate your own keystore file.
        // see https://facebook.github.io/react-native/docs/signed-apk-android.
        signingConfig signingConfigs.debug
        minifyEnabled enableProguardInReleaseBuilds
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
    }
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
    variant.outputs.each { output ->
        // For each separate APK per architecture, set a unique version code as described here:
        // https://developer.android.com/studio/build/configure-apk-splits.html
        def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
        def abi = output.getFilter(OutputFile.ABI)
        if (abi != null) {  // null for the universal-debug, universal-release variants
            output.versionCodeOverride =
                    versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
        }

    }
}
}

dependencies {
implementation project(':react-native-push-notification')
implementation project(':react-native-firebase')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.facebook.react:react-native:+"  // From node_modules
implementation "com.google.android.gms:play-services-base:16.1.0"
implementation "com.google.firebase:firebase-core:17.0.1"
implementation "com.google.firebase:firebase-messaging:19.0.1"
implementation 'me.leolin:ShortcutBadger:1.1.21@aar'
implementation "com.google.android.gms:play-services:7.8.0"

if (enableHermes) {
    def hermesPath = "../../node_modules/hermes-engine/android/";
    debugImplementation files(hermesPath + "hermes-debug.aar")
    releaseImplementation files(hermesPath + "hermes-release.aar")
} else {
    implementation jscFlavor
}
}

task copyDownloadableDepsToLibs(type: Copy) {
from configurations.compile
into 'libs'
}

apply from: file("../../node_modules/@react-native-community/cli-platform- 
android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
apply plugin: "com.google.gms.google-services"

【问题讨论】:

  • 添加你的 build.gradle 文件

标签: android firebase react-native android-studio


【解决方案1】:

您的 play-services-analytics 版本 [7.8.0] 非常旧。尝试使用最新的

implementation 'com.google.android.gms:play-services-analytics:17.0.0'
implementation 'com.google.android.gms:play-services-ads:18.3.0'

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,我创建了一个新项目并添加了一些旧库。 早在编写这些库和其他使用它们的项目时,它们就在某个时候迁移到了 AndroidX。如果我没记错的话,gradle.properties (Project Properties) 文件中添加了以下几行:

    android.useAndroidX=true
    # Automatically convert third-party libraries to use AndroidX
    android.enableJetifier=true
    

    在我的新项目中,android.enableJetifier=true 行丢失,导致出现此错误。

    【讨论】:

      【解决方案3】:

      这是由于 firebase 和 google 服务依赖项之间的冲突而发生的。您基本上有两种解决方案。

      您可以更改版本并继续这样做,直到获得更耗时的完美工作组合,或者您可以找到一个每次都有效的解决方案,这样您就不必每次都更改版本。每当遇到此错误时,您只需使用该解决方案即可。

      因此,您需要使用更新后的注入 firebase 依赖项的方法,如 android 开发者网站所示。

      使用火力基地“bom”

      将此行放入您的依赖项中:

      implementation platform('com.google.firebase:firebase-bom:28.1.0')
      

      然后,您可以在不指定版本的情况下使用任何 firebase 依赖项,并且每次都能完美运行!

      最终的解决方案是这样的,

      implementation platform('com.google.firebase:firebase-bom:28.1.0')
      implementation 'com.google.firebase:firebase-analytics'
      implementation 'com.google.firebase:firebase-config'
      implementation 'com.google.firebase:firebase-crashlytics'
      

      所以现在,它不会产生任何与 play-services 依赖项相关的重复错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多