原因:java.lang.NoClassDefFoundError: Class not found using the boot class loader;没有可用的堆栈跟踪

我尝试使用 OkHttp 和 Picasso(按照这个答案)对我从 Firebase 存储下载的图像进行磁盘缓存.现在,该应用程序给出了异常和崩溃.我看过这些帖子:post 1, 发表2 但我没有找到任何相关的解决方案.我也尝试清理和重建项目,但没有运气.

这里是 build.gradle:

应用插件:'com.android.application'安卓 {compileSdkVersion 25构建工具版本25.0.2"默认配置 {applicationId "social.com.networking.social.media.app"minSdkVersion 16targetSdkVersion 25版本代码 1版本名称1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}构建类型 {发布 {缩小启用假proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'multiDexEnabled 真}}}依赖{编译 fileTree(dir: 'libs', include: ['*.jar'])androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {排除组:'com.android.support',模块:'support-annotations'})编译'com.android.support:appcompat-v7:25.1.0'编译'com.android.support:design:25.1.0'编译'br.com.mauker.materialsearchview:materialsearchview:1.2.0'编译 'com.alirezaafkar:toolbar:1.1.1'编译'com.github.mancj:MaterialSearchBar:0.3.5'编译'com.squareup.picasso:picasso:2.5.2'编译'com.nineoldandroids:library:2.4.0'编译'com.daimajia.slider:library:1.1.5@aar'编译'com.google.code.gson:gson:2.6.2'编译'com.google.firebase:firebase-core:10.0.1'编译'com.google.firebase:firebase-ads:10.0.1'编译'com.google.firebase:firebase-messaging:10.0.1'编译com.google.firebase:firebase-auth:10.0.1"编译'com.google.firebase:firebase-storage:10.0.1'编译 'com.google.firebase:firebase-crash:10.0.1'编译'com.squareup.okhttp3:okhttp:3.2.0'编译'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.0.2'编译'com.android.support:multidex:1.0.1'编译'org.parceler:parceler-api:1.1.6'annotationProcessor 'org.parceler:parceler:1.1.6'测试编译'junit:junit:4.12'}应用插件:'com.google.gms.google-services'

完整的堆栈跟踪:

进程:social.com.networking.social.media.app,PID:28258java.lang.NoClassDefFoundError:解析失败:Lcom/squareup/okhttp/OkHttpClient;在 com.squareup.picasso.OkHttpDownloader.defaultOkHttpClient(OkHttpDownloader.java:31)在 com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:76)在 com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:65)在 social.com.networkingsocialmediaapp.Global.onCreate(Global.java:17)在 android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1018)在 android.app.ActivityThread.handleBindApplication(ActivityThread.java:4991)在 android.app.ActivityThread.-wrap1(ActivityThread.java)在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1555)在 android.os.Handler.dispatchMessage(Handler.java:111)在 android.os.Looper.loop(Looper.java:207)在 android.app.ActivityThread.main(ActivityThread.java:5776)在 java.lang.reflect.Method.invoke(本机方法)在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)原因:java.lang.ClassNotFoundException:在路径上找不到类com.squareup.okhttp.OkHttpClient":DexPathList [[zip file/data/app/social.com.networking.social.media.app-2/base.apk"],nativeLibraryDirectories=[/data/app/social.com.networking.social.media.app-2/lib/arm64,/vendor/lib64,/system/lib64]]在 dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)在 java.lang.ClassLoader.loadClass(ClassLoader.java:511)在 java.lang.ClassLoader.loadClass(ClassLoader.java:469)在 com.squareup.picasso.OkHttpDownloader.defaultOkHttpClient(OkHttpDownloader.java:31)在 com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:76)在 com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:65)在 social.com.networkingsocialmediaapp.Global.onCreate(Global.java:17)在 android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1018)在 android.app.ActivityThread.handleBindApplication(ActivityThread.java:4991)在 android.app.ActivityThread.-wrap1(ActivityThread.java)在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1555)在 android.os.Handler.dispatchMessage(Handler.java:111)在 android.os.Looper.loop(Looper.java:207)在 android.app.ActivityThread.main(ActivityThread.java:5776)在 java.lang.reflect.Method.invoke(本机方法)在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)抑制:java.lang.ClassNotFoundException:com.squareup.okhttp.OkHttpClient在 java.lang.Class.classForName(本机方法)在 java.lang.BootClassLoader.findClass(ClassLoader.java:781)在 java.lang.BootClassLoader.loadClass(ClassLoader.java:841)在 java.lang.ClassLoader.loadClass(ClassLoader.java:504)... 15 更多原因:java.lang.NoClassDefFoundError: Class not found using the boot class loader;没有可用的堆栈跟踪

