java.lang.RuntimeException:无法实例化应用程序:ClassNotFoundException(仅限X86架构设备)

28
这似乎是Stack Overflow上最常被问到的问题之一,但即使我尝试了10多个问题的20多种解决方案并参考了Android文档,我的问题仍未得到解决。
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

问题
1. 应用程序在Motorola、三星S6和三星S7等非x86设备上运行良好
2. 应用程序在x86架构的设备上抛出错误。

我到目前为止尝试过的事情
1. 在清单文件、包名等中交叉检查包名。
2. 在清单文件中使用了完整和部分包名作为android:name属性的值。
3. 将Application类从utils包移动到Main Package中。

背景
1. 安装Android Studio时执行命令sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 lib32bz2-1.0,除lib32bz2-1.0外都正常工作,但是直到现在没有出现任何问题。(以前从未尝试在x86设备上安装应用程序)
2. CompileSDKVersion - 25
3. BuildToolsVersion - 25.0.0
4. Gradle version - 2.2.2

工作环境
1. Ubuntu 16.04
2. 已更新至JAVA 8
3. Android Studio 2.2.2

编辑: 我认为可能会引起问题的部分gradle(app)

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/')
    }
}

附注:已经多次交叉检查了清单,似乎没有问题。在任何 Android 文档中也找不到这种异常的可能原因。

更新:参考此答案后,启用即时运行不会导致此问题。但是通过 debug.apk 安装应用程序仍然出现相同的问题。


1
你是否启用了multidex? - Sourabh Saldi
嗨,Atif,你能否在Android Studio中禁用“即时运行”功能并运行你的应用程序,试一下,也许会有所帮助。 - Ram Koti
嗨@Ram,感谢您的建议。即时运行已经被禁用,我在问题的最后一行已经提到了启用即时运行的问题。 - Mohammed Atif
@Mr.Z,我已经检查了build.gradle文件。所有相关的数据都已经在问题中了,其他的是依赖项和与Fabric集成相关的代码。 - Mohammed Atif
你尝试过使用简单的命令行./gradlew clean installDebug吗?(在install和Debug之间添加flavor名称)。如果它不起作用,你有两个选择:你没有正确配置multidex或该类不存在于你正在运行的variant/flavor中。 - Mimmo Grottoli
显示剩余14条评论
3个回答

11

当使用jack编译器和MultiDex一起时会出现已知问题。这可能会导致Pre-Lollipop设备(以及可能的Pre-Marshmallow设备)出现NoClassDefFoundError。解决方法是禁用jack编译器,改用RetroLambda

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
        }
    }

}

别忘了给Thomas Sunderland的回答点赞,因为正是这样我才找到了这个解决方案。


我认为答案错误地引用了“棒棒糖(Lollipop)”之前的设备。这不应该是“棉花糖(Marshmallow)”之前的设备吗?我在5.0、5.1和5.1.1上看到了这种行为。 - AutoM8R
@AutoM8R 我只在Lollipop之前的设备上遇到过这个问题,但显然你和其他一些在Marshmallow之前的用户也有同样的问题。根据引用问题的重复情况,我更新了我的答案。 - Rockney

5
您的应用程序类应该 extends MultiDexApplication 而不是 extends Application
希望这有所帮助。

1
MultiDexApplication缺少很多常见的功能。 - Oliver Dixon

3

这种错误的原因之一是由于MultiDexApplication。我曾经在使用其他库时遇到过这种问题。

要解决这个问题,您需要处理多个Dex文件。通过应用程序build.gradleApplication类的帮助来实现。

以下是在build.gradle文件中所需的更改:

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.

}

整个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.

}

如果您的应用程序使用扩展Application类,您可以重写attachBaseContext()方法并调用MultiDex.install(this)来启用multidex。使用扩展Application类安装multipledex文件上下文。请注意,应用程序类必须扩展自Application类。
public class MyAppClass extends MultiDexApplication{
@Override
    protected void attachBaseContext(Context newBase) {
        MultiDex.install(newBase);
        super.attachBaseContext(newBase);
    }
}

javaMaxHeapSize "4g" 尝试使用 2g 如果Ubuntu系统出现错误,请让我知道。


2
为什么你要扩展MultiDexApplication并同时调用MultiDex.install?它们做的是同样的事情! - Mimmo Grottoli
@MimmoGrottoli 我尝试过不安装,但对于4.4及以下版本,仍然会出现XXXX.class未找到的错误。我想这是因为有多个dex文件。 - user1140237

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接