启用混淆时动态特性模块出现FragmentArgs重复定义AGPBI错误
问题
项目包含app和favorite两个模块,其中favorite是动态特性模块。使用Navigation Component时,动态特性导航图引用了app模块内的RecipeInformationFragment,正常运行无问题,但开启minifyEnabled=true(R8混淆)后,出现AGPBI错误:提示com.example.recipes.ui.detail.RecipeInformationFragmentArgs$Companion类在app和favorite模块的构建产物中重复定义。已尝试清理重建项目、Invalidate缓存、将参数重构为Parcelable并添加@Keep注解,均无法解决。
app模块build.gradle配置
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'kotlin-kapt' id 'androidx.navigation.safeargs.kotlin' id 'com.google.dagger.hilt.android' } apply from: "../shared_dependencies.gradle" android { namespace 'com.example.recipes' compileSdk 33 defaultConfig { applicationId "com.example.recipes" minSdk 21 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } debug { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } buildFeatures { viewBinding true } dynamicFeatures = [':favorite'] } dependencies { api project(":core") // Safe Args api "androidx.navigation:navigation-fragment-ktx:2.5.3" api "androidx.navigation:navigation-ui-ktx:2.5.3" api "androidx.navigation:navigation-dynamic-features-fragment:2.5.3" // UI implementation "androidx.drawerlayout:drawerlayout:1.1.1" } kapt { correctErrorTypes true }
favorite模块build.gradle配置
plugins { id 'com.android.dynamic-feature' id 'org.jetbrains.kotlin.android' id 'kotlin-kapt' id 'com.google.dagger.hilt.android' id 'androidx.navigation.safeargs.kotlin' } apply from: "../shared_dependencies.gradle" android { namespace 'com.example.recipe.favorite' compileSdk 33 buildFeatures{ viewBinding true } defaultConfig { minSdk 21 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation project(":app") implementation project(":core") }
解决方案
1. 移除动态特性模块的Safe Args插件
favorite模块依赖app模块,app已生成RecipeInformationFragmentArgs相关类,动态特性模块无需重复生成。在favorite的build.gradle中移除androidx.navigation.safeargs.kotlin插件:
plugins { id 'com.android.dynamic-feature' id 'org.jetbrains.kotlin.android' id 'kotlin-kapt' id 'com.google.dagger.hilt.android' // 移除该行 // id 'androidx.navigation.safeargs.kotlin' }
2. 添加统一混淆规则
在app模块的proguard-rules.pro中添加规则,确保RecipeInformationFragmentArgs及其内部类不被混淆或重复处理:
-keep class com.example.recipes.ui.detail.RecipeInformationFragmentArgs { *; } -keep class com.example.recipes.ui.detail.RecipeInformationFragmentArgs$Companion { *; }
3. 统一buildType的混淆配置
app的debug开启了minify,favorite未配置debug的minify,需统一配置避免构建差异:
// 在favorite模块的android.buildTypes下添加debug配置 debug { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' }
4. 彻底清理构建缓存
执行./gradlew clean命令,再通过Android Studio的File -> Invalidate Caches... -> Invalidate and Restart清理IDE缓存,最后重新构建项目。
内容的提问来源于stack exchange,提问作者Kakaranara

