Java.lang.NoClassDefFoundError: 无法解析 com/google/android/gms/common/R$string 的失败。

26
我尝试了很多方法,但都不能解决我的问题。
完整错误代码(Full Error Code):
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) 

build.gradle应用程序:

    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'
    implementation 'com.google.android.gms:play-services-auth:15.0.1'
    testImplementation 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '25.3.0'
            }
        }
    }
}

build.gradle 项目:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath 'com.google.gms:google-services:3.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        google()
        maven {
            url "https://jitpack.io"
        }
        maven {
            url "https://maven.google.com"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="main.faseflex.de.fgnapp"
    tools:node="replace">

    <supports-screens
        android:anyDensity="true"
        android:compatibleWidthLimitDp="50"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="false"
        android:smallScreens="false"
        android:xlargeScreens="false" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/fgn_icon"
        android:label="@string/app_name"
        android:resizeableActivity="false"
        android:roundIcon="@mipmap/fgn_icon"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        ------------------------------------------------
        MAIN ACTIVITY
        ------------------------------------------------
        <activity
            android:name=".FGNmainActivity"
            android:label="FGN-App"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        STUNDENPLAN
        ------------------------------------------------
        <activity
            android:name=".activity_timetable"
            android:label="FGN-App | Stundenplan"
            android:parentActivityName=".FGNmainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <category android:name="android.intent.category.APP_BROWSER" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        ESSENSBESTELLUNG
        ------------------------------------------------
        <activity
            android:name=".activity_food"
            android:label="FGN-App | Essenplan"
            android:parentActivityName=".FGNmainActivity"
            android:theme="@style/AppTheme">
            <intent-filter>
                <category android:name="android.intent.category.APP_BROWSER" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        MDV
        ------------------------------------------------
        <activity
            android:name=".activity_mdv"
            android:label="FGN-App | Verkehrsverbund"
            android:parentActivityName=".FGNmainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <category android:name="android.intent.category.APP_BROWSER" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        DOKUMENTE
        ------------------------------------------------
        <activity
            android:name=".activity_documents"
            android:label="FGN-App | Dokumente"
            android:parentActivityName=".FGNmainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <category android:name="android.intent.category.APP_BROWSER" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        NEWS
        ------------------------------------------------
        <activity
            android:name=".activity_news"
            android:label="FGN-App | News"
            android:parentActivityName=".FGNmainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <category android:name="android.intent.category.APP_BROWSER" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        LEHRER
        ------------------------------------------------
        <activity
            android:name=".activity_teachers"
            android:label="FGN-App | Lehrer"
            android:parentActivityName=".FGNmainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <category android:name="android.intent.category.APP_BROWSER" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        MOODLE
        ------------------------------------------------
        <activity
            android:name=".activity_moodle"
            android:label="FGN-App | Moodle"
            android:parentActivityName=".FGNmainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <category android:name="android.intent.category.APP_BROWSER" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        OFFICE 365
        ------------------------------------------------
        <activity
            android:name=".activity_office"
            android:label="FGN-App | Office 365"
            android:parentActivityName=".FGNmainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <category android:name="android.intent.category.APP_BROWSER" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        KONTAKT
        ------------------------------------------------
        <activity
            android:name=".activity_contact"
            android:label="FGN-App | Kontakt"
            android:parentActivityName=".FGNmainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <category android:name="android.intent.category.APP_BROWSER" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        TERMINE
        ------------------------------------------------
        <activity
            android:name=".activity_termine"
            android:label="FGN-App | Termine"
            android:parentActivityName=".FGNmainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <category android:name="android.intent.category.APP_BROWSER" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        MITTAGSBAND
        ------------------------------------------------
        <activity
            android:name=".activity_mittagsband"
            android:label="FGN-App | Mittagsband"
            android:parentActivityName=".FGNmainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <category android:name="android.intent.category.APP_BROWSER" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        FEEDBACK
        ------------------------------------------------
        <activity
            android:name=".activity_menu_feedback"
            android:label="FGN-App | Feedback"
            android:parentActivityName=".SettingsActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        IMPRESSUM
        ------------------------------------------------
        <activity
            android:name=".activity_menu_impressum"
            android:label="FGN-App | Impressum"
            android:parentActivityName=".FGNmainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        FIREBASE
        ------------------------------------------------
        <service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".FirebaseIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".MyInstanceIDListenerService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        ------------------------------------------------
        EINSTELLUNGEN
        ------------------------------------------------
        <activity
            android:name=".SettingsActivity"
            android:label="FGN-App | Einstellungen"
            android:parentActivityName=".FGNmainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="main.faseflex.de.fgnapp.FGNmainActivity" />
        </activity>
        ------------------------------------------------
        SPLASH SCREEN
        ------------------------------------------------
        <activity
            android:name=".SplashActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        ------------------------------------------------
        CHANGELOG
        ------------------------------------------------
        <activity
            android:name=".ChangelogActivity"
            android:label="FGN-App | Changelog"
            android:parentActivityName=".SettingsActivity"></activity>
    </application>



</manifest>

我知道NoClassDefFoundError是什么,但我没有找到问题所在。如果需要更多文件来解决问题,请告诉我。


这可能是multidex问题。您可以尝试从https://developer.android.com/studio/build/multidex使用第二种解决方案。 - Vishu
我没有完全阅读你的帖子,如果你移除proguard会发生什么?异常还会发生吗?该库中的其他类(在同一个包中)是否在运行时存在? - deathangel908
@Vishu,我添加了multidex,但它不起作用 :/ - Dureox
@deathangel908 我删除了这一行 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 但是它还是不起作用。 - Dureox
你的项目中存在一些冲突的库,请检查 ./gradlew app:dependencies - deathangel908
@deathangel908 我已经完成了,构建成功并执行了1个可执行任务。 - Dureox
10个回答

44
如果您的应用程序中使用了{{Realm plugin}},那么您可以尝试{{升级插件}},因为这是应用程序在运行时崩溃的原因之一。


更新Realm插件的步骤:

  • 打开build.gradle(Project: Project_name_here)文件。
  • 将realm gradle插件的classpath替换为最新稳定版本,如下所示:
  • classpath "io.realm:realm-gradle-plugin:7.0.0"


此外,请检查build.gradle文件中是否有任何过时的插件,因为这可能是应用程序崩溃的原因之一。




我在将Android Studio更新到3.6并使用Gradle 3.6.0后遇到了类似的问题。

为了解决这个问题,我将Gradle降级到3.5.3,直到未来修复。


步骤:

  • 打开build.gradle(Project: Project_name_here)文件。
  • 在依赖项下将android工具构建gradle的classpath替换为以下内容:
  • classpath 'com.android.tools.build:gradle:3.5.3'



请注意:这只是一个临时解决方案。它可能会在未来得到修复,因此请降级并继续应用程序开发。


19
如果您刚刚将项目更新到Gradle 3.6.0并且正在使用 Realm ,则非常有可能此问题是由 Realm插件引起的。 根据评论,此问题发生在旧版Realm插件中,小于5.13.1。 因此,只需更新到最新版本的Realm即可解决问题。

截至撰写本文时,它是: classpath "io.realm:realm-gradle-plugin:6.1.0"


这个有效。https://github.com/firebase/firebase-android-sdk/issues/665#issuecomment-574431015 - ahmadalibaloch

9
你需要更新你的REALM-PLUGIN,并且你需要将以下内容写入顶级Gradle(Project:Gradle),在"classpath"行下方复制粘贴:
buildscript {
  ... 
  dependencies {
    classpath 'io.realm:realm-gradle-plugin:7.0.0' //<--Your answer.
  } 
}

3

你好,我知道这个问题提得有点晚了,但对于当前的安卓版本,我解决了这个问题。

在build.gradle项目级别中更新依赖项:

  • 将com.android.tools.build:gradle更新为最新的稳定版本(安卓更新不会自动更新)

  • 如果使用Realm:

  • 如果使用Google移动服务

    • 也需要更新

在build.gradle应用级别更新你的依赖项

一些答案涉及multidex和其他修复措施,但都没有解决我的问题。

这就是为什么我要给出这个答案的原因。

我的当前设置:

项目级别:

dependencies {
   classpath 'com.android.tools.build:gradle:4.0.0'
   classpath "io.realm:realm-gradle-plugin:6.0.2"
   classpath 'com.google.gms:google-services:4.3.3'
}

应用级别

dependencies {
  implementation fileTree(include: ['*.jar'], dir: 'libs')
  implementation 'androidx.core:core:1.3.0'
  implementation 'androidx.annotation:annotation:1.1.0'
  implementation 'androidx.appcompat:appcompat:1.1.0'
  implementation 'com.google.android.material:material:1.1.0'
  implementation 'com.android.volley:volley:1.1.1'
  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  implementation 'androidx.legacy:legacy-support-v4:1.0.0'
  implementation 'androidx.recyclerview:recyclerview:1.0.0'
  testImplementation 'junit:junit:4.12'
  androidTestImplementation 'androidx.test:runner:1.2.0'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
  implementation 'com.facebook.android:facebook-login:5.15.3'
  implementation "com.google.android.gms:play-services-location:17.0.0"
  implementation 'com.google.code.gson:gson:2.8.5'
  implementation 'com.squareup.picasso:picasso:2.71828'
  implementation 'commons-validator:commons-validator:1.6'
  implementation 'com.appeaser.sublimepickerlibrary:sublimepickerlibrary:2.1.2'
  implementation 'de.hdodenhof:circleimageview:2.2.0'
  implementation 'com.google.maps.android:android-maps-utils:0.5+'
  implementation 'com.google.android.gms:play-services-maps:17.0.0'
  implementation 'com.evrencoskun.library:tableview:0.8.8'
  implementation 'androidx.multidex:multidex:2.0.1'
  implementation 'com.onesignal:OneSignal:[3.9.1, 3.99.99]'
  implementation 'com.google.firebase:firebase-core:17.4.3'
  implementation 'com.facebook.android:facebook-share:5.15.3'
  implementation('com.smartlook.recording:app:1.1.0-native')
}

希望我能帮助到一些人!

1

我因为使用realm-db遇到了这个问题

这是因为使用了gradle的alpha版本,并且涉及到其他项目特定情况(AndroidX,Kotlin),所以这不是一个普遍性的问题,但还是需要检查一下


2
为了进一步解释这个答案,您可能需要在项目的build.gradle中将classpath“io.realm:realm-gradle-plugin:5.11.0”更新为classpath“io.realm:realm-gradle-plugin:5.13.1”。 - TheScriptan

1

最近,在更新到Gradle 3.6.4(以及更高版本)后,面临了相同的问题ClassNotFoundException:未找到类“com.google.android.gms.common.R $ string”

在我的情况下,是旧的已弃用的io.mironov.smuggler插件。

似乎在大多数情况下,问题出现在旧插件上(特别是使用转换API的插件),因此要找出哪个插件首先应该尝试更新它们全部。如果这样做没有帮助,只需逐个禁用它们,直到应用程序开始运行而不出现错误。


1
据我所见,这个问题来自于项目级别的build.gradle文件中依赖版本的冲突。特别是在gradle版本更新时容易出现。
buildscript {
    ...
    dependencies {
        classpath 'com.google.gms:google-services:4.3.4'
        classpath 'com.android.tools.build:gradle:4.1.1'
        classpath "io.objectbox:objectbox-gradle-plugin:2.8.1"
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.4.1'
    }
}

当您更新gradle版本(classpath 'com.android.tools.build:gradle:4.1.1')时,请确保您的其他类路径依赖项也是最新的。

0

我从我的项目中删除了所有的Firebase和GMS依赖项,然后出现了与LeakCanary相关的错误,于是我将其删除。然后又出现了与Calligraphy3相关的错误,以此类推......我认为这是Multidex的错误。


0

我最近也遇到了同样的问题。

使用 classpath('com.google.firebase:perf-plugin:1.2.1'),禁用后问题得以解决。


-2

我曾经遇到过同样的问题,所以请确保扩展应用程序的类具有以下内容:

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

否则,您需要执行 clean - rebuild 操作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接