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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40546581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:18:27  来源:igfitidea点击:

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

javaandroidandroid-studiox86

提问by Mohammed Atif

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.

这似乎是 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.access00(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.access00(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.

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

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:nameattribute in the manifest.
3. Tried moving Applicationclass to Main Package from utilspackage.

到目前为止我尝试过的内容
1. 我在 Manifest、packages 等中交叉检查了包名。
2. 将完整和部分包名都android:name赋予了 manifest 中的属性。
3. 尝试将Application类从utils包移动到主包。

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

背景
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 版本 - 2.2.2

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

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

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

编辑: 我认为可能导致问题的 gradle(应用程序)的一部分

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.

PS 多次检查清单,似乎没有问题。在任何 Android 文档中也找不到任何可能导致此异常的原因。

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

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

回答by Rockney

This is a known issuewhen using the Hyman-compiler and MultiDex together. This causes NoClassDefFoundErroron Pre-Lollipop devices, possibly on Pre-Marshmallow as well. The solution is to disable the Hyman-compiler, and use RetroLambdainstead.

这是同时使用 Hyman-compiler 和 MultiDex 时的一个已知问题。这会导致NoClassDefFoundErrorPre-Lollipop 设备,也可能在 Pre-Marshmallow 上。解决方案是禁用 Hyman-compiler,并改用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

        HymanOptions {
            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.

不要忘记为Thomas Sunderland 的回答点赞,因为这就是我找到这个解决方案的方式。

回答by Ye Min Htut

Your application class should extends MultiDexApplicationinstead of extends Application.

您的应用程序类应该extends MultiDexApplication而不是extends Application.

Hope this helps.

希望这可以帮助。

回答by user1140237

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

这个错误的原因之一是因为 MultiDexApplication 。我在其他一些库中遇到过这种问题

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

要解决这个问题,您需要处理多个 Dex 文件。在应用程序的帮助下build.gradle&Application class

below changes which is required in build.gradlefile

下面是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.

}

Entire build.gradle

全部的 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 contextusing Applicaiton class which should extends

如果您的应用程序使用扩展应用程序类,您可以覆盖 attachBaseContext() 方法并调用 MultiDex.install(this) 以启用 multidex。使用应扩展的 Applicaiton 类 安装 Multipledex 文件上下文

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

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

javaMaxHeapSize "4g"2g如果 ubuntu 系统出现任何错误,请 尝试使用。

Let me know if anything

让我知道如果有的话