编辑 1:正如 Redman 所说,我已经启用了 MultiDex,但它仍然给出了同样的错误.据此:developer.android.com/studio/build/multidex.html#keep,我需要确定基础 APK 中需要哪些类.我如何确定哪些课程是必要的?

解决方案

在Android Studio 2.3 及更高版本中,Instant Run可能会影响您的代码.p>

禁用即时运行

文件 -> 设置 -> 构建、执行、部署 -> 即时运行

<块引用>

在我的情况下,从 Android Studio 运行时它运行良好,但是从其他来源安装时崩溃

I tried using OkHttp and Picasso (following this answer) for disk caching of images that I was downloading from Firebase storage. Now, the app gives exceptions and crashes. I have seen these posts: post 1, post 2 but I didn't find any relevant solutions. I also tried to clean and rebuild the project but had no luck.

Here is build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "social.com.networking.social.media.app"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            multiDexEnabled true
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    compile 'br.com.mauker.materialsearchview:materialsearchview:1.2.0'
    compile 'com.alirezaafkar:toolbar:1.1.1'
    compile 'com.github.mancj:MaterialSearchBar:0.3.5'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.daimajia.slider:library:1.1.5@aar'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.google.firebase:firebase-core:10.0.1'
    compile 'com.google.firebase:firebase-ads:10.0.1'
    compile 'com.google.firebase:firebase-messaging:10.0.1'
    compile "com.google.firebase:firebase-auth:10.0.1"
    compile 'com.google.firebase:firebase-storage:10.0.1'
    compile 'com.google.firebase:firebase-crash:10.0.1'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.0.2'
    compile 'com.android.support:multidex:1.0.1'

    compile 'org.parceler:parceler-api:1.1.6'
    annotationProcessor 'org.parceler:parceler:1.1.6'


    testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'

Full Stacktrace:

Process: social.com.networking.social.media.app, PID: 28258
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/squareup/okhttp/OkHttpClient;
at com.squareup.picasso.OkHttpDownloader.defaultOkHttpClient(OkHttpDownloader.java:31)
at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:76)
at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:65)
at social.com.networkingsocialmediaapp.Global.onCreate(Global.java:17)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1018)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4991)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1555)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.squareup.okhttp.OkHttpClient" on path: DexPathList[[zip file "/data/app/social.com.networking.social.media.app-2/base.apk"],nativeLibraryDirectories=[/data/app/social.com.networking.social.media.app-2/lib/arm64, /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 com.squareup.picasso.OkHttpDownloader.defaultOkHttpClient(OkHttpDownloader.java:31)
at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:76)
at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:65)
at social.com.networkingsocialmediaapp.Global.onCreate(Global.java:17)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1018)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4991)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1555)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

Suppressed: java.lang.ClassNotFoundException: com.squareup.okhttp.OkHttpClient
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)
... 15 more

Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

Edit 1: I have enabled MultiDex as Redman said but it still gives the same error. According to this: developer.android.com/studio/build/multidex.html#keep, I need to decide which classes are necessary in the base APK. How do I determine which classes are necessary?

解决方案

In Android Studio 2.3 and higher, There is Instant Run which may affect your code.

Disable Instant Run

File -> Settings -> Build, Execution, Deployment -> Instant Run

In my case it was working perfect when run from Android Studio but crashing when install from other sources

相关文章