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

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

编译器有点问题, 我在 Nexus 5 上使用的代码相同,没有错误。 当我在平板电脑中使用它时,它立即崩溃并显示错误

java.lang.NoClassDefFoundError: com.google.android.gms.R$字符串 来源不明的早午餐...

如果我删除

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

更改此行

<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 的方式的异常情况。看看 technical docs.

我相信您应该尝试将 multiDexEnabled 设为 false 并摆脱编译 'com.google.android.gms:play-services-gcm:8.4.0'。您有两个播放服务迫使您将 multiDexEnabled 设置为 true

我检查了我的代码很多次,我检查了我正在使用的每个库,我能够修复它。

首先,就像@BrainMiz 所说的,mutiDexEnabled 应该将其关闭。我只是评论它而不是将其设置为 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 播放商店的代码的错误。

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'
}

我遇到了同样的问题

有 gradle.build 个依赖项:

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

并且在 AndroidManifest.xml 中:

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

遇到同样的问题,卡了一整天。终于解决了。

添加到您的应用级别gradle.build:

repositories {
  jcenter()
}

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

然后在您的 Application class 中覆盖 attachBaseContext,如下所示:

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

并且不要忘记启用 multlidex 支持,如下所示:

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

遇到了同样的问题..只是清理项目,然后制作它,最后 运行 它

在我的例子中,我在 AndroidManifest.xml 文件中更改了我的应用程序的主题并清除了我的 styles.xml 文件(因为我是一个菜鸟,希望只使用 out-of -the-box android 主题)。启动该应用程序后,我认为是僵尸对旧主题的引用导致了错误,因为在恢复我的更改后,错误消失了。

我确实删除了

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

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