Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.RuntimeException: Unable to instantiate application : ClassNotFoundException (Only on X86 architecture device)

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.

like image 278
Mohammed Atif Avatar asked Nov 11 '16 11:11

Mohammed Atif


3 Answers

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.

like image 70
Rockney Avatar answered Nov 14 '22 13:11

Rockney


Your application class should extends MultiDexApplication instead of extends Application.

Hope this helps.

like image 24
Ye Min Htut Avatar answered Nov 14 '22 11:11

Ye Min Htut


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

like image 3
user1140237 Avatar answered Nov 14 '22 13:11

user1140237