28

This seems to be one of the highest asked question in the Stack Overflow, but even after trying more than 20 solutions from 10+ questions and referring the Android Docs, my problem is not solved.

LogCat:

FATAL EXCEPTION: main
Process: com.some.app, PID: 22838
java.lang.RuntimeException: Unable to instantiate application com.some.app.utils.Application: java.lang.ClassNotFoundException: Didn't find class "com.some.app.utils.Application" on path: DexPathList[[zip file "/data/app/com.some.app-1/base.apk"],nativeLibraryDirectories=[/data/app/com.some.app-1/lib/x86_64, /vendor/lib64, /system/lib64]]
    at android.app.LoadedApk.makeApplication(LoadedApk.java:563)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4526)
    at android.app.ActivityThread.access$1500(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.some.app.utils.Application" on path: DexPathList[[zip file "/data/app/com.some.app-1/base.apk"],nativeLibraryDirectories=[/data/app/com.some.app-1/lib/x86_64, /vendor/lib64, /system/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    at android.app.Instrumentation.newApplication(Instrumentation.java:980)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:558)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4526) 
    at android.app.ActivityThread.access$1500(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    Suppressed: java.lang.ClassNotFoundException: com.some.app.utils.Application
    at java.lang.Class.classForName(Native Method)
    at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
    at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
    ... 13 more
    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

Problem
1. App is working fine in non x86 devices like Motorola, Samsung S6, Samsung S7
2. App is throwing that error on x86 architecture devices.

What I have tried till now
1. I cross checked package names in Manifest, packages, etc.
2. Gave both full and partial package name to the android:name attribute in the manifest.
3. Tried moving Application class to Main Package from utils package.

background
1. While installing Android Studio - sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 lib32bz2-1.0 - everything worked fine except lib32bz2-1.0, but had no issues till now. (Never tried installing the app on x86 devices earlier)
2. CompileSDKVersion - 25
3. BuildToolsVersion - 25.0.0
4. Gradle version - 2.2.2

Working Environment
1. Ubuntu 16.04
2. Updated JAVA 8
3. Android Studio 2.2.2

EDIT: part of gradle (app) which I think might be causing the issue

packagingOptions {
    exclude 'META-INF/NOTICE' // will not include NOTICE file
    exclude 'META-INF/LICENSE' // will not include LICENSE file
    exclude 'META-INF/notice'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license'
    exclude 'META-INF/license.txt'
}
sourceSets {
    main {
        java.srcDirs = ['src/main/java']
    }
    robolectric {
        java.srcDir file('src/test/java/')
    }
}

P.S. cross checked the Manifest several times and seems no issue with that. Couldn't find any possible cause for this anomaly in any Android docs too.

Update: After referring to this answer enabling instant run is not causing the issue. But installing the app through debug.apk is giving the same issue.

Community
  • 1
  • 1
Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
  • 1
    Do you have multidex enabled? – Sourabh Saldi Nov 11 '16 at 11:06
  • yes @AndroidFanatic, multidex is enabled, proguard is off. – Mohammed Atif Nov 11 '16 at 11:12
  • Could we see your build.gradle file? – Sergey Nikitin Nov 11 '16 at 11:13
  • Which part of build.gradle?and project module or app module? @SergeyNikitin – Mohammed Atif Nov 11 '16 at 11:25
  • flavorDimensions and productFlavors @MohammedAtif – Sergey Nikitin Nov 11 '16 at 11:28
  • I use none of the above in my gradle build. And in BuildTypes - its the same as generated by android studio. I still added the packaging and source set options as i think those might be causing the issue @SergeyNikitin – Mohammed Atif Nov 11 '16 at 11:31
  • does gradle console log anything suspicious while building your app? – nandsito Nov 11 '16 at 12:59
  • No @nandsito log shows a regular build. – Mohammed Atif Nov 11 '16 at 13:05
  • Hi, Atif can you once disable the "instant run" feature in the android studio and run your application, try it once, maybe it will help you. – Ram Koti Nov 15 '16 at 10:31
  • Hi @Ram, thanks for the suggestion. Instant run is already disabled and problem with enabling the instant run I already mentioned in the last line of my question. – Mohammed Atif Nov 15 '16 at 10:35
  • Invalidate cache of Android studio ,make sure you have a class extending `Application` in your project app module root, and print a log message in it's `onCreate` or `constructor`, then test your app in the following given sections , make sure to `build` and then `run` – Zulqurnain Jutt Nov 21 '16 at 09:27
  • @Mr.Z, `1. Invalidate Cache` - Already tried, `2. Application class in root module` - I am pretty sure that its at right class with right reference in Manifest - **App is working fine in most of the devices**, `3. Build and Run` - Tried -> cleaning, building, rebuilding, generating signed and regular APKS, almost everything. Thanks for the response but these things did not make any difference. – Mohammed Atif Nov 21 '16 at 09:39
  • are you sure you application class path mentioned in manifest is `com.some.app.Application` not `com.some.app.utils.Application` – Zulqurnain Jutt Nov 21 '16 at 09:46
  • It is `com.some.app.utils.Application` because my Application class is in utils package. P.S. I tried placing the Application class to root folder too, but same result. @Mr.Z – Mohammed Atif Nov 21 '16 at 09:55
  • Can you post your full `module:app` , build.gradle file in question ? it would be a help to analyze your problem – Zulqurnain Jutt Nov 21 '16 at 10:01
  • @Mr.Z, I have checked the build.gradle file. All the relevant data is already in question, others are dependencies and Fabric integration related codes. – Mohammed Atif Nov 21 '16 at 10:32
  • Try downgrading your gradle build version, just for kicks and giggles. – zgc7009 Nov 21 '16 at 16:03
  • Actually, isn't gradle in its 3rd major iteration at this point? – zgc7009 Nov 21 '16 at 16:26
  • Have you tried with a simple command line ./gradlew clean installDebug ? (Add the flavor name between install and Debug) If it doesn't work you have two chances: you didn't configure multidex well or that class doesn't exist for the variant/flavor you're running – Mimmo Grottoli Nov 21 '16 at 18:07

3 Answers3

11

This is a known issue when using the jack-compiler and MultiDex together. This causes NoClassDefFoundError on Pre-Lollipop devices, possibly on Pre-Marshmallow as well. The solution is to disable the jack-compiler, and use RetroLambda instead.

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'me.tatarka:gradle-retrolambda:3.6.0'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

repositories {
   mavenCentral()
}

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.2'
    defaultConfig {
        applicationId "your.application.id"
        minSdkVersion 16
        targetSdkVersion 25
        multiDexEnabled true

        jackOptions {
            enabled false
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }

}

Don't forget to upvote Thomas Sunderland his answer , because that is how I found this solution.

Rockney
  • 10,380
  • 2
  • 20
  • 26
  • I think the answer mistakenly quotes "pre-lollipop" devices. Shouldn't this be "pre-marhsmallow" devices? I see this behavior on 5.0 5.1 and 5.1.1 – AutoM8R Sep 12 '17 at 03:15
  • @AutoM8R I only experienced it on pre-lollipop devices, but apparently you and some and other users on pre-marshmallow devices have the same issue. According to a duplicate of the referenced issue, so I updated my answer. – Rockney Sep 13 '17 at 17:16
5

Your application class should extends MultiDexApplication instead of extends Application.

Hope this helps.

Ye Min Htut
  • 2,904
  • 15
  • 28
3

One of the reason of this error is because of MultiDexApplication .I have face this kind of issue with some other library

To resolve that you need to handle Multiple Dex file. with the help of application build.gradle & Application class

below changes which is required in build.gradle file

dexOptions {
        incremental true
        // here heap size give 4g(for ubuntu you can try with 2g) i got this thing from //https://groups.google.com/forum/#!topic/adt-dev/P_TLBTyFWVY

        javaMaxHeapSize "4g"
    }


dependencies {
     compile 'com.android.support:multidex:1.0.1'
    //    your dependencies which you are using.

}

Entire build.gradle

android {
    signingConfigs {
        /*
        releasebuild {
            keyAlias 'hellotest'
            keyPassword 'hellotest'
            storeFile file('path to keystore')
            storePassword 'hellotest'
        }
        */
    }
    compileSdkVersion 'Google Inc.:Google APIs:22'
    buildToolsVersion '23.0.0'
    /* if you got error regarding duplicate file of  META-INF/LICENSE.txt from jar file
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
    }
    */
    dexOptions {
        jumboMode = true
        incremental true
        // here heap size give 4g i got this thing from https://groups.google.com/forum/#!topic/adt-dev/P_TLBTyFWVY

        javaMaxHeapSize "4g"
    }
    defaultConfig {
        multiDexEnabled true
        applicationId "com.myapp.packagenme"
        minSdkVersion 17
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.releasebuild
        }
        debug {
            signingConfig signingConfigs.releasebuild
        }
    }
}

dependencies {
     compile 'com.android.support:multidex:1.0.1'
    //    your dependencies which you are using.

}

If your app uses extends the Applicationclass, you can override the attachBaseContext() method and call MultiDex.install(this) to enable multidex. To install multipledex file context using Applicaiton class which should extends

public class MyAppClass extends MultiDexApplication{
@Override
    protected void attachBaseContext(Context newBase) {
        MultiDex.install(newBase);
        super.attachBaseContext(newBase);
    }
}

javaMaxHeapSize "4g" try with 2g if in case ubuntu system give any error.

Let me know if anything

user1140237
  • 5,015
  • 1
  • 28
  • 56
  • 2
    Why do you extend MultiDexApplication and at the same time call the MultiDex.install ? They are doing the same thing! – Mimmo Grottoli Nov 22 '16 at 13:18
  • @MimmoGrottoli i tried without installing but for the 4.4 and below still i was getting like XXXX.class not found error i guess its because of multiple dex files – user1140237 Nov 24 '16 at 05:57