Search Unity

Error when building Android .APK

Discussion in 'Editor & General Support' started by flyinglimousine, Jan 11, 2019.

  1. flyinglimousine

    flyinglimousine

    Joined:
    Jan 9, 2019
    Posts:
    4
    Hi, I'm fairly new to unity, and just experimenting with publishing to android, I am using Unity 2017 4.17f1, JDK 1.8 and the latest Android SDK. I press build, the progress bar nearly reaches the end but then throws up this error:



    It also printed this in the console:

    CommandInvokationFailure: Gradle build failed.
    C:/Program Files/Java/jdk1.8.0_191\bin\java.exe -classpath "D:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-4.6.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx4096m" "assembleRelease"

    stderr[
    D8: Program type already present: com.unity3d.ads.mediation.IUnityAdsExtendedListener

    FAILURE: Build failed with an exception.

    * What went wrong:
    Execution failed for task ':transformDexArchiveWithExternalLibsDexMergerForRelease'.
    > com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: D:\Documents\Zoom Racing\Temp\gradleOut\build\intermediates\transforms\dexBuilder\release\0.jar, D:\Documents\Zoom Racing\Temp\gradleOut\build\intermediates\transforms\dexBuilder\release\1.jar, D:\Documents\Zoom Racing\Temp\gradleOut\build\intermediates\transforms\dexBuilder\release\2.jar, D:\Documents\Zoom Racing\Temp\gradleOut\build\intermediates\transforms\dexBuilder\release\3.jar, D:\Documents\Zoom Racing\Temp\gradleOut\build\intermediates\transforms\dexBuilder\release\4.jar, D:\Documents\Zoom Racing\Temp\gradleOut\build\intermediates\transforms\dexBuilder\release\5.jar, D:\Documents\Zoom Racing\Temp\gradleOut\build\intermediates\transforms\dexBuilder\release\6.jar, D:\Documents\Zoom Racing\Temp\gradleOut\build\intermediates\transforms\dexBuilder\release\7.jar, D:\Documents\Zoom Racing\Temp\gradleOut\build\intermediates\transforms\dexBuilder\release\8.jar, D:\Documents\Zoom Racing\Temp\gradleOut\build\intermediates\transforms\dexBuilder\release\9.jar
    Program type already present: com.unity3d.ads.mediation.IUnityAdsExtendedListener
    Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.

    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

    * Get more help at https://help.gradle.org

    BUILD FAILED in 35s
    ]
    stdout[
    Starting a Gradle Daemon (subsequent builds will be faster)
    WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
    It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
    :checkReleaseClasspath
    :preBuild UP-TO-DATE
    :preReleaseBuild
    :compileReleaseAidl NO-SOURCE
    :compileReleaseRenderscript
    :checkReleaseManifest
    :generateReleaseBuildConfig
    :prepareLintJar
    :mainApkListPersistenceRelease
    :generateReleaseResValues
    :generateReleaseResources
    :mergeReleaseResources
    :createReleaseCompatibleScreenManifests
    :processReleaseManifest
    :splitsDiscoveryTaskRelease
    :processReleaseResources
    :generateReleaseSources
    :javaPreCompileRelease
    :compileReleaseJavaWithJavac
    :compileReleaseNdk NO-SOURCE
    :compileReleaseSources
    :lintVitalRelease
    :mergeReleaseShaders
    :compileReleaseShaders
    :generateReleaseAssets
    :mergeReleaseAssets
    :transformClassesWithDexBuilderForRelease
    :transformDexArchiveWithExternalLibsDexMergerForRelease FAILED
    21 actionable tasks: 21 executed
    ]
    exit code: 1
    UnityEditor.Android.Command.WaitForProgramToRun (UnityEditor.Utils.Program p, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
    UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
    UnityEditor.Android.AndroidJavaTools.RunJava (System.String args, System.String workingdir, System.Action`1 progress, System.String error)
    UnityEditor.Android.GradleWrapper.Run (System.String workingdir, System.String task, System.Action`1 progress)
    Rethrow as GradleInvokationException: Gradle build failed
    UnityEditor.Android.GradleWrapper.Run (System.String workingdir, System.String task, System.Action`1 progress)
    UnityEditor.Android.PostProcessor.Tasks.BuildGradleProject.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context)
    UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Does this happen on a new/empty project also?
     
  3. flyinglimousine

    flyinglimousine

    Joined:
    Jan 9, 2019
    Posts:
    4
    Interesting, just tested it and it works fine with an empty project.. I believe it could be something to do with duplicate libraries/assets being imported to my project or something like that? (may have got the word wrong there)
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  5. flyinglimousine

    flyinglimousine

    Joined:
    Jan 9, 2019
    Posts:
    4
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Correct. We'll need specific steps to reproduce.
     
  7. flyinglimousine

    flyinglimousine

    Joined:
    Jan 9, 2019
    Posts:
    4
    Sorry, I'm not quite sure what you mean.
     
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please provide directions how we might see the same behavior in a new project. It doesn't happen in a brand new project, but happens in the larger project. Provide steps to reproduce what you are seeing, starting with an new/empty project.
     
  9. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    @flyinglimousine from your error message, i believe this is the root cause:
    So, it appears that you may have duplicate libraries related to Unity Ads.

    This is a typical error that occurs in case you included the Unity ads libraries yourself, but then added them once again through the "Services" tab in the editor.

    Try to see if you have any files named similarly to UnityAds (with .jar or .aar extension) in your project - remove those and then try to re-import it through the "Services" tab. then try to rebuild and see if it works :)

    Let me know if you need any further assistance.
     
    ranath, srodrigodev, ghedo and 2 others like this.
  10. srodrigodev

    srodrigodev

    Joined:
    Oct 15, 2018
    Posts:
    15
    I just had the exact same problem, and the cause is what @liortal mentions.
     
  11. Haze74

    Haze74

    Joined:
    Jul 28, 2015
    Posts:
    11
    Same problem here @liortal and @srodrigodev ...

    I imported the ads support through the asset store (Unity monetization 3.03) and I also switched on Unity ads in the services tab. (Do I understand you correctly above when you say that this can/will cause duplication of libraries?)

    I get this error message:

    . com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: ...........

    Followed @liortal s advice and found one file UnityAds.jar (but not any file UnityAds.aar) and tried to remove it. That did not fix the issue though...

    Most grateful for any help here!
     
  12. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  13. Haze74

    Haze74

    Joined:
    Jul 28, 2015
    Posts:
    11
    Thanks @JeffDUnity3D.

    To be honest I do not know if I'm even diagnosing the problem correctly at the moment. I'm looking at the threads at the UnityAds forum and nothing seems really to be my problem.

    Here is some more error message detail:

    From popup:

    "com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:...."

    From console:

    "CommandInvokationFailure: Gradle build failed.
    /Applications/Unity/PlaybackEngines/AndroidPlayer/Tools/OpenJDK/MacOS/bin/java -classpath "/Applications/Unity/PlaybackEngines/AndroidPlayer/Tools/gradle/lib/gradle-launcher-4.6.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx4096m" "assembleRelease"

    stderr[
    D8: Program type already present: android.support.annotation.AnyThread
    ..........."

    And further down

    ":transformDexArchiveWithExternalLibsDexMergerForRelease FAILED"


    Do these messages indicate that this is a problem with UnityAds? Or is it something else?

    Thanks!
     
  14. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  15. JazzCreateGames1

    JazzCreateGames1

    Joined:
    Sep 24, 2014
    Posts:
    7
    I had this issue in my project, I just resolved it, for me it was because there was 2 folders called plugins assioted to play games service folders, turns out both contained xml manifest. I deleted the plugin folder that was related to the old play games service that I had tried remove
     
  16. ranath

    ranath

    Joined:
    Dec 25, 2014
    Posts:
    1
    Fixed.. Thanks Lot Mate..
    one more beside this matter [ Unity Ads vs Ad mob what u prefer?]
     
  17. ucaduque

    ucaduque

    Joined:
    Oct 26, 2018
    Posts:
    3
  18. Ebonicus

    Ebonicus

    Joined:
    Oct 31, 2016
    Posts:
    158
    That allowed me to build, but app completely crashes on start now.