Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is release build throwing error, but not debug when buildTypes are identical? (Android gradle build types)

I'm trying to figure out why I can't get a release build to install correctly using Android Studio. This is my buildTypes block:

buildTypes {
    debug {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
    }
    release {
        shrinkResources false
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
    }
}

and these are my product flavors:

flavorDimensions "version"
productFlavors {
    free {
        applicationId "com.example.app.free"
        dimension "version"
        signingConfig signingConfigs.config
    }
    paid {
        applicationId "com.example.app.paid"
        dimension "version"
        signingConfig signingConfigs.config
    }
}

When I install the debug paid or free versions, all is fine and dandy. Nothing wrong at all. When I try to install the release versions (paid or free) I'm getting:

'Execution failed for task ':app:transformDexArchiveWithDexMergerForFreeRelease'. com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: ... Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes. Program type already present: com.google.android.youtube.player.YouTubeApiServiceUtil'

My questions is this: Why is the duplicate class exception happening only during release, when I have nothing different defined between the release and debug buildTypes?

EDIT I've solved the issue by removing the YouTubePlayer Library dependency in gradle (b/c apparently my implementation of the google YT service was causing an internal library to be created, so depending on the imported one was redundant?). This still leaves my question valid. Why did the debug work, but not the release when nothing was declared differently?

like image 976
Mr.Drew Avatar asked Feb 04 '26 14:02

Mr.Drew


1 Answers

I had the same issue when linking with another module when I added another (indirect) dependency on its assemble (it was Protobuf module which needs to generate Java files from .proto files before the app module can generate its JSON model). It seems that assembleRelease optimises the code in a way that it may generate slightly different class file depending on where it is called from and the Dex merger then cannot decide which of the class files to use. assembleDebug generates always the same (unoptimised) code which it can merge.

(For anyone having the same problem with Protobuf, the solution for that is to depend on :protobuf:GenerateProto instead of :protobuf:assemble.)

like image 171
StenSoft Avatar answered Feb 06 '26 06:02

StenSoft