javaandroidandroid-multidex

Android MultiDex ClassNotFoundException


Because of my large application, i have to use MultiDex to split my app for pre Lollipop devices. When debugging my app on my Nexus 4 (ICS 4.3), i get the following errors.

Why were my classes not found?

defaultConfig {
  applicationId "de.itout.bring.handsoffme"
  minSdkVersion 17
  targetSdkVersion 23
  versionCode 6
  versionName "1.2"
  multiDexEnabled true
}
buildTypes {
  release {
      //signingConfig signingConfigs.debug
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      signingConfig signingConfigs.release
  }
  debug {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  }
} 
dexOptions {
        //javaMaxHeapSize "512m"
        //preDexLibraries = false
        javaMaxHeapSize "4g"
        incremental true
    }

dependencies {
    compile project(':emoji')
    provided fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.twitter.sdk.android:twitter:1.12.0'
    //compile 'com.google.android.gms:play-services-analytics:8.3.0'
    compile 'com.crashlytics.sdk.android:crashlytics:2.5.5'
    compile 'com.google.android.gms:play-services:8.3.0'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:cardview-v7:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.github.satyan:sugar:1.4'
    compile 'com.loopj.android:android-async-http:1.4.9'
    compile 'com.path:android-priority-jobqueue:1.1.2'
    compile 'com.anjlab.android.iab.v3:library:1.0.30'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.facebook.android:facebook-android-sdk:4.9.0'
}

  public class MyApplikation extends SugarApp {
     @Override
        protected void attachBaseContext(Context base) {
            super.attachBaseContext(base);
            //MultiDex.install(getApplicationContext());
            //MultiDex.install(getBaseContext());
            MultiDex.install(this);

        }

(Edit)

<application
        android:name=".MyApplikation"
        android:allowBackup="true"
        android:icon="@mipmap/my_icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:icon" >

Proguard-rules.pro

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-dontoptimize
-verbose

   FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication: java.lang.ClassNotFoundException: Didn't find class "com.android.tools.fd.runtime.BootstrapApplication" on path: DexPathList[[zip file "/data/app/de.itout.bring.handsoffme-2.apk"],nativeLibraryDirectories=[/data/app-lib/de.itout.bring.handsoffme-2, /vendor/lib, /system/lib]]
     at android.app.LoadedApk.makeApplication(LoadedApk.java:509)
     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4417)
     at android.app.ActivityThread.access$1300(ActivityThread.java:141)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:137)
     at android.app.ActivityThread.main(ActivityThread.java:5103)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:525)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
     at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.android.tools.fd.runtime.BootstrapApplication" on path: DexPathList[[zip file "/data/app/de.itout.bring.handsoffme-2.apk"],nativeLibraryDirectories=[/data/app-lib/de.itout.bring.handsoffme-2, /vendor/lib, /system/lib]]
     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
     at android.app.Instrumentation.newApplication(Instrumentation.java:975)
     at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4417) 
     at android.app.ActivityThread.access$1300(ActivityThread.java:141) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:5103)

Solution

  • @razzledazzle: Thanks a lot. These tips are very helpful.

    After that, I´ve the next problems. In my apk-file missing classes respectively classdefinitions. SOME sugarmodels-classes was missing (NOT ALL). The little tool classyshark and the sugarlog help me! So I compare the classes and found a solution!

    For all Sugar-ORM-Users with multiDex problems. I put the "ignore"-Annotation for a debugvariable in all my sugarclasses and now they are in the dexfile. I don´t know how or why, but it works...

     @Ignore
     public boolean multiDex;
    

    Maybe there is better solution, but I don´t know... Possibly the RetentionPolicy of the Ignore-annotation do the trick.


    Next thing for possible Sugar-Orm problems could be the proguardtool. It is important to keep the classnames of your models.

    -keep class com.package.example.models.* {*;}
    

    these rule keep the complete class for proguard. I hope it help.

    Happy coding...