casidiablo/multidex

ClassNotFoundException

Closed this issue · 8 comments

I traied your method but, after a succesful build, Android give me a ClassNotFoundException:

10-01 11:35:10.897  18389-18389/it.eng.mainapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: it.eng.mainapp, PID: 18389
    java.lang.RuntimeException: Unable to instantiate application it.eng.mainapp.MainApplication: java.lang.ClassNotFoundException: Didn't find class "it.eng.mainapp.MainApplication" on path: DexPathList[[zip file "/data/app/it.eng.mainapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/it.eng.mainapp-2, /vendor/lib, /system/lib]]
            at android.app.LoadedApk.makeApplication(LoadedApk.java:507)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4301)
            at android.app.ActivityThread.access$1500(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "it.eng.mainapp.MainApplication" on path: DexPathList[[zip file "/data/app/it.eng.mainapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/it.eng.mainapp-2, /vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
            at android.app.Instrumentation.newApplication(Instrumentation.java:975)
            at android.app.LoadedApk.makeApplication(LoadedApk.java:502)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4301)
            at android.app.ActivityThread.access$1500(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

I have multiple modules in my Android Project and MainApplication extends BaseApplication that it's defined into another module (Core) and that class extends MultiDexApplication.

All modules are "android-library" (Core module too) and my app it's in "mainapp" module.

All gradle related snippets for multi-dex are in gradle.build in mainapp module.

What could be the problem here?

The reason it's that the system can't find the class into dex files... But if i try to put android.support.multidex.MultiDexApplication in my Manifest, my app start without a problem, but i really need to load some roboguice modules in a custom application class... How can i solve this problem?

Hi @redirect11! You can try specify your classes on the file that contains the classes that always stay on main dex file.
Did you tried this? https://github.com/casidiablo/multidex#whats-it-for-again.
Basically you need to enable the main-dex-file option for dx, and add your application classes (the it.eng.mainapp.MainApplication and it's super class) on the main dex file.

Yes, i read that... this is my build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 19
        versionCode 5
        versionName "0.9"
    }
    buildTypes {
        debug {
            debuggable true
            runProguard false
            //applicationIdSuffix ".debug"
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.txt'
        }
        release {
            debuggable false
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    dexOptions {
        preDexLibraries = false
    }

}

dependencies {
    compile 'com.google.android:multidex:0.1'
    compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    compile project(':Skeleton')
    compile project(':Core')
    compile project(':imageUpload')
    //    compile 'se.emilsjolander:android-flipview:1.1.0'
    compile project(':library-slidingMenu')
    compile project(':LoginAuthorization')
    compile project(':WeatherDicet')
    compile project(':AR')
    compile project(':library-photoview')
    compile 'com.viewpagerindicator:library:2.4.1@aar'
}


afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = []
        }
        dx.additionalParameters += '--multi-dex' // enable multidex

        // optional
        dx.additionalParameters += "--main-dex-list=$projectDir/class-list.txt".toString() // enable the main-dex-list
        dx.additionalParameters += '--minimal-main-dex'
    }
}

and this is my class-list.txt:

android/support/multidex/BuildConfig.class
android/support/multidex/MultiDex$V14.class
android/support/multidex/MultiDex$V19.class
android/support/multidex/MultiDex$V4.class
android/support/multidex/MultiDex.class
android/support/multidex/MultiDexApplication.class
android/support/multidex/MultiDexExtractor$1.class
android/support/multidex/MultiDexExtractor.class
android/support/multidex/ZipUtil$CentralDirectory.class
android/support/multidex/ZipUtil.class

Do you mean to put my application class in the file above?

OK... Solved XD... Thanks
Il 01/ott/2014 16:35 "Fernando Gonçalves" notifications@github.com ha
scritto:

Yes, exactly!


Reply to this email directly or view it on GitHub
#7 (comment).

I'm having the same problem and I think I'm writing my application class path in a wrong way on the file.

Assuming the Application Class is it.eng.mainapp.MainApplication: should i write:
it/eng/mainapp/MainApplication.class
under
android/support/multidex/ZipUtil.class ?

@dcampogiani yes. Keep in mind that even doing that, if MainApplication tries to load a class that is not in the main APK, then it will still fail. What's the exact error message?

@casidiablo the problem was the same as @redirect11.

I solved this downloading the new support library release yesterday and new build tools (version 21), changed compileSdkVersion, buildToolsVersion and targetSdkVersion in build.gradle

In the application class I'm overriding attachBaseContext and in the onCreate I'me moved some initialization in a Runnable.

Even if i'm using compile 'com.android.support:support-v4:21.0.+' I need to add also compile 'com.google.android:multidex:0.1' in build.gradle, otherwise it won't find Multidex class.