Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to instantiate application: java.lang.ClassNotFoundException:

The issue occurs if:

  • I run app from Android Studio , the app runs properly

    then I delete the app and tried to install debug.apk from .../app/build/outputs/apk/debug.apk

My OS is Ubuntu 16.04, oracle jdk

but on Windows 7 all works properly(apk can be installed from .../app/build/outputs/apk/debug.apk and runs properly)

This error occurs:

FATAL EXCEPTION: main
Process: com.example, PID: 21084
java.lang.RuntimeException: Unable to instantiate application com.example.MyApp: java.lang.ClassNotFoundException: Didn't find class "com.example.MyApp" on path: DexPathList[[zip file "/data/app/com.example-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example-1/lib/arm, /vendor/lib, /system/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:676)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6289)
at android.app.ActivityThread.access$1800(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1860)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.MyApp" on path: DexPathList[[zip file "/data/app/com.example-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example-1/lib/arm, /vendor/lib, /system/lib]]
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:1004)
at android.app.LoadedApk.makeApplication(LoadedApk.java:666)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6289) 
at android.app.ActivityThread.access$1800(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1860) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Suppressed: java.lang.ClassNotFoundException: com.example.MyApp
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)
        ... 12 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

My gradle:

    apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'io.fabric'
apply plugin: 'android-apt'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

defaultConfig {
    applicationId "com.example"
    minSdkVersion 19
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"

    multiDexEnabled true
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    jackOptions {
        enabled false
    }
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

dexOptions {
    javaMaxHeapSize "8g"
    jumboMode = true
    preDexLibraries = false
}

signingConfigs {
    //release
    release {
        storeFile file("/keys/apk_key.jks")
        storePassword "apk_key"
        keyAlias "apk_key"
        keyPassword "apk_key"
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.release
        debuggable false
    }
    debug {
        signingConfig signingConfigs.release
        debuggable true
    }
}

dexOptions {
    javaMaxHeapSize "3G"
}

packagingOptions {
    exclude 'LICENSE.txt'
    exclude 'mockito-extensions/org.mockito.plugins.MockMaker'
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
}

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    ...
}

Also I tried to:

  • Invalidate Cache/Restart

  • Clean Project

  • Rebuild project

like image 389
NickUnuchek Avatar asked Apr 07 '17 08:04

NickUnuchek


3 Answers

Thank you @Dileep Patel I faced the same issue. App only worked if it's installed from Android Studio and crashed if it's installed by adb command. Invalidate caches/Restart and disabling Instant Run(Android Studio->Preferences->Instant Run, uncheck "Enable Instance Run") worked in my case.

like image 177
Nay Chi Avatar answered Sep 22 '22 08:09

Nay Chi


add multidex in app level gradle

compile 'com.android.support:multidex:1.0.1'

enable multidex

 defaultConfig {
 multiDexEnabled true
}

Last add this code inside application class

 @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
like image 44
Om Infowave Developers Avatar answered Sep 26 '22 08:09

Om Infowave Developers


I just wanna say that I know this is really dumb and I know the question has already been answered but I changed the name of my App class and forgot to change it in the manifest, for some reason the app still built. Anyway make sure you have the correct name of your app class in the manifests of your project:

    <application
    android:name=".CorrectAppClassHere"
        //...
like image 21
nmu Avatar answered Sep 25 '22 08:09

nmu