R8 says type is defined multiple times: in build\.transforms and in build\tmp\kotlin-classes
Asked Answered
T

4

6

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?

Thekla answered 24/2, 2021 at 3:5 Comment(0)
T
1

As it turns out, there were in fact duplicate classes in my project. I was just unable to find them at first because the package name in the file did not correspond to the folder where the file was located.

Thekla answered 24/2, 2021 at 17:23 Comment(0)
B
4

I am having a similar issue but the duplicate dependency name is gibberish

"Type a.a is defined multiple times"

I have no idea what "a.a" is so I don't know where to start looking.

Bohannon answered 28/6 at 19:20 Comment(5)
Have the same problem. Did you find the cause?Muimuir
I believe the cause was related to having updated Gradle from 8.0.2 to 8.4.0. I reverted my project back to the last time it compiled (which included going back to Gradle 8.0.2) and purged my Gradle cache and I was able to compile. I added my code changes back in without touching the Gradle version and I was good to go.Bohannon
Reverting to Gradle also solved the problem for me. It still bugs me that random issues appear when upgrading thoMuimuir
I have the same issue, it's so annyoing that everytime you want to upgrade your gradle stuff something breaks. I assume there is no real solution yet to this problem?Inconvenient
Some more information, I checked and the last Gradle version which works for my project is 8.3.2. Every newer version would lead to the "Type a.a is defined multiple times" error without changing anything else in my project.Inconvenient
T
1

As it turns out, there were in fact duplicate classes in my project. I was just unable to find them at first because the package name in the file did not correspond to the folder where the file was located.

Thekla answered 24/2, 2021 at 17:23 Comment(0)
N
0

I was facing the same issue, but in my case, our SDK demo app was depending directly on the modules which were already being pulled from the repository, and this way I was getting duplicate classes from different sources.

Northrop answered 24/2, 2023 at 9:24 Comment(2)
then how did u fix?Griseofulvin
I just removed the duplicate dependencies. You can decide based on your case if you want to keep the module with source or built dependency.Northrop
J
0

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:

  • Avoid enabling minifyEnabled in library modules. Let the app module handle the code shrinking and resource shrinking process.
  • If you're seeing "type is defined multiple times" errors during the build, check your Gradle files to ensure you're not enabling R8 (via ProGuard) in multiple modules.
Jeroboam answered 24/10 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.