2

I'm using MultiDex in my app to support over 65k methods. Over api 21 it works well because it does a predex build. Under api 21 I have a crash in my Application with this stacktrace:

java.lang.VerifyError: mypackage/com/myapp/MyAppApplication
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1215)
            at android.app.Instrumentation.newApplication(Instrumentation.java:990)
            at android.app.Instrumentation.newApplication(Instrumentation.java:975)
            at android.app.LoadedApk.makeApplication(LoadedApk.java:502)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4537)
            at android.app.ActivityThread.access$1500(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1402)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5322)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
            at dalvik.system.NativeStart.main(Native Method)

This is my gradle file (simplified) with different flavors for compiling with predex in debug:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "mypackage.com.myapp"
        targetSdkVersion 22
        versionCode Integer.parseInt(new Date().format("yyMMddHH"))
        versionName "1.0"
        multiDexEnabled = true
    }

    productFlavors {
        normal {
            minSdkVersion 21
        }

        beta {
            minSdkVersion 16
            versionName "1.0.0_beta"
        }

        prod {
            minSdkVersion 16
        }
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

    buildTypes {
        debug {
            minifyEnabled false
            debuggable true
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:multidex:1.0.0'
}

And this is my Application:

public class MyApplication extends CustomLibraryApplication {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

And in my manifest under <application> i have android:name=".MyApplication"

Solutions like: "exclude some of google play services" or "extend MultiDexApplication" are not useful in this case.

Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70
  • "Solutions like: "exclude some of google play services" or "extend MultiDexApplication" are not useful in this case." - why is this not useful? I would suggest running Proguard to get rid of some of your excess classes. – Booger Jul 23 '15 at 12:51
  • @Booger "exclude some of google play services" because i know how to exclude them, but i want to try this multidex approach. "extend MultiDexApplication" because i'm extending a third party library now that i can't modify. I thought also to run Proguard, but this will be the last solution, before i want only to understand why this error happens, i don't really need an "alternative" solution. My question was to understand better how multidex works.. – Giorgio Antonioli Jul 23 '15 at 12:54

2 Answers2

3

SOLVED

The problem was...drumroll...the buildToolsVersion!

I changed the buildToolsVersion in my gradle (only the buildToolsVersion) from 22.0.1 to 23 rc3 (the last one from preview channel) and it worked well with api < 21 with multidex.

Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70
  • This didn't work for me, am using buildToolsVersion 28, I tried downgrading to 23 rc but still didn't work, which version should I use. – Samuel Owino Jan 24 '19 at 13:07
1

The other problem might be when you use some library exceptions (for me it was KeyPermanentlyInvalidated Exception) and it only appeared with api <21. As soon as I changed it to simple Exception, everything was fine. This helped me perfectly https://stackoverflow.com/a/16060251/12258664