java.lang.NoClassDefFoundError 和 ClassNotFoundException 添加 android Facebook SDK 到项目后

java.lang.NoClassDefFoundError and ClassNotFoundException after add android Facebook SDK to the project

我刚刚将 android Facebook SDK 添加到我的项目中以使用 Facebook 广告来推广我的应用程序,但应用程序在启动时崩溃了,我遇到了这个异常

这是日志

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: www.pro_cs_is.com, PID: 20307
    java.lang.NoClassDefFoundError: Failed resolution of: Lcom/facebook/appevents/InternalAppEventsLogger;
        at com.facebook.marketing.internal.MarketingLogger.<init>(MarketingLogger.java:47)
        at com.facebook.marketing.internal.MarketingInitProvider.setupCodeless(MarketingInitProvider.java:68)
        at com.facebook.marketing.internal.MarketingInitProvider.onCreate(MarketingInitProvider.java:49)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1927)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1902)
        at android.app.ActivityThread.installProvider(ActivityThread.java:6528)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6021)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5927)
        at android.app.ActivityThread.access00(ActivityThread.java:200)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1673)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6806)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.facebook.appevents.InternalAppEventsLogger" on path: DexPathList[[zip file "/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/base.apk", zip file "/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/split_lib_dependencies_apk.apk", zip file "/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/split_lib_resources_apk.apk", zip file "/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/split_lib_slice_0_apk.apk", zip file "/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/split_lib_slice_1_apk.apk", zip file "/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/split_lib_slice_2_apk.apk", zip file "/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/split_lib_slice_3_apk.apk", zip file "/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/split_lib_slice_4_apk.apk", zip file "/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/split_lib_slice_5_apk.apk", zip file "/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/split_lib_slice_6_apk.apk", zip file "/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/split_lib_slice_7_apk.apk", zip file "/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/split_lib_slice_8_apk.apk", zip file "/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/lib/arm64, /system/lib64, /vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at com.facebook.marketing.internal.MarketingLogger.<init>(MarketingLogger.java:47) 
        at com.facebook.marketing.internal.MarketingInitProvider.setupCodeless(MarketingInitProvider.java:68) 
        at com.facebook.marketing.internal.MarketingInitProvider.onCreate(MarketingInitProvider.java:49) 
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1927) 
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1902) 
        at android.app.ActivityThread.installProvider(ActivityThread.java:6528) 
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6021) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5927) 
        at android.app.ActivityThread.access00(ActivityThread.java:200) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1673) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:201) 
        at android.app.ActivityThread.main(ActivityThread.java:6806) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 
        Suppressed: java.io.IOException: No original dex files found for dex location /data/app/www.pro_cs_is.com-ewD1U225Ajh1l9uikBkdWg==/split_lib_resources_apk.apk
        at dalvik.system.DexFile.openDexFileNative(Native Method)
        at dalvik.system.DexFile.openDexFile(DexFile.java:354)
        at dalvik.system.DexFile.<init>(DexFile.java:101)
        at dalvik.system.DexFile.<init>(DexFile.java:75)
        at dalvik.system.DexPathList.loadDexFile(DexPathList.java:394)
        at dalvik.system.DexPathList.makeDexElements(DexPathList.java:354)
        at dalvik.system.DexPathList.<init>(DexPathList.java:164)
        at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:74)
        at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
        at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
        at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73)
        at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88)
E/AndroidRuntime:     at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:74)
        at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:40)
        at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:729)
        at android.app.LoadedApk.getClassLoader(LoadedApk.java:812)
        at android.app.LoadedApk.getResources(LoadedApk.java:1034)
        at android.app.ContextImpl.createAppContext(ContextImpl.java:2357)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5817)
                ... 8 more

build.gradle(模块:应用程序)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "test.com.app"
        minSdkVersion 15
        targetSdkVersion 28
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            useProguard true
            debuggable false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        checkReleaseBuilds false
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
    packagingOptions {
        exclude 'META-INF/proguard/androidx-annotations.pro'
    }
    dataBinding {
        enabled = true
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.vectordrawable:vectordrawable-animated:1.0.0'
    implementation 'androidx.exifinterface:exifinterface:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.1.0-alpha05'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.google.firebase:firebase-messaging:17.5.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0-alpha03'
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-alpha4', {
        exclude group: 'com.google.code.findbugs'
    })
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    implementation 'org.jsoup:jsoup:1.11.3'
    implementation 'com.google.apis:google-api-services-blogger:v3-rev57-1.23.0'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'org.apache.commons:commons-lang3:3.8.1'
    implementation 'com.github.ybq:Android-SpinKit:1.2.0'
    implementation 'com.google.firebase:firebase-core:16.0.8'
    implementation('com.crashlytics.sdk.android:crashlytics:2.9.8@aar') {
        transitive = true
    }

    implementation 'androidx.multidex:multidex:2.0.1'
    configurations {
        all*.exclude group: 'com.google.guava', module: 'listenablefuture'
    }
    implementation 'com.android.support:customtabs:28.3.0'
    implementation 'org.jetbrains:annotations:15.0'
    implementation 'org.ocpsoft.prettytime:prettytime:4.0.1.Final'
    implementation 'com.github.seventhmoon:ipapi-retrofit:1.44'
    implementation 'com.google.firebase:firebase-ads:17.2.0'
    implementation 'com.google.android.ads.consent:consent-library:1.0.7'
    implementation 'com.facebook.android:facebook-android-sdk:4.42.0'
    implementation 'com.facebook.android:facebook-core:4.42.0'
    implementation 'com.facebook.android:facebook-marketing:4.42.0'

}
apply plugin: 'com.google.gms.google-services'
apply plugin: 'io.fabric'

AndroidManifest 文件中的元数据

 <meta-data android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id"/>

我尝试添加 old/previous 版本的 facebook SDK,但这也没有用

implementation 'com.facebook.android:facebook-android-sdk:[4,5

proguard Facebook 规则

# Facebook
-keep class com.facebook.** {*;}
-dontwarn com.facebook.**

尝试禁用 Instant 运行。转到文件 -> 设置 -> 构建、执行、部署 -> 即时 运行 -> 取消选中即时 运行.

的复选框

尝试在您的 build.gradle 文件中添加,

repositories {
    mavenCentral()
}

您也可以转到文件 -> 使缓存无效并重新启动 -> 无效并重新启动

更新:将 SDK 版本更改为 4.39.0 即可修复。

你的应用有上网权限吗?

也试试 4.39.0 作为 FB SDK 版本

不要忘记使缓存失效并重新启动。