java.lang.NoClassDefFoundError: com.google.android.gms.R$string Java.lang.NoClassDefFoundError: com.google.android.gms.R$string

6
我在编译器方面遇到了一些问题, 我在 Nexus 5 上使用的代码没有错误。 但是只要我在平板电脑上使用它,它就会立即崩溃,并出现以下错误消息:
java.lang.NoClassDefFoundError: com.google.android.gms.R$string with brunch of unknown source...
如果我删除掉标签,则会出现更多的错误。
multiDexEnabled true 

并删除

 compile 'org.twitter4j:twitter4j-core:4.0.2'

然后它在两个上都可以工作,有人知道原因吗?下面是我的build.grade

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "com.package.name"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
     multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'

compile 'org.twitter4j:twitter4j-core:4.0.2'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.android.support:design:23.1.1'
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.isseiaoki:simplecropview:1.0.8'
compile 'com.qozix:tileview:2.0.7'
compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.google.android.gms:play-services-gcm:8.4.0'

}

以下是manifest.xml文件的内容:
  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package.name" >

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission
    android:name="com.google.android.c2dm.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<application
    android:name=".utility.Apps"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:largeHeap="true"
    android:theme="@style/AppTheme" >
    <receiver
        android:name=".gcm.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </receiver>

    <service android:name=".gcm.GcmIntentService" />

    <activity android:name=".SplashActivity"
              android:theme="@style/AppTheme.NoActionBar" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    and then a lot of activities

保持所有内容不变,并将minifyEnabled设置为false。 - Nishant Srivastava
@Radix 我刚把minifyEnabled设置为false。但是还是出现了同样的错误。只要我排除twitter4J jar文件,就没问题了。但是我需要twitter 4j..... - NOT_A_PROGRAMMER
你能发布你的堆栈跟踪和清单文件吗? - Nishant Srivastava
8个回答

5

我多次查看了我的代码,并查看我所使用的每个库,最终我成功修复了问题。

首先,像@BrainMiz所说的那样,应该将mutiDexEnabled设置为false。我只是将其注释掉而不是将其设置为false。

defaultConfig {
     applicationId "com.package.name"
     minSdkVersion 16
     targetSdkVersion 23
     versionCode 1
     versionName "1.0"
     //multiDexEnabled true
}

其次,是依赖关系。由于我在我的 libs 文件夹中没有任何 jar 包,因此我删除了它。
  compile fileTree(dir: 'libs', include: ['*.jar'])

同时删除所有未使用的gms库,只添加正在使用的库。我必须感谢@Radix,因为我发现了代码中的一些错误,这些错误涉及到检查设备是否具有Google Play商店的代码。

dependencies {
    //compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

    compile 'org.twitter4j:twitter4j-core:4.0.2'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
    //compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.android.support:design:23.1.1'
    compile 'com.squareup.okhttp:okhttp:2.5.0'
    //compile 'com.android.support:support-v4:23.1.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.isseiaoki:simplecropview:1.0.8'
    compile 'com.qozix:tileview:2.0.7'
    compile 'com.android.support:cardview-v7:23.1.1'
    compile 'com.google.android.gms:play-services-gcm:8.4.0'
}

2

我认为您应该尝试将multiDexEnabled设置为false,并删除compile 'com.google.android.gms:play-services-gcm:8.4.0'。您有两个play-services库,这迫使您将multiDexEnabled设置为true。


是的,你说得对,我这么做是因为我看到了其他帖子中的答案。我只是找到了解决方法。我将发布答案。 - NOT_A_PROGRAMMER

1

改变这一行

<uses-permission
android:name="com.google.android.c2dm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />

使用这个

<permission android:name="<your-package-name>.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="<your-package-name>.permission.C2D_MESSAGE" />

此外,我注意到您的清单文件中有许多关于如何声明GCM的异常。请查看技术文档

0

我已经删除

    compile 'com.android.support:multidex:1.0.1' 

同步项目。添加这行代码后再次同步。接下来,清理项目以删除旧的dex文件(可能会导致dex归档合并问题),这有所帮助。

0
在我的情况下,我在AndroidManifest.xml文件中更改了应用程序的主题,并清空了我的styles.xml文件(因为我是一个新手,希望只使用一个开箱即用的Android主题)。启动应用程序后,我假设存在指向旧主题的僵尸引用,导致错误发生,因为在恢复更改后,错误消失了。

0

我遇到了同样的问题...只需清理项目,然后构建它,最后运行它即可


-1

我遇到了同样的问题

在 gradle.build 的依赖中有:

compile 'com.android.support:multidex:1.0.0'

在 AndroidManifest.xml 中:

<application
        ...
        android:name="android.support.multidex.MultiDexApplication">

-1

我也遇到了同样的问题,卡了一整天。最终找到了解决方案。

在你的应用级别的gradle.build中添加:

repositories {
  jcenter()
}

dependencies {
  compile 'com.google.android:multidex:0.1'
}

然后在您的类中重写attachBaseContext,如下所示:

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

而且不要忘记启用multidex支持,如下所示:

android {
    ...
    defaultConfig {
        ...
        multiDexEnabled true
    }
}

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