升级Gradle 8后minifyReleaseWithR8出现BuildConfig类重复定义问题
Gradle 8升级后minifyReleaseWithR8报错:BuildConfig重复定义
报错信息
Type com.my.package.BuildConfig is defined multiple times: C:\Users\MyProject\build\firebase_messaging\intermediates\runtime_library_classes_jar\release\classes.jar:com/my/package/BuildConfig.class, C:\Users\MyProject\firebase_core\intermediates\runtime_library_classes_jar\release\classes.jar:com/my/package/BuildConfig.class
项目配置
根目录build.gradle
buildscript { ext.kotlin_version = '1.8.22' repositories { google() mavenCentral() maven { url "https://plugins.gradle.org/m2/" } } dependencies { classpath 'com.android.tools.build:gradle:8.1.4' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.4.0' classpath "com.jeff-media:fix-javadoc-plugin:1.19" } } allprojects { repositories { google() mavenCentral() } } subprojects{ afterEvaluate { android { namespace 'com.my.package' } } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" project.evaluationDependsOn(':app') } tasks.register("clean", Delete) { delete rootProject.buildDir } apply plugin: "com.jeff-media.fix-javadoc-plugin"
App模块build.gradle
def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" def keystoreProperties = new Properties() def keystorePropertiesFile = rootProject.file('key.properties') if (keystorePropertiesFile.exists()) { keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) } android { compileSdk 33 sourceSets { main.java.srcDirs = ['src/main/kotlin', 'build/generated/source/buildConfig/release', 'build/generated/source/buildConfig/debug'] } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } defaultConfig { applicationId 'com.my.package' minSdkVersion 23 targetSdkVersion 33 versionCode flutterVersionCode.toInteger() versionName flutterVersionName multiDexEnabled true } signingConfigs { release { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null storePassword keystoreProperties['storePassword'] } } buildTypes { release { signingConfig signingConfigs.release minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' ndk { abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' } } } lint { disable 'InvalidPackage' } namespace 'com.my.package' } flutter { source '../..' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.google.firebase:firebase-messaging:23.3.1' implementation 'androidx.multidex:multidex:2.0.1' } apply plugin: 'com.google.gms.google-services'
Flutter环境信息
[√] Flutter (Channel stable, 3.16.0, on Microsoft Windows [Version 10.0.22621.2715], locale en-US) • Flutter version 3.16.0 on channel stable at C:\Development\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision db7ef5bf9f (9 days ago), 2023-11-15 11:25:44 -0800 • Engine revision 74d16627b9 • Dart version 3.2.0 • DevTools version 2.28.2 [√] Windows Version (Installed version of Windows is version 10 or higher) [√] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at C:\Users\MSM\AppData\Local\Android\sdk • Platform android-34, build-tools 34.0.0 • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231) • All Android licenses accepted. [√] Chrome - develop for the web [√] VS Code (version 1.79.2) • VS Code at C:\Users\MSM\AppData\Local\Programs\Microsoft VS Code • Flutter extension version 3.76.0 [√] Connected device (3 available) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22621.2715] • Chrome (web) • chrome • web-javascript • Google Chrome 119.0.6045.124 • Edge (web) • edge • web-javascript • Microsoft Edge 119.0.2151.72 [√] Network resources • All expected network resources are available. • No issues found!
已尝试的无效方案
- 降级至Gradle7,但出现更多问题,不得不回退到Gradle8
- 清理/重新构建项目
- 无效缓存并重启
- 删除项目及根目录下的.build、.gradle文件夹
解决方案
问题根源在于根目录build.gradle中给所有子模块强制设置了相同的namespace 'com.my.package',包括firebase_messaging、firebase_core这些第三方依赖模块,导致它们生成了与主App模块相同包名的BuildConfig类,在R8混淆时冲突。
修改根目录的subprojects配置,只给你的App模块设置namespace,排除第三方子模块:
subprojects{ afterEvaluate { // 只给app模块设置namespace,跳过firebase等第三方子模块 if (project.name == 'app') { android { namespace 'com.my.package' } } } }
或者更灵活地排除所有非自定义的子模块:
subprojects{ afterEvaluate { // 仅当项目是本地模块而非第三方依赖时设置namespace if (file("src/main").exists()) { android { namespace 'com.my.package' } } } }
修改后,执行clean再重新构建项目即可解决BuildConfig重复定义的问题。
内容的提问来源于stack exchange,提问作者Gombo Khorloo
相关产品推荐
相关产品推荐

