You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity导入Android Studio集成Firebase遇重复类错误求助

Unity导入Android Studio后集成Firebase出现重复类错误

Unity项目导入Android Studio后可正常运行,但添加Firebase集成后,立即触发Execution failed for task ':launcher:checkDebugDuplicateClasses'错误。

已尝试添加以下配置但无效:

android.useAndroidX=true
android.enableJetifier=true

同时已正确放置Firebase的json配置文件。

错误核心信息:

Duplicate class androidx.annotation.AnimRes found in modules annotation-1.3.0.jar (androidx.annotation:annotation:1.3.0) and jetified-androidx.annotation.annotation-1.2.0.jar (androidx.annotation.annotation-1.2.0.jar)
Duplicate class androidx.annotation.AnimatorRes found in modules annotation-1.3.0.jar (androidx.annotation:annotation:1.3.0) and jetified-androidx.annotation.annotation-1.2.0.jar (androidx.annotation.annotation-1.2.0.jar)
...
Go to the documentation to learn how to Fix dependency resolution errors.

相关配置文件如下:

build.gradle(project)

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

        dependencies {
            // If you are changing the Android Gradle Plugin version, make sure it is compatible with the Gradle version preinstalled with Unity
            // See which Gradle version is preinstalled with Unity here https://docs.unity3d.com/Manual/android-gradle-overview.html
            // See official Gradle and Android Gradle Plugin compatibility table here https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
            // To specify a custom Gradle version in Unity, go do "Preferences > External Tools", uncheck "Gradle Installed with Unity (recommended)" and specify a path to a custom Gradle version
            classpath 'com.android.tools.build:gradle:4.0.1'
            classpath 'com.google.gms:google-services:4.3.14'

        }
    }

    repositories {
        google()
        jcenter()
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }
    }
}

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

build.gradle(module launcher)

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

dependencies {
    implementation project(':unityLibrary')
    implementation 'com.google.firebase:firebase-inappmessaging-display:20.2.0'
}

android {
    compileSdkVersion 30
    buildToolsVersion '30.0.2'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        minSdkVersion 22
        targetSdkVersion 30
        applicationId 'com.MyCompany.test'
        ndk {
            abiFilters 'armeabi-v7a'
        }
        versionCode 1
        versionName '1.0'
    }

    aaptOptions {
        noCompress = ['.unity3d', '.ress', '.resource', '.obb', '.bundle', '.unityexp'] + unityStreamingAssets.tokenize(', ')
        ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
    }

    lintOptions {
        abortOnError false
    }

    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt')
            signingConfig signingConfigs.debug
            jniDebuggable true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt')
            signingConfig signingConfigs.debug
        }
    }

    packagingOptions {
        doNotStrip '*/armeabi-v7a/*.so'
    }

    bundle {
        language {
            enableSplit = false
        }
        density {
            enableSplit = false
        }
        abi {
            enableSplit = true
        }
    }
}

build.gradle(module library)

apply plugin: 'com.android.library'


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation(name: 'googlemobileads-unity', ext:'aar')
    implementation project('GoogleMobileAdsPlugin.androidlib')

}

android {
    compileSdkVersion 30
    buildToolsVersion '30.0.2'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        minSdkVersion 22
        targetSdkVersion 30
        ndk {
            abiFilters 'armeabi-v7a'
        }
        versionCode 1
        versionName '1.0'
        consumerProguardFiles 'proguard-unity.txt'
    }

    lintOptions {
        abortOnError false
    }

    aaptOptions {
        noCompress = ['.unity3d', '.ress', '.resource', '.obb', '.bundle', '.unityexp'] + unityStreamingAssets.tokenize(', ')
        ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
    }

    packagingOptions {
        doNotStrip '*/armeabi-v7a/*.so'
    }
}

gradle.properties

org.gradle.jvmargs=-Xmx4096M
org.gradle.parallel=true
android.enableR8=false
unityStreamingAssets=.json
unityTemplateVersion=3
android.useAndroidX=true
android.enableJetifier=true

解决方法

1. 排除重复的androidx.annotation依赖

在build.gradle(module launcher)的dependencies块中,修改Firebase依赖配置,排除重复的annotation模块:

implementation('com.google.firebase:firebase-inappmessaging-display:20.2.0') {
    exclude group: 'androidx.annotation', module: 'annotation'
}

若unityLibrary模块也引入了该依赖,同样在launcher层排除:

implementation(project(':unityLibrary')) {
    exclude group: 'androidx.annotation', module: 'annotation'
}

2. 强制统一依赖版本

在项目根目录的build.gradle中添加版本强制规则,确保所有模块使用同一版本的androidx.annotation:

allprojects {
    // 保留原有配置...
    configurations.all {
        resolutionStrategy.force 'androidx.annotation:annotation:1.3.0'
    }
}

3. 删除本地重复库文件

检查unityLibrary/libs目录,若存在androidx.annotation.annotation-1.2.0.jar,直接删除该文件。

4. 更新Google Services插件

将根目录build.gradle中的Google Services插件版本更新至兼容版本:

classpath 'com.google.gms:google-services:4.4.0'

5. 清理缓存重建

执行以下操作清除缓存后重建:

  • 执行Build > Clean Project
  • 执行File > Invalidate Caches... > Invalidate and Restart
  • 删除项目根目录的.gradle和build文件夹后重新构建

内容的提问来源于stack exchange,提问作者Verc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 19:20:19