Search Unity

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

Discussion in 'Android' started by Sebrofjr, Jun 3, 2016.

  1. Sebrofjr

    Sebrofjr

    Joined:
    Oct 1, 2014
    Posts:
    24
    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.
     
  2. Sebrofjr

    Sebrofjr

    Joined:
    Oct 1, 2014
    Posts:
    24
    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.
     
  3. Sebrofjr

    Sebrofjr

    Joined:
    Oct 1, 2014
    Posts:
    24
  4. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    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;
     
  5. andyisbonza

    andyisbonza

    Joined:
    Nov 26, 2012
    Posts:
    1
    Hey Sebrofjr, did you ever find a workaround?
    I am having this exact issue :(

    Thanks
    Andrew
     
  6. oslav

    oslav

    Joined:
    Jun 2, 2015
    Posts:
    3
    Hi, guys! Is there any solution? Thanks
    I can confirm, it's not generated by Unity IAP :(
     
  7. tanveerm

    tanveerm

    Joined:
    Feb 24, 2016
    Posts:
    22
    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
     
  8. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    As a temporary workaround, you may delete the BuildConfig.class from within the .jar in Unity IAP plugin.
     
    rsodre likes this.
  9. tanveerm

    tanveerm

    Joined:
    Feb 24, 2016
    Posts:
    22
    @Yury-Habets , is it really happening from Unity IAP? if I delete BuildConfig.class,what impact will it have on IAP purchases?

    Thanks,
    Tanveer.
     
  10. oslav

    oslav

    Joined:
    Jun 2, 2015
    Posts:
    3
    @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.
     
  11. ciobanu_pf

    ciobanu_pf

    Joined:
    Dec 10, 2015
    Posts:
    5
    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.
     
  12. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Any normal solution for this issue?
     
  13. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    I asked the developer for an update.
     
    nicholasr and Qbit86 like this.
  14. nicholasr

    nicholasr

    Joined:
    Aug 15, 2015
    Posts:
    183
    @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.
     
  15. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    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.
     
  16. rsodre

    rsodre

    Joined:
    May 9, 2012
    Posts:
    229
    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:

    Code (CSharp):
    1. # Removing duplicated class [B]BuildClass[/B]
    2. # List classes from JAR files
    3. $ cd Assets
    4. $ find . -name '*.jar' -exec grep -Hls BuildConfig {} \;
    5. ./Plugins/Android/GoogleMobileAdsPlugin/libs/unity-plugin-library.jar
    6. ./Plugins/Android/KochavaSDK.jar
    7. # Remove class
    8. $ zip -d ./Plugins/Android/GoogleMobileAdsPlugin/libs/unity-plugin-library.jar "*/BuildConfig.class
     
  17. dustinandrew

    dustinandrew

    Joined:
    Apr 30, 2010
    Posts:
    102
    For anyone looking to solve this issue here is what I added in each Android plugin's gradle:

    Code (CSharp):
    1.  
    2. task removeBuildConfig(dependsOn: "compileReleaseSources") {
    3.     doFirst {
    4.         file("$buildDir/intermediates/classes/release/pathToFile/BuildConfig.class").delete()
    5.     }
    6. }
    7.  
     
    A-Zhdanov likes this.
  18. unitynewb

    unitynewb

    Joined:
    Feb 22, 2009
    Posts:
    243
    Did you change the pathToFile part in the path or just put it in like that?
     
  19. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    I'm using Unity 2017.2.0f2 Gradle and still get Duplicate zip entry ...BuildConfig.class
    Code (csharp):
    1. Execution failed for task ':transformClassesAndResourcesWithProguardForRelease'.
    2. > 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]))
     
  20. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    @nicholasr Have you ever tried building Android-games with Gradle instead of legacy internal build system?
     
  21. Jawad92

    Jawad92

    Joined:
    Jan 16, 2017
    Posts:
    3
    I faced same error in 2017.3.0f3. I simply changed build System to internal from gradle as I was not using any plugins.
    Build Settings -> build system
     
    AlexandruSpasenie and RafayAli like this.
  22. Durston88

    Durston88

    Joined:
    Mar 15, 2012
    Posts:
    110
    Has anyone been able to resolve this? Here's where I'm at:
    Code (csharp):
    1. Could not load custom rule jar file /Users/.../.../.../Temp/gradleOut/build/intermediates/exploded-aar/timber-4.5.1/jars/lint.jar
    2. java.lang.NoClassDefFoundError: com/android/tools/lint/detector/api/Detector$JavaPsiScanner
    3.    at ...
    4.    ...
    5.    ...
    6. Caused by: java.lang.ClassNotFoundException: com.android.tools.lint.detector.api.Detector$JavaPsiScanner
    7.    ...
    8.    ... 140 more
    9.  
    10. FAILURE: Build failed with an exception.
    11.  
    12. * What went wrong:
    13. Execution failed for task ':transformClassesWithJarMergingForDebug'.
    14. > com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/.../.../BuildConfig.class
    I've been digging through forum posts for a while. I've set minifyEnabled and multiDexEnabled to true, and also added the following to the standard mainTemplate.gradle file:
    Code (csharp):
    1. dexOptions {
    2.   preDexLibraries = false
    3. }
    4.  
    5. afterEvaluate {
    6.   applicationVariants.all {
    7.     variant ->
    8.       // variantName: e.g. Debug, Release
    9.       def variantName = variant.name.capitalize()
    10.       // now we tell gradle to always start lint after compile
    11.       // e.g. start lintDebug after compileDebugSources
    12.       project.tasks["compile${variantName}Sources"].doLast {
    13.       project.tasks["lint${variantName}"].execute()[/INDENT]
    14.     }
    15.   }
    16. }
    That seems to have "changed" the error, but didn't get rid of it. Previously I'd see the same "multiple dex files define so-and-so". Really getting stumped with this, but we need this working as we need to add more native plugins. Any input's appreciated. FYI, using Unity 5.6.5p1.
     
  23. AlexandruSpasenie

    AlexandruSpasenie

    Joined:
    Jun 24, 2013
    Posts:
    4
    this worked for me
     
  24. Wasplay

    Wasplay

    Joined:
    Jun 2, 2018
    Posts:
    46
    I still get this error, so I can't build using Gradle...
    When will they fix that ?
     
  25. hamza37

    hamza37

    Joined:
    Feb 5, 2017
    Posts:
    10
    I am having the same issue when i imported the Facebook sdk in unity 2018.1.0...how to find BuildConfig.class??
     
    alphadogware and orangetech like this.
  26. Sora7672

    Sora7672

    Joined:
    Jul 23, 2018
    Posts:
    1
    I opened my project and searched for the file and the path was something like:
    C:\...\PROJECTFOLDER\Temp\gradleOut\build\intermediates\classes\debug\com\COMPANYNAME\PROJECTNAME
     
  27. unity_6ktspI8YpJ2_Gg

    unity_6ktspI8YpJ2_Gg

    Joined:
    Sep 27, 2018
    Posts:
    1
    What is the Solution to the error "Cant convert to Dex"
     
  28. alphadogware

    alphadogware

    Joined:
    Nov 15, 2017
    Posts:
    76
    I am having this issue with the current Facebook SDK 7.13. Any solutions to this?

    I have tried
    1. reinstalling the SDK
    2. wiping the temp folder
    3. switch to internal build
     
    Qbit86 likes this.
  29. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    I'm having this exact same issue after adding unity IAP to services.

    Modal
    com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lcom/unity3d/ads/BuildConfig; See the Console for details.



    How do we build .apks? I'm using XR so Gradle can't be turned off
     
    Last edited: Nov 24, 2018
  30. Razorlance

    Razorlance

    Joined:
    Aug 25, 2014
    Posts:
    15
    I've also got the same issue after switching on Unity Ads on Unity 2018.2.8 and eventually getting multiDex to work (I think!), I still get this error:
    I've spent hours looking for a solution to no avail, also trying to find out if anyone knows the location of the IAP plugin .jar file to try removing the BuildConfig.class file, as mentioned from a Unity3d person above, if that's still a valid solution? but I've no idea where it's located. I've tried searching the Unity install folder but can't see anything relating to IAP.

    How is anyone using IAP in an Android project at the moment if this error completely prevents a .apk file of your project from being created?

    Edit: Ok, so I tried one last search and it turns out I had the Ads package installed and Ads turned on from the services window! I think I downloaded the package because I'd been following an tutorial that was older perhaps?
    I removed the package and the android build worked!
     
    Last edited: Dec 7, 2018
    MeteOzguz, Quorak and Qbit86 like this.
  31. doomtoo

    doomtoo

    Joined:
    Feb 16, 2015
    Posts:
    41
    It's funny/ sad that it's still an issue 4yrs later o_O

    I just added Unity Ads and Unity IAP.

    Changing from Gradle to the Internal Build still fails:

    CommandInvokationFailure: Unable to convert classes into dex format.
    ...
    translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/ads/BuildConfig;
    Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/ads/IUnityAdsListener;
    Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/ads/UnityAds;
    Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/ads/UnityAds$FinishState;
    Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/ads/UnityAds$PlacementState;
    Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/ads/UnityAds$UnityAdsError;
    Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/ads/mediation/IUnityAdsExtendedListener;
    Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/ads/metadata/InAppPurchaseMetaData;
    Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/ads/metadata/MediationMetaData;


    Similar from Gradle:

    stderr[
    Dex: Error converting bytecode to dex:
    Cause: com.android.dex.DexException: Multiple dex files define Lcom/unity3d/ads/BuildConfig;
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lcom/unity3d/ads/BuildConfig;
    ...


    Tried to fix it:

    Opening in 7zip:
    Assets\plugins\Android\UnityAds.aar\classes.jar\com\unity3d\ads\
    deleting BuildConfig.class

    Then get error:

    Cause: com.android.dex.DexException: Multiple dex files define Lcom/unity3d/ads/IUnityAdsListener;


    Then deleting that...

    Multiple dex files define Lcom/unity3d/ads/UnityAds$FinishState;


    Then deleting all classes....


    Multiple dex files define Lcom/unity3d/ads/mediation/IUnityAdsExtendedListener;


    Then finally deleting the whole Assets\plugins\Android\UnityAds.aar....

    Allows it to build, but obviously you can't use Unity Ads

    ad = Monetization.GetPlacementContent(placementId) as ShowAdPlacementContent;

    (ad is null)

    So no go on Unity Ads :/
     
  32. doomtoo

    doomtoo

    Joined:
    Feb 16, 2015
    Posts:
    41
    Found the answer from post: https://forum.unity.com/threads/ads-not-working.606127/

    Summary:

    Don't follow Unity's instructions, which are completely wrong.
    Disable Ads Under services (but leave Analytics)
    Remove Advertisement from Windows->Package Manager
    Install 'Unity Monetization 3.0' from the asset store (5th one when searching 'Monetization', non existent when searching 'ads')

    (Issue seems to be because monetization adds the unity ads aar, but so does package manager, and services might add it again, so it tries to put in 2-3versions of the library, and the only one that's valid from code is the one downloaded from the asset store)
     
    MSOTech likes this.
  33. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    I get this issue when using a custom gradle template. But it works fine when not using one.
     
    leni8ec, mahmoud93p and Qbit86 like this.