Android Project - Multiple dex files define Lcom/android/vending/billing/BuildConfig

I have been trying to export an Google Android Project from Unity so I can integrate an external library with Gradle.

I am using :

  • Unity 5.4 Beta 18 OSX
  • Android Studio 2.1.1

I had first attempted to use Unity 5.3.4 & 5.3.5 but it was crashing on launch in what appeared to be an issue with the Unity IAP export when building a Google Android Project. A bit of research lead me to believe that the issue was resolved in the latest 5.4 Beta, however now I am getting a Multiple define error when building.

Something signigant has changed as now I have a googleAIDL, googlePlay, and unity adsrelease folders.

  • cupcakeTest
  • common
  • googleAIDL
  • googlePlay
  • unityadsrelease
  • unityandroidresources
  • Gradle Scripts

The error the gradle build is failing on:

Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Lcom/android/vending/billing/BuildConfig;

Error:Execution failed for task ‘:cupcakeDigital:transformClassesWithDexForDebug’.

com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java’’ finished with non-zero exit value 2

When trying to remove this particular class it just moved onto the next one and repeat the error again. Is this a Unity issue with IAP and Ads or could this be something specific with my project?

If I build just to an APK from Unity it works fine but then i cannot implement the external SDK. I can also export a test scene without Ads or IAP enabled and it does work in Project. I will try and enable those next to see if it is specifically these addons.

After a few more tests on a fresh project I have confirmed that once you turn on Unity IAP in the services panel this error will occur in your google android project. These last few tests were with Unity 5.3.5p1 and 5.4.0b20.

I will file a bug and in the meantime try and find a workaround.

Bug Filed https://fogbugz.unity3d.com/default.asp?803400_q536ombn69giejor

1 Like

I’m not using Unity IAP, I get this
Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;

Hey Sebrofjr, did you ever find a workaround?
I am having this exact issue :frowning:

Thanks
Andrew

Hi, guys! Is there any solution? Thanks
I can confirm, it’s not generated by Unity IAP :frowning:

Hi,
I’m also having similar issue. not able to make apk file from Android Studio.

I’m using

  • Unity 5.3.5p7 with Unity IAP
  • Gradle 2.14.1
  • Android Studio V_2.2.2

Help would be appreciated.
Thanks

As a temporary workaround, you may delete the BuildConfig.class from within the .jar in Unity IAP plugin.

1 Like

@Yury-Habets , is it really happening from Unity IAP? if I delete BuildConfig.class,what impact will it have on IAP purchases?

Thanks,
Tanveer.

@tanveerm , that would have actually no impact. This class is auto-generated and contains information about the configuration(tools versions) of the build.
@Yury-Habets , I’m not using Unity IAP. But, I have: Facebook, OpenIAB, Fyber, Helpshift, NativeLibrary and a couple more plugins in the project.

The temporary solution for me(as I really need to have all my plugins in the project) was to remove BuildConfig class from the .jars with the help of WinRar.

This worked for me too, thank you! I just searched the whole project structure exported from Unity for “*.jar” and then just searched for BuildConfig class in every .jar file and deleted every occurence. Then I imported and built my project in AndroidStudio, and it worked, no more exceptions.

Any normal solution for this issue?

I asked the developer for an update.

2 Likes

@Qbit88 @andyisbonza @tanveerm We suspect the Unity 5.3-era Exporter’s transformation of the Java archives (AARs) made it more likely to see a duplicate symbol build problem later in this case’s build pipeline.

We’re not planning on stripping the BuildConfig classes, currently: Unity IAP does include a BuildConfig.class in some of its AARs and another copy seems to have been injected somewhere in the 5.3/5.4-era “ADT” (considering retiring this soon) exporter, or possibly by a latter phase in the build toolchain after Unity. (Thanks to @Sebrofjr for the repro detail in his case.)

I believe a resolution can be found in Unity 5.5’s “Gradle export” (versus the 5.3-era “ADT export”). It could resolve this particular duplicate class vulnerability since there is no transformation of the AARs; they are kept intact as the sole source of these Java classes for the latter steps of the build chain, Android Studio in this case.

Also, the workaround of performing a kind of “ZIP surgery” on the classes.jar enclosed within the various AARs and stripping the BuildConfig.class, should also still work. I believe this would be unnecessary with Unity 5.5’s Gradle export however.

I’ve hit this issue exactly migrating from “Build System: Internal (Default)” to “Build System: Gradle (New)” (with no export at first). I have not tried “Build System: ADT (Legacy)”. Unity 5.5.4f1.

As said before, removing BluildConfig from the Unity jar solves the issue.
In my case, the conflict started after adding the Kochava SDK.
On a mac, you do it like this:

# Removing duplicated class [B]BuildClass[/B]
# List classes from JAR files
$ cd Assets
$ find . -name '*.jar' -exec grep -Hls BuildConfig {} \;
./Plugins/Android/GoogleMobileAdsPlugin/libs/unity-plugin-library.jar
./Plugins/Android/KochavaSDK.jar
# Remove class
$ zip -d ./Plugins/Android/GoogleMobileAdsPlugin/libs/unity-plugin-library.jar "*/BuildConfig.class
4 Likes

For anyone looking to solve this issue here is what I added in each Android plugin’s gradle:

task removeBuildConfig(dependsOn: "compileReleaseSources") {
    doFirst {
        file("$buildDir/intermediates/classes/release/pathToFile/BuildConfig.class").delete()
    }
}
1 Like

Did you change the pathToFile part in the path or just put it in like that?

I’m using Unity 2017.2.0f2 Gradle and still get Duplicate zip entry …BuildConfig.class

Execution failed for task ':transformClassesAndResourcesWithProguardForRelease'.
> java.io.IOException: Can't write [...\Temp\gradleOut\build\intermediates\transforms\proguard\release\jars\3\1f\main.jar] (Can't read [...\Temp\gradleOut\build\intermediates\exploded-aar\gradleOut\UnityOBBDownloader\unspecified\jars\classes.jar(;;;;;;**/*.class)] (Duplicate zip entry [classes.jar:com/unity3d/player/BuildConfig.class]))

@nicholasr Have you ever tried building Android-games with Gradle instead of legacy internal build system?