Search Unity

Enable Multidex Support for Unity 2020.

Discussion in 'Android' started by Mr-Mechanical, Oct 19, 2020.

  1. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Hi,

    I'm looking for guidance on how to enable Multidex with Unity 2020. I'm using Mopub. Thank you so much for any help.
     
  2. Nomadjackalope

    Nomadjackalope

    Joined:
    Jul 12, 2016
    Posts:
    16
    I can't get it to work. I'm getting "Could not find com.androidx.multidex:multidex:2.0.1"

    But I have read that you need to enable and modifythe following in Edit>Project Settings>Player>Publishing Settings
    Custom Main Manifest
    Custom Launcher Gradle Template
    Custom Gradle Properties Template

    Custom Main Manifest - application line should read as follows
    Code (csharp):
    1. <application android:name="androidx.multidex.MultiDexApplication">

    Custom Launcher Gradle Template - Add multidex to dependencies and multiDexEnabled true to defaultConfig
    Code (csharp):
    1. dependencies {
    2.     implementation 'com.androidx.multidex:multidex:2.0.1'
    3.     ...
    4.     }
    5. android {
    6.     ...
    7.     defaultConfig {
    8.         ...
    9.         multiDexEnabled true
    10.         ...
    11.     }
    12. }

    Custom Gradle Properties Template - add the following below **ADDITIONAL_PROPERTIES**
    Code (csharp):
    1. **ADDITIONAL_PROPERTIES**
    2.  
    3. android.enableJetifier=true
    4. android.useAndroidX=true
    5.  
    If anyone has gotten this to work successfully please help.
     
    berksng, AlexCome, Johannski and 3 others like this.
  3. Kidsdi

    Kidsdi

    Joined:
    Jun 19, 2019
    Posts:
    1
    in Edit>Project Settings>Player>Publishing Settings enable:

    Custom Main Gradle Template
    Custom Launcher Gradle Template
    Custom Gradle Properties Template


    But edit only Custom Launcher Gradle Template.This video helps me.
    You should add new dependence
    implementation 'androidx.multidex:multidex:2.0.1'
    and new setting for defaultConfig
    multiDexEnabled true


    So my Custom Launcher Gradle Template now looks

    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 project(':unityLibrary')
    7.     implementation 'androidx.multidex:multidex:2.0.1'
    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.     multiDexEnabled true
    21.         minSdkVersion **MINSDKVERSION**
    22.         targetSdkVersion **TARGETSDKVERSION**
    23.         applicationId '**APPLICATIONID**'
    24.         ndk {
    25.             abiFilters **ABIFILTERS**
    26.         }
    27.         versionCode **VERSIONCODE**
    28.         versionName '**VERSIONNAME**'
    29.     }
    30.  
    31.     aaptOptions {
    32.         noCompress = ['.ress', '.resource', '.obb'] + unityStreamingAssets.tokenize(', ')
    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.             proguardFiles getDefaultProguardFile('proguard-android.txt')**SIGNCONFIG**
    44.             jniDebuggable true
    45.         }
    46.         release {
    47.             minifyEnabled **MINIFY_RELEASE**
    48.             proguardFiles getDefaultProguardFile('proguard-android.txt')**SIGNCONFIG**
    49.         }
    50.     }**PACKAGING_OPTIONS****SPLITS**
    51. **BUILT_APK_LOCATION**
    52.     bundle {
    53.         language {
    54.             enableSplit = false
    55.         }
    56.         density {
    57.             enableSplit = false
    58.         }
    59.         abi {
    60.             enableSplit = true
    61.         }
    62.     }
    63. }**SPLITS_VERSION_CODE****LAUNCHER_SOURCE_BUILD_SETUP**
    64.  
     
  4. afedulov

    afedulov

    Joined:
    Aug 6, 2019
    Posts:
    4
    Hi. I've implemented steps mentioned in this thread, but got an error:
    Code (CSharp):
    1. java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mycompany.myapp/androidx.multidex.MultiDexApplication}: java.lang.ClassCastException: androidx.multidex.MultiDexApplication cannot be cast to android.app.Activity
    and application crash immediately. A possible solution is to build android project by unity and edit it: inherit MyApplication from MultiDexApplication, but I hope this is unnecessary. Any ideas? Is it related specially to androidx?
     
    aaron-PlayQ likes this.
  5. 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.



    check these items in player settings,


    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
     
    AlexCome likes this.
  6. jokigenki

    jokigenki

    Joined:
    Jul 19, 2014
    Posts:
    41
    As of 2022/01/20, Unity 2020.3.26f1 this is what worked for me:
    • Go into Menu -> Assets/Play Services Resolver/Android Resolver/Settings and check Enable Auto-Resolution and Prompt Before Auto-Resolution
    • Open Project Settings and check:
      • Custom Launcher Gradle Template
      • Custom Gradle Properties Template
    • Edit Plugins/Android/launcherTemplate.gradle and add
      • implementation 'androidx.multidex:multidex:2.0.1' in dependencies {}
      • multiDexEnabled true in android { defaultConfig {} }
    • Edit Plugins/Android/gradleTemplate.properties and add
      • android.useAndroidX=true
    • The Android Resolver should kick off and resolve all the dependencies. If not, use Menu -> Assets/Play Services Resolver/Android Resolver/Resolve
     
    beechan, koreanyo, r137 and 1 other person like this.
  7. unity_suIvHUJsyi0TIw

    unity_suIvHUJsyi0TIw

    Joined:
    Oct 4, 2021
    Posts:
    3
    Not all heroes wear capes indeed! Thanks @jokigenki
    I was getting this error on logcat and the steps above resolved the issue.
    Code (CSharp):
    1. Didn't find class "android.support.multidex.MultiDexApplication" on path: DexPathList
     
  8. Starbox

    Starbox

    Joined:
    Sep 17, 2014
    Posts:
    470
    Where exactly in the launcher template should these two lines be placed?

    Same question for the gradle template, although that file is much simpler, has no structure and it looks like putting the line before or after **ADDITIONAL PROPERTIES** wouldn't make much of a difference.
     
  9. jokigenki

    jokigenki

    Joined:
    Jul 19, 2014
    Posts:
    41
    The (start of the) launchTemplate looks like this (you might need to update the JavaVersion, depending on which JDK you're using):

    Code (CSharp):
    1. dependencies {
    2.     implementation project(':unityLibrary')
    3.     implementation 'androidx.multidex:multidex:2.0.1'
    4.     }
    5.  
    6. android {
    7.     compileSdkVersion **APIVERSION**
    8.     buildToolsVersion '**BUILDTOOLS**'
    9.  
    10.     compileOptions {
    11.         sourceCompatibility JavaVersion.VERSION_1_8
    12.         targetCompatibility JavaVersion.VERSION_1_8
    13.     }
    14.  
    15.     defaultConfig {
    16.         minSdkVersion **MINSDKVERSION**
    17.         targetSdkVersion **TARGETSDKVERSION**
    18.         applicationId '**APPLICATIONID**'
    19.         ndk {
    20.             abiFilters **ABIFILTERS**
    21.         }
    22.         versionCode **VERSIONCODE**
    23.         versionName '**VERSIONNAME**'
    24.         multiDexEnabled true
    25.     }...
    The gradleTemplate looks like:
    Code (CSharp):
    1. org.gradle.jvmargs=-Xmx**JVM_HEAP_SIZE**M
    2. org.gradle.parallel=true
    3. android.enableR8=**MINIFY_WITH_R_EIGHT**
    4. android.useAndroidX=true
    5. unityStreamingAssets=.unity3d**STREAMING_ASSETS**
    6. # Android Resolver Properties Start
    7. android.useAndroidX=true
    8. android.enableJetifier=true
    9. # Android Resolver Properties End
    10. **ADDITIONAL_PROPERTIES**
     
  10. jokigenki

    jokigenki

    Joined:
    Jul 19, 2014
    Posts:
    41
  11. Starbox

    Starbox

    Joined:
    Sep 17, 2014
    Posts:
    470
    Possibly... more like "it may just fail for no reason but yeah we threw this in and... err.. good luck" kind of solution. Unsurprisingly, a suggestion by the Gradle report failed to solve the building issue.