Android项目出现java.lang.NoClassDefFoundError问题求助
Hey there, let's tackle this java.lang.NoClassDefFoundError you're hitting when Firebase initializes. Looking at your error log and build.gradle config, the main issues are mismatched Firebase dependency versions, missing Google Play Services core components, and a small gap in your Multidex setup. Here's how to fix it step by step:
1. Standardize Firebase Dependency Versions
Right now, your Firebase modules have inconsistent versions (core:15.0.2, messaging:15.0.2, database:15.0.1, auth:15.1.0). This causes dependency conflicts that break the Google Play Services resources Firebase needs. Update them to use the same version:
implementation 'com.google.firebase:firebase-core:15.0.2' implementation 'com.google.firebase:firebase-messaging:15.0.2' implementation 'com.google.firebase:firebase-database:15.0.2' implementation 'com.google.firebase:firebase-auth:15.0.2'
2. Add Google Play Services Base Dependency
The error specifically mentions missing com.google.android.gms.common.R$string, which means you're missing the core Google Play Services library that Firebase relies on. Add this to your dependencies:
implementation 'com.google.android.gms:play-services-base:15.0.1'
(Note: 15.0.1 is fully compatible with Firebase 15.0.2 versions)
3. Complete Multidex Configuration
You've enabled multiDexEnabled true, but you need to make sure your app properly initializes Multidex:
- If you have a custom Application class, have it extend
MultiDexApplication:
public class MyApp extends MultiDexApplication { // Your existing application code here }
- If you can't change the parent class of your Application, override
attachBaseContextto initialize Multidex:
@Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); }
Don't forget to update your AndroidManifest.xml to point to your custom Application class (if you have one) using the android:name attribute in the <application> tag.
4. Clean and Rebuild Your Project
After making these changes, do a full clean and rebuild to ensure all dependencies are resolved correctly:
- Go to
Build > Clean Projectin Android Studio - Then run
Build > Rebuild Project - Finally, clear your app's cache and data on the device/emulator before re-launching
Full Error Log
05-16 09:59:53.428 25010-25010/main.faseflex.de.fgnapp E/AndroidRuntime: FATAL EXCEPTION: main Process: main.faseflex.de.fgnapp, PID: 25010 java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/R$string; at com.google.android.gms.common.internal.StringResourceValueReader.<init>(Unknown Source:14) at com.google.firebase.FirebaseOptions.fromResource(Unknown Source:2) at com.google.firebase.FirebaseApp.initializeApp(Unknown Source:19) at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source:4) at android.content.ContentProvider.attachInfo(ContentProvider.java:1925) at android.content.ContentProvider.attachInfo(ContentProvider.java:1900) at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source:23) at android.app.ActivityThread.installProvider(ActivityThread.java:6578) at android.app.ActivityThread.installContentProviders(ActivityThread.java:6133) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6043) at android.app.ActivityThread.-wrap1(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6944) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.R$string" on path: DexPathList[[zip file "/data/app/main.faseflex.de.fgnapp-C4iBOV7sIO2cNc4pfCATmA==/base.apk"],nativeLibraryDirectories=[/data/app/main.faseflex.de.fgnapp-C4iBOV7sIO2cNc4pfCATmA==/lib/arm64, /system/lib64, /system/vendor/lib64]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93) at java.lang.ClassLoader.loadClass(ClassLoader.java:379) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) at com.google.android.gms.common.internal.StringResourceValueReader.<init>(Unknown Source:14) at com.google.firebase.FirebaseOptions.fromResource(Unknown Source:2) at com.google.firebase.FirebaseApp.initializeApp(Unknown Source:19) at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source:4) at android.content.ContentProvider.attachInfo(ContentProvider.java:1925) at android.content.ContentProvider.attachInfo(ContentProvider.java:1900) at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source:23) at android.app.ActivityThread.installProvider(ActivityThread.java:6578) at android.app.ActivityThread.installContentProviders(ActivityThread.java:6133) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6043) at android.app.ActivityThread.-wrap1(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6944) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Your App Module build.gradle Config
apply plugin: 'com.android.application' android { compileSdkVersion 25 //buildToolsVersion "25.0.2" defaultConfig { applicationId "main.faseflex.de.fgnapp" minSdkVersion 21 targetSdkVersion 25 multiDexEnabled true dexOptions { preDexLibraries true } versionCode 1 versionName "0.98.41" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' multiDexKeepFile file('multidex-config.txt') } } productFlavors { } } dependencies { implementation 'com.android.support:support-v4:25.4.0' implementation 'com.android.support:support-vector-drawable:25.4.0' implementation fileTree(include: ['*.jar'], dir: 'libs') androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) implementation 'com.google.firebase:firebase-core:15.0.2' implementation 'com.google.firebase:firebase-messaging:15.0.2' implementation 'com.google.firebase:firebase-database:15.0.1' implementation 'com.google.firebase:firebase-auth:15.1.0' implementation 'com.android.support:appcompat-v7:25.4.0' implementation 'com.android.support:design:25.4.0' implementation 'com.android.support:preference-v7:25.4.0' implementation 'com.android.support.constraint:constraint-layout:1.1.0' implementation 'com.github.SHashank02051997:FancyToast-Android:0.1.3' implementation 'com.android.support:multidex:1.0.3' }
内容的提问来源于stack exchange,提问作者Dureox

