I recently extracted some code from my Android application project into separate kotlin modules (the build.gradle files declare the "java-library" and "kotlin" plugins).
Now, the task ':app:minifyQaWithR8' is failing with the message:
AGPBI: {"kind":"error","text":"Type com.myapp.ext.models.AckResponse is defined multiple times: E:\projects\myapp\ext\build\.transforms\35656f2face08400c6d53844207373f0\jetified-ext.jar:com/myapp/ext/models/AckResponse.class, E:\projects\myapp\app\build\tmp\kotlin-classes\qa\com\myapp\ext\models\AckResponse.class"}],"tool":"R8"}
I tried deleting each module's build folder, then invaldate cache/restart, then assemble, and got a similar result with a different class. But both times, the locations were the same: one was in .transforms\35656f2face08400c6d53844207373f0\jetified-ext.jar and one in app\build\tmp\kotlin-classes\qa
In a similar question a member of the R8 team suggests that one of these locations represents a dependency, and one represents the app code, however, I cannot find any instance of the class in question in my application code, nor any indication that my module is being imported more than once.
It may be relevant that two of my modules do have a lot of the same classes, however I'm using the following statement to only include one of them in the build:
if(api_version == "ext2") {
implementation project(":ext2")
}else{
implementation project(":ext1")
}
The packages in these modules do not appear in the main application code.
What other steps can I take to track down the root of this issue?
I encountered the same issue in my Android modular project when building the app in the release variant after updating the Gradle version. The error message, "R8 says type is defined multiple times," pointed to a conflict in the build process, particularly in the build.transforms and build\tmp\kotlin-classes directories.
Root Cause:
This issue usually occurs when the R8 code shrinker is enabled (through ProGuard) in both the app module and library modules. The conflict arises because each module tries to apply code shrinking independently, leading to duplicate type definitions during the build process.
My Initial Setup:
In the build.gradle file of each library module, I had the following code:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
And in the build.gradle file of the app module, I used:
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
The minifyEnabled true flag was causing R8 to run separately in both the app and library modules, leading to the conflict.
The Fix:
To resolve this issue, I disabled code shrinking (R8) in all the library modules by updating the buildTypes section as follows:
buildTypes {
debug {
minifyEnabled false
}
release {
minifyEnabled false
}
}
By setting minifyEnabled false in both the debug and release build types for all library modules, the error was fixed. R8 now only runs in the app module, preventing duplicate type definitions.
Why This Fix Works:
Code shrinking (R8/ProGuard) should only be applied at the app level. Library modules should not apply their own ProGuard rules because the app module will handle the final shrinking and obfuscation. Enabling it in both the app and libraries can lead to conflicts like the one described.
Key Takeaways:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With