Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    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.
     
    Qbit86, Weiky, lazycoder00000 and 5 others like this.
  2. JuliusM

    JuliusM

    Unity Technologies

    Joined:
    Apr 17, 2013
    Posts:
    835
    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?
     
    Qbit86, Novack, inxidious and 2 others like this.
  4. marcin-huuuge

    marcin-huuuge

    Joined:
    Sep 5, 2018
    Posts:
    15
    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.
     
    inxidious, daancode and marcin-huuuge like this.
  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?
     
    umarhyatt and Novack like this.
  7. RobertTry

    RobertTry

    Joined:
    Jan 25, 2015
    Posts:
    8
    I did try to build without multidex on Android 21 and it did build :)
     
    nabergh, 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.
     
    nabergh and Novack like this.
  9. Seanm07

    Seanm07

    Joined:
    Mar 18, 2014
    Posts:
    12
    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:
    52
    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.
     
    bennetpe, Brathnann, VenoliS and 2 others like this.
  11. Seanm07

    Seanm07

    Joined:
    Mar 18, 2014
    Posts:
    12
    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:
    52
    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:
    12
    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:
    61
    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:
    1,166
    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. Grinchi

    Grinchi

    Joined:
    Apr 19, 2014
    Posts:
    130
    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:
    14
    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.
     
    Riovox likes this.
  23. p0w1nd

    p0w1nd

    Joined:
    Oct 28, 2015
    Posts:
    140
    This works perfectly for me. Thanks a lot!
     
    Riovox and kevinatgame like this.
  24. Nucci

    Nucci

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

    rainandgames

    Joined:
    Aug 6, 2018
    Posts:
    12
    Thank you so much
     
  26. The_MrX_

    The_MrX_

    Joined:
    Aug 25, 2019
    Posts:
    12
    Using 2020.1.13f1 I also needed to add
    android.useAndroidX=true in gradleTemplate.properties file.
    But it worked at that point and didn't crash!

    Thank you.

    Also not sure what you mean needs to changed in the android manifest. But it seems to work without adjusting it.

    Edit. this google implementation(in mainTemplate) was causing crashing for me during startup, incase this information is useful to anyone in future.
    Code (CSharp):
    1. implementation 'com.google.android.gms:play-services-ads:19.4.0'
    for context I am working on appodeal integration.
     
    Last edited: Nov 18, 2020
  27. Wraiter

    Wraiter

    Joined:
    Dec 23, 2018
    Posts:
    1
    Your solution helped with compilation. But now the game crashes right away.
    Trying to get Appodeal to work
     
  28. SrNull

    SrNull

    Joined:
    Jun 9, 2011
    Posts:
    25
    Bro, I Love you, thank you so much !!!
    this helped me when I tried to add Applovin and Chartboost Plugins in the same project, geez what a nightmare,
     
  29. seidgaparov

    seidgaparov

    Joined:
    Jul 18, 2013
    Posts:
    18
    I spent all day searching for this solution:confused:, thank you very much:)
     
  30. mrAntoha

    mrAntoha

    Joined:
    Jun 27, 2015
    Posts:
    2
    Bless you, kind man
     
  31. Sparkling6

    Sparkling6

    Joined:
    Jun 1, 2017
    Posts:
    1
    This man right here is a life saver!
    You have my thanks and I'm sure, many others who encounter this issue
     
  32. glezmen_unity

    glezmen_unity

    Joined:
    Feb 15, 2021
    Posts:
    1
    Holy crap, but simply chaning the minimum Android API to 21 I'm finally able to build the APK, after spending the larger part of the day with trying...

    Thank you!
     
  33. thantieuhodo91

    thantieuhodo91

    Joined:
    Dec 3, 2014
    Posts:
    9
    I am sorry but at unity 2020.3.18, I have a problem with com.androidx.multidex:multidex:2.0.1

    Could not determine the dependencies of task ':launcher:compileReleaseJavaWithJavac'.
    > Could not resolve all task dependencies for configuration ':launcher:releaseCompileClasspath'.
    > Could not find com.androidx.multidex:multidex:2.0.1.
    Required by:
    project :launcher


    Thank you so much for any help!
     
  34. p0w1nd

    p0w1nd

    Joined:
    Oct 28, 2015
    Posts:
    140
    Your problem may be related to Android "Minimum API Level" setting in your Unity Project Settings.
    The current setting may be Android 4.4 ('Kit Kat') (API Level 19). Try changing it to Android 5.0 'Lollipop' (API Level 21).
     
    thantieuhodo91 likes this.
  35. thantieuhodo91

    thantieuhodo91

    Joined:
    Dec 3, 2014
    Posts:
    9
    I found the problem!
    It should have been 'androidx.multidex:multidex:2.0.1' but I wrote it as 'com.androidx.multidex:multidex:2.0.1'.
    So sorry about that!
     
  36. aaron-PlayQ

    aaron-PlayQ

    Joined:
    Mar 27, 2015
    Posts:
    19
    After following these steps, my project builds but I get this exception when I try to launch it on device:
    Code (CSharp):
    1. java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{net.company.myapp/androidx.multidex.MultiDexApplication}: java.lang.ClassCastException: androidx.multidex.MultiDexApplication cannot be cast to android.app.Activity
    Any ideas?
    EDIT: Discovered my problem, I put the "android:name="androidx.multidex.MultiDexApplication" in the activity section of the AndroidManifest instead of the application section.
     
    Last edited: Sep 22, 2021
  37. NatsuD_123

    NatsuD_123

    Joined:
    Feb 19, 2019
    Posts:
    18
    Hi,
    if you people are still having problems regarding multidex, i figured it out . \

    I was
    Using
    Unity version: 2020.3.20f1
    AppsFlyer version: 6.4.0
    Facbook SDK version : 11.0.0
    EDM4U version: 1.2.167
    Unified native admob sdk
    ,
    having jetifier enabled in EDM4U and
    dependencies {
    implementation 'com.androidx.multidex:multidex:2.0.1'
    ...
    }
    android {
    ...
    defaultConfig {
    ...
    multiDexEnabled true
    ...
    }
    }
    in launcher gradle file and
    # Android Resolver Properties Start
    android.useAndroidX=true
    android.enableJetifier=true
    # Android Resolver Properties End
    in gradle properties file , after force resolving dependencies, regenerating android manifest, still getting gradle failed error
    > Could not find com.androidx.multidex:multidex:2.0.1. Required by: project :launcher unity

    basically i was opting to use multidex because of 64k limit error. Even if i was managing to build using multidex, i had missing class errors in logcat of admob/facebook

    the solution is quite simple.

    upload_2021-10-27_11-36-28.png

    check these items in player settings,

    upload_2021-10-27_11-38-43.png
    use the settings for EDM4U

    and just dont add multi dex code i.e.
    dependencies {
    implementation 'com.androidx.multidex:multidex:2.0.1'
    ...
    }
    android {
    ...
    defaultConfig {
    ...
    multiDexEnabled true
    ...
    }
    }

    in launcher gradle. just go for build, test your sdks and it will work
     
    Thaina likes this.
  38. bennetpe

    bennetpe

    Joined:
    Jul 23, 2019
    Posts:
    5
    Worked for me thanks!