Android project generating too many DEX files in release builds

General Tech Bugs & Fixes 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I'm working on a game being exported from Unity as an Android Studio project. When I assemble and run as a debug build, it works as expected. Since we are using multiple 3rd party libraries, there are more than 65K methods, and it generates quite a few DEX files (11 files). Looking in the contents of these DEX files, they are not all full. In fact, most of them contain only a single BuildConfig class, or a bunch of related R classes. In fact, only 2 of the DEX files have anything appreciable in them, classes7.dex and classes11.dex. I don't know how the app even runs; I thought the main activity needed to be in classes.dex for it to work. But in any case everything actually works fine.

However, in release builds, the situation is much, much worse. I'm talking about 109 (one hundred and nine!) DEX files. This appears to simply be much more granular separation of the classes that were originally in the 11 DEX files before, for some reason. And here, things start to break down. On launch, ClassNotFoundExceptions start appearing on some devices, but on others it works fine. The common factor I have seen indicating whether it will work is OS version. All the devices are running Android OS 5.0+, so multidexing is supported natively, but the stable devices are mostly running 6.0+.

The main activity is in classes54.dex, which extends from a class in classes30.dex, which extends from a class in classes106.dex, which extends from Activity. Those classes it can find just fine though. The first class it complains it can't find is over in classes91.dex, for example.

I assume the problem is within the gradle process, since the issue occurs when exporting directly to an APK from Unity or when building within Android Studio. So my question is how do I either:

  1. convince Unity/Android Studio/Gradle to output a sensible number of DEX files, or
  2. Get all devices to look at all the dex files, even when there are 100+, when looking for classes?

Current build.gradle created when exporting from Unity:

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

allprojects {
    repositories {
        flatDir {
            dirs 'libs'
        }
        google()
    }
}

apply plugin: 'com.android.application'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation(name: 'GoogleAIDL', ext:'aar')
    implementation(name: 'GooglePlay', ext:'aar')
    implementation(name: 'android.arch.lifecycle.runtime-1.0.0', ext:'aar')
    //...
    //Also included: Google Play, Facebook, Crashlytics, AdMob, Firebase, and more, redacted for convenience
    //...
    implementation project(':Firebase')
    implementation project(':GoogleMobileAdsIronSourceMediation')
    implementation project(':GoogleMobileAdsMediationTestSuite')
    implementation project(':GoogleMobileAdsPlugin')
    implementation project(':GoogleMobileAdsTapjoyMediation')
    implementation project(':GooglePlayGamesManifest.plugin')
    implementation project(':unity-android-resources')
}

android {
    compileSdkVersion 27
    buildToolsVersion '28.0.3'

    defaultConfig {
        targetSdkVersion 27
        applicationId 'redacted'
        multiDexEnabled true
        ndk {
            abiFilters 'armeabi-v7a'
        }
        versionCode 0
        versionName '1.0.8'
    }

    dexOptions {
        incremental true
            javaMaxHeapSize "4g"
    }    

    lintOptions {
        abortOnError false
    }

    aaptOptions {
        noCompress '.unity3d', '.ress', '.resource', '.obb', 'crashlytics-build.properties', 'google-services-desktop.json', 'someotherfiles'
    }

    signingConfigs {
        release {
            storeFile file('/path/to/key.keystore')
            storePassword 'redacted'
            keyAlias 'key'
            keyPassword 'redacted'
        }
    }

    buildTypes {
        debug {
            minifyEnabled false
            useProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'
            jniDebuggable true
            //Explicitly sign with release key anyway
            signingConfig signingConfigs.release
        }
        release {
            minifyEnabled false
            useProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'
            signingConfig signingConfigs.release
        }
    
                                                
                                                
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.