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.
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.
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
来源:https://stackoverflow.com/questions/31587765/java-lang-verifyerror-with-api-21