Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Enter the 2020.2 Beta Sweepstakes for a chance to win an Oculus Quest 2.
    Dismiss Notice

Can't build with multidex enabled

Discussion in '2019.3 Beta' started by mikapote, Nov 7, 2019.

  1. mikapote

    mikapote

    Joined:
    Oct 24, 2018
    Posts:
    28
    Hi there,

    I've been struggling for multiple days now trying to enable Multidex in order to build my project with Appodeal (which breaks the 64k methods limit) with Unity 2019.3 (betas 7 to 10). I've been through many issues, that I'm going to try to quickly describe here, as I'm sure it could spare some users some tears!

    What I've done

    1) I've created a cutsom mainTemplate.gradle file, in which I've added 'multiDexEnabled true' in the default config, and 'com.android.support:multidex:1.0.3' as a dependency.
    When doing that, I've hit the issue described in this post: "More than one file was found with OS independent path 'androidsupportmultidexversion.txt'". cc @justtime

    2) By exporting the project and using the command
    gradle androidDependencies
    , I've been able to see that my compiled project contained both 'com.android.support:multidex:1.0.3' and 'androidx.multidex:multidex:2.0.0' dependencies. I honestly don't know where the androidX version comes from... anyone has a guess?

    To fix this issue, I've pinned the multidex dependency as 'androidx.multidex:multidex:2.0.0', allowing the jar resolver to merge multidex deps properly, and I added android:name="androidx.multidex.MultiDexApplication" to the application tag in my custom AndroidManifest.xml.

    3) At this point, not more weird gradle error... except that multidex is not enabled at all, as I encounter the following issue again: "D8: Cannot fit requested classes in a single dex file.". I'm back to the start... How is that possible? I can see that androidx.multidex is properly added as a dependency, I have properly set multiDexEnabled to true and edited my manifest file as described above.

    Possible hint?

    In this post @JuliusM talks about the recent changes to the build.gradle files made to get Unity working as a library. I've dig a bit into that, but I've basically no experience regarding Android development and build config. Should `multiDexEnabled true` be set in the launcher's gradle.build file instead of the libreary's gradle file? If yes, how should developers proceed without having to export their game to Android Studio?

    I've honestly lost days on this, so any help would be greatly appreciated guys! Anyone from @PavelLU 's team maybe? Thanks a lot in advance.
     
  2. JuliusM

    JuliusM

    Unity Technologies

    Joined:
    Apr 17, 2013
    Posts:
    586
    Hi,
    Officially Unity does not support multidex. Instead we suggest to use Proguard's minify option.
    I have not tested this, but my guess would be it has to be specified in both places. The only official way to modify launcher's build.gradle file is by exporting the project and doing modifications afterwards. You could modify the template in the Unity install directory, but that is not supported and it will affect all of your projects built with that Unity version.
     
  3. mikapote

    mikapote

    Joined:
    Oct 24, 2018
    Posts:
    28
    Hi Julius, thank you for your answer!

    I've been using multidex in my previous Unity games without encountering any issue: it works well since Gradle is officially implemented.

    I'm actually using Proguard minification, but Appodeal SDK is still above the 65k limit. They offer a version of their SDK that doesn't require Multidex, but according to their tech team, this will soon be deprecated.

    So I guess it's of utmost importance for them to find a way to ensure compatibility with Unity 2019.3! The easy installation of their solution is a strong selling point, so having their users modify the Unity install of export the project might be an issue... I'll send them the link to this discussion.

    Nevertheless, it seems that many users require multidex. would it be possible to imagine Unity adding a new checkbox near the "Custom gradle file" option, to automatically patch both Gradle files in order to activate Multidex?
     
  4. marcin-matczak

    marcin-matczak

    Joined:
    Sep 5, 2018
    Posts:
    6
    It's not only Appodeal SDK issue. This can be achieved with different SDKs too. Just using Firebase and Mopub is enough to require multidex.
    I hope Unity will soon fix this and bring back full multidex compatibility (having Unity Cloud Build in mind).
     
    inxidious, Garrettec and daancode like this.
  5. RobertTry

    RobertTry

    Joined:
    Jan 25, 2015
    Posts:
    8
    I have same problem. I need to use multidex. just add Facebook, Firebase, and some mediation SDK and you are our of limits. Please fix it. Local solution is not enough I can't modify files in unity install directory on UCB.
     
  6. daancode

    daancode

    Joined:
    Sep 9, 2017
    Posts:
    2
    Hello,
    can we know what is official reason why multidex isn't supported from version 2019.3? I using that feature in every project that I working on. According to official documentation https://developer.android.com/studio/build/multidex.html that is the solution for this situation?
    We should set minSdkVersion to API 21 in order to have multidex enabled by default?
     
    Novack likes this.
  7. RobertTry

    RobertTry

    Joined:
    Jan 25, 2015
    Posts:
    8
    I did try to build without multidex on Android 21 and it did build :)
     
    Novack and nicolasgramlich like this.
  8. mikapote

    mikapote

    Joined:
    Oct 24, 2018
    Posts:
    28
    Yes I managed to have it working with a minimum Android version >= 21. Impossible to have it work under v21.
     
    Novack likes this.
  9. Seanm07

    Seanm07

    Joined:
    Mar 18, 2014
    Posts:
    5
    https://stackoverflow.com/questions/26609734/how-to-enable-multidexing-with-the-new-android-multidex-support-library

    Pretty much using any plugins alongside a few of the Firebase plugins quickly hits the dex limit. Just dropping support for devices running versions of the SDK lower than 21 isn't really a good option either.

    If Unity can't fix multidex support directly then a good potential workaround could be for them to let us switch between gradle building as an library and as an application, as 2019.2 and lower build as applications which allow implementation of multidex.

    I'm talking about
    apply plugin: 'com.android.application'
    in the gradle mainTemplate which is used in 2019.2 and lower vs
    apply plugin 'com.android.library'
    which is forced to be used in 2019.3 and higher.

    If alternatively anyone knows how to possibly build multidex for an android library to support android APIs lower than 21 that would also be great and solve everything too ;).
     
  10. tduriga

    tduriga

    Joined:
    Dec 9, 2015
    Posts:
    6
    Hi, I managed to get it work even on lower minApi version (mine is set to 19) with Unity 2019.3.

    You just need to edit the "launcherTemplate.gradle" file that is located at your Unity install location inside "Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates". Just add the multiDexEnabled true and implementation 'com.android.support:multidex:1.0.1' here and it should work. Also you probably need to open your text editor as admin otherwise the changes won't be saved.
     
  11. Seanm07

    Seanm07

    Joined:
    Mar 18, 2014
    Posts:
    5
    Sadly this doesn't seem to work either, from my testing the builds seemed to BUILD fine but launching the app on any devices running android 5.0 or lower would just log a bunch of class not found errors in logcat and plugins would fail to initialise or work correctly. (And running the app on android 5.0 or later would work fine)

    Let me know if your builds do actually run on devices lower than android 5 though :eek:
     
  12. tduriga

    tduriga

    Joined:
    Dec 9, 2015
    Posts:
    6
    I don't have any Android 4.4.x device, but I tried in in emulator (Nox with Android version 4.4.2) and the game launched without any problems. Do you use proguard? Maybe it's stripping some classes needed for the android 4.4
     
  13. Seanm07

    Seanm07

    Joined:
    Mar 18, 2014
    Posts:
    5
    No, we had all stripping disabled.

    The game may launch without any problems but are your plugins working correctly, e.g admob ads showing, firebase functions working correctly etc (depending what plugins you're using) (and are you actually using any plugins? It'll probably work fine multidexing a project without any plugins)

    Are you seeing any class not found errors in logcat?

    At the time of testing we had a fresh android project setup with the latest version of the full firebase suite and latest admob github plugin. I may need to come back and revisit this if you have it working, maybe the later versions of 2019.3 changed something? - However I'm currently in the middle of a project and don't have time to mess with upgrading and fixing multidex right now.
     
  14. vizgl

    vizgl

    Joined:
    Nov 4, 2014
    Posts:
    34
    Unity 2019.3.4f1 still can't build my game.
    P.S. multidex enabled and worked on 2019.2.21f1
     
  15. bogdan_ag

    bogdan_ag

    Joined:
    May 14, 2016
    Posts:
    2
    Having an option in Editor should be the correct fix but until then, as JuliusM said you can add multidexEnabled to the exported gradle file using an IPostGenerateGradleAndroidProject script.
    OneSignal SDK also had problems with this gradle template change in 2019.3 ( https://github.com/OneSignal/OneSig...WFzsFhKkzNrqwpDqGyMhjA8cdjc2oJlLKUDymKzZdulxo )
    You can use their solution and add code to change the "launcher/build.gradle" and add "\nmultiDexEnabled true\n" in it.
     
    Thaina likes this.
  16. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    438
    In conclusion

    MultiDex need to be enabled in launcher/build.gradle. But unity since 2019.3 has a bug that not allow us to override launcherTemplate.gradle. That bug was fixed in 2020.1.0b2 (yesterday) and not even backported into 2019.3.6 (today) yet

    https://unity3d.com/unity/beta/2020.1.0b2
     
    leni8ec likes this.
  17. Grinch_GEO

    Grinch_GEO

    Joined:
    Apr 19, 2014
    Posts:
    92
    i think no one is testing new build of unity for android and iOS. Its disaster, for android i am using 2019.2.x and for iOS 2019.3.x. Cmon guys do like its was before one Engine to rule them all !
     
  18. N1kel

    N1kel

    Joined:
    Jun 27, 2013
    Posts:
    5
    For anyone looking for solution.
    You have to move your
    implementation 'androidx.multidex:multidex:2.0.1'
    and
    multiDexEnabled true
    from mainTemplate.gradle to launcherTemplate.gradle and it should work.

    You should activate these files first here in the Project Setting->Player->Publishing Settings
    upload_2020-4-10_13-22-21.png
     
  19. jinsoolee

    jinsoolee

    Joined:
    Sep 19, 2015
    Posts:
    1
    Great!

    I worked out it! Thank you.
     
    DungDajHjep likes this.
  20. bekici

    bekici

    Joined:
    Nov 1, 2018
    Posts:
    5
    Hi @N1kel could you please share an example launcherGradle.template?
     
  21. N1kel

    N1kel

    Joined:
    Jun 27, 2013
    Posts:
    5
    @bekici, sure, this is just the default one with multidex thing added to it:

    Code (CSharp):
    1. // GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
    2.  
    3. apply plugin: 'com.android.application'
    4.  
    5. dependencies {
    6.     implementation 'androidx.multidex:multidex:2.0.1'
    7.     implementation project(':unityLibrary')
    8.     }
    9.  
    10. android {
    11.     compileSdkVersion **APIVERSION**
    12.     buildToolsVersion '**BUILDTOOLS**'
    13.  
    14.     compileOptions {
    15.         sourceCompatibility JavaVersion.VERSION_1_8
    16.         targetCompatibility JavaVersion.VERSION_1_8
    17.     }
    18.  
    19.     defaultConfig {
    20.         minSdkVersion **MINSDKVERSION**
    21.         targetSdkVersion **TARGETSDKVERSION**
    22.         multiDexEnabled true
    23.         applicationId '**APPLICATIONID**'
    24.         ndk {
    25.             abiFilters **ABIFILTERS**
    26.         }
    27.         versionCode **VERSIONCODE**
    28.         versionName '**VERSIONNAME**'
    29.     }
    30.  
    31.     aaptOptions {
    32.         noCompress = ['.unity3d', '.ress', '.resource', '.obb'**STREAMING_ASSETS**]
    33.         ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
    34.     }**SIGN**
    35.  
    36.     lintOptions {
    37.         abortOnError false
    38.     }
    39.  
    40.     buildTypes {
    41.         debug {
    42.             minifyEnabled **MINIFY_DEBUG**
    43.             useProguard **PROGUARD_DEBUG**
    44.             proguardFiles getDefaultProguardFile('proguard-android.txt')**SIGNCONFIG**
    45.             jniDebuggable true
    46.         }
    47.         release {
    48.             minifyEnabled **MINIFY_RELEASE**
    49.             useProguard **PROGUARD_RELEASE**
    50.             proguardFiles getDefaultProguardFile('proguard-android.txt')**SIGNCONFIG**
    51.         }
    52.     }**PACKAGING_OPTIONS****SPLITS**
    53. **BUILT_APK_LOCATION**
    54.     bundle {
    55.         language {
    56.             enableSplit = false
    57.         }
    58.         density {
    59.             enableSplit = false
    60.         }
    61.         abi {
    62.             enableSplit = true
    63.         }
    64.     }
    65. }**SPLITS_VERSION_CODE****LAUNCHER_SOURCE_BUILD_SETUP**
    66.  
    If it's your first time adding a multidex then don't forget to change the name parameter in AndroidManifest.xml to:

      <application android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:icon="@drawable/app_icon" android:label="[USER=7078]@String[/USER]/app_name" android:debuggable="false" android:name="androidx.multidex.MultiDexApplication">
     
    Last edited: Apr 20, 2020
  22. mhmad2212

    mhmad2212

    Joined:
    Sep 12, 2019
    Posts:
    5
    very very very thank you!
    after 2 days (searching and try, searching and try.....)...your answer worked for me.
     
  23. phong-genix

    phong-genix

    Joined:
    Oct 28, 2015
    Posts:
    126
    This works perfectly for me. Thanks a lot!
     
  24. Nucci

    Nucci

    Joined:
    Sep 23, 2015
    Posts:
    6
    THANK U ALOT
     
  25. rainandgames

    rainandgames

    Joined:
    Aug 6, 2018
    Posts:
    5
    Thank you so much
     
unityunity