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

时间:2016-11-11 11:01:07

标签: java android android-studio x86

这似乎是Stack Overflow中问题最多的问题之一,但即使在尝试了10多个问题并引用Android Docs的20多个解决方案之后,我的问题仍未解决。

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

到目前为止我一直在尝试
我在Manifest,包装等中交叉检查了包裹名称 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. CompileSDKVersion - 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(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/')
    }
}
  

P.S。交叉检查Manifest几次,似乎没有问题。在任何Android文档中都找不到任何可能的异常原因。

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

3 个答案:

答案 0 :(得分:7)

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

}

不要忘记upvote Thomas Sunderland his answer,因为这就是我找到这个解决方案的方式。

答案 1 :(得分:1)

您的申请类应extends MultiDexApplication而不是extends Application

希望这有帮助。

答案 2 :(得分:0)

出现此错误的原因之一是MultiDexApplication。我遇到了其他一些库的问题

要解决此问题,您需要处理Multiple 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.

}

如果您的应用程序使用扩展Applicationclass,您可以覆盖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);
    }
}
如果ubuntu系统出现任何错误,

javaMaxHeapSize "4g"请尝试使用2g

如果有的话,请告诉我