【问题标题】:java.lang.RuntimeException: Unable to instantiate application : ClassNotFoundException (Only on X86 architecture device)java.lang.RuntimeException:无法实例化应用程序:ClassNotFoundException(仅在 X86 架构设备上)
【发布时间】:2016-11-11 11:01:07
【问题描述】:

这似乎是 Stack Overflow 中提出的最高问题之一,但即使在尝试了 10 多个问题中的 20 多个解决方案并参考了 Android Docs 之后,我的问题仍然没有解决。

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. 应用在摩托罗拉、三星 S6、三星 S7 等非 x86 设备上运行良好
2. 应用在 x86 架构设备上抛出该错误。

我到目前为止所尝试的
1. 我在 Manifest、packages 等中交叉检查了包名。
2. 为清单中的android:name 属性提供完整和部分包名称。
3. 尝试将Application 类从utils 包移动到主包。

背景
1. 安装 Android Studio - sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 lib32bz2-1.0 时,除了 lib32bz2-1.0 外,一切正常,但到目前为止没有任何问题。 (之前从未尝试在 x86 设备上安装该应用程序)
2. 编译SDK版本 - 25
3. BuildToolsVersion - 25.0.0
4. Gradle 版本 - 2.2.2

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

编辑: 我认为可能导致问题的部分 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/')
    }
}

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

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

【问题讨论】:

  • 你启用了multidex吗?
  • 是 @AndroidFanatic,multidex 已启用,proguard 已关闭。
  • 我们可以看看你的 build.gradle 文件吗?
  • build.gradle 的哪一部分?以及项目模块或应用程序模块? @SergeyNikitin
  • flavorDimensions 和 productFlavors @MohammedAtif

标签: java android android-studio x86


【解决方案1】:

当同时使用 jack-compiler 和 MultiDex 时,这是一个 known issue。这会在 Pre-Lollipop 设备上导致 NoClassDefFoundError,也可能在 Pre-Marshmallow 上。解决办法是禁用jack-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

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

}

别忘了点赞Thomas Sunderland his answer,因为我就是这样找到了这个解决方案的。

【讨论】:

  • 我认为答案错误地引用了“pre-lollipop”设备。这不应该是“pre-marhsmallow”设备吗?我在 5.0 5.1 和 5.1.1 上看到了这种行为
  • @AutoM8R 我只在棒棒糖之前的设备上体验过它,但显然你和一些在棉花糖之前的设备上的其他用户也有同样的问题。根据引用问题的副本,所以我更新了我的答案。
【解决方案2】:

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

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

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.

}

如果您的应用程序使用扩展应用程序类,您可以重写 attachBaseContext() 方法并调用 MultiDex.install(this) 来启用 multidex。 To install multipledex file context 使用应扩展的 Applicaiton 类

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

javaMaxHeapSize "4g" 尝试 2g 以防 ubuntu 系统出现任何错误。

如果有什么请告诉我

【讨论】:

  • 为什么要扩展 MultiDexApplication 并同时调用 MultiDex.install ?他们在做同样的事情!
  • @MimmoGrottoli 我试过不安装,但对于 4.4 及以下版本,我仍然觉得 XXXX.class not found 错误我猜是因为多个 dex 文件
【解决方案3】:

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

希望这会有所帮助。

【讨论】:

  • MultiDexApplication 缺少很多常用功能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 2013-04-12
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
相关资源
最近更新 更多