首页 > 解决方案 > 将 Firebase Crashlytics 添加到 Android 时出现“重复类 javax.inject.*”构建错误

问题描述

我正在尝试将Firebase Crashlytics添加到我的项目中。我收到以下错误。

Duplicate class javax.inject.Inject found in modules javax.inject-1.jar (javax.inject:javax.inject:1) and jetified-roboguice-3.0.1.jar (org.roboguice:roboguice:3.0.1)
Duplicate class javax.inject.Named found in modules javax.inject-1.jar (javax.inject:javax.inject:1) and jetified-roboguice-3.0.1.jar (org.roboguice:roboguice:3.0.1)
Duplicate class javax.inject.Provider found in modules javax.inject-1.jar (javax.inject:javax.inject:1) and jetified-roboguice-3.0.1.jar (org.roboguice:roboguice:3.0.1)
Duplicate class javax.inject.Qualifier found in modules javax.inject-1.jar (javax.inject:javax.inject:1) and jetified-roboguice-3.0.1.jar (org.roboguice:roboguice:3.0.1)
Duplicate class javax.inject.Scope found in modules javax.inject-1.jar (javax.inject:javax.inject:1) and jetified-roboguice-3.0.1.jar (org.roboguice:roboguice:3.0.1)
Duplicate class javax.inject.Singleton found in modules javax.inject-1.jar (javax.inject:javax.inject:1) and jetified-roboguice-3.0.1.jar (org.roboguice:roboguice:3.0.1)

Go to the documentation to learn how to Fix dependency resolution errors.

项目级别build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0'
        classpath 'com.google.gms:google-services:4.3.5'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.5.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        google()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用级别build.gradle

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

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "package.name"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 26
        versionName "1.4.1"
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath true
            }
        }
        buildConfigField "long", "TIMESTAMP", System.currentTimeMillis() + "L"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        checkReleaseBuilds true
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }

}

project.tasks.withType(JavaCompile) { task ->
    options.compilerArgs << "-AguiceAnnotationDatabasePackageName=package"
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation 'junit:junit:4.12'
    testImplementation project(':module1')
    testImplementation 'org.mockito:mockito-core:1.+'
    testImplementation('org.robolectric:robolectric:3.1-rc1') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
    implementation project(':module1')
    implementation project(':module2')
    implementation project(':roboguicehelper')
    compileOnly 'org.roboguice:roboblender:3.+'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'org.roboguice:roboguice:3.+'
    implementation 'com.google.android.material:material:1.0.0'
    //noinspection GradleCompatible
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation project(':module4')

    implementation platform('com.google.firebase:firebase-bom:26.6.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-crashlytics'
}

如果我删除它->implementation 'com.google.firebase:firebase-crashlytics'项目构建并运行良好。但我需要添加 Crashlytics。

Gradle 版本是gradle-5.6.4

有什么建议么?

标签: javaandroidfirebasegradlecrashlytics

解决方案


从下图中,我们可以看到roboguice库中已经有java.inject库。因此,Firebase Crashlytics 添加java.inject:java.inject:1@jar到导致错误的库中。

在此处输入图像描述

将应用程序级别中的以下行build.gradle

implementation 'com.google.firebase:firebase-crashlytics'

implementation ('com.google.firebase:firebase-crashlytics') {
    exclude module: "javax.inject"
}

修复了错误。


推荐阅读