AndroidRunTime exception NoClassDefFoundError on simulator API17 but not on API22

前端 未结 1 1286
青春惊慌失措
青春惊慌失措 2021-01-16 20:25

Android Studio: 1.3.1 - Gradle Build Plugin: 1.1.2 - Gradle: 1.3.0

On Android Studio, I have an app that runs perfectly fine on Android API22 (Lollipop, on both simu

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-16 21:19

    I was able to fix my own issue.

    So essentially, as I understand it (but I don't have time to investigate further), the issue is due to the fact that the way "Multidex" is supported has changed in Android API 21 (aka Android 5.0).

    The link: https://developer.android.com/tools/building/multidex.html tells us:

    Android 5.0 and higher uses a runtime called ART which natively supports loading multiple dex files from application APK files.

    Now, it would seem that when multidexing and trying to support both "normal DEXing" (before 5.0/API21) and "ART oat files" (the new "DEX" files after 5.0/API21) in the same application you can encounter some issues similar to mine (unable to find some methods before API21 but app working fine on API21 and above).

    My app hit the 65k methods limit and I had to support from API17+.

    Anyway, the workaround was to disable multidex and use instead minifyEnabled with "Proguard", so that all unused methods are removed by Proguard and the total number of methods ends up below 65k.

    The associated code (in Gradle build file):

    defaultConfig {
    
        ...
    
        //Enabling multidex support (for more than 65k methods)
        multiDexEnabled false
    }
    
    buildTypes {
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    

    This isn't a real solution, of course, as someone who needs to effectively use 65k methods will HAVE to use multidex, but it did the job nicely for me and I can't spend more time on these issues.

    Hopefully this will help someone.

    0 讨论(0)
提交回复
热议问题