簡體   English   中英

java.lang.NoClassDefFoundError: com.google.android.gms.R$字符串

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

我在編譯器上遇到了一些麻煩,我在 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

我多次查看我的代碼,查看我正在使用的每個庫,我能夠修復它。

首先,就像@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,因為我確實在我的代碼中發現了一些關於我檢查設備是否有谷歌游戲商店的代碼的錯誤。

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

我相信您應該嘗試將 multiDexEnabled 設為 false 並擺脫編譯“com.google.android.gms:play-services-gcm:8.4.0”。 您有兩個播放服務,它們迫使您將 multiDexEnabled 設置為 true

改變這一行

<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 的異常情況。 看看技術文檔。

遇到同樣的問題..只是清理項目,然后制作它並最后運行它

在我的例子中,我在 AndroidManifest.xml 文件中更改了我的應用程序的主題並清除了我的 styles.xml 文件(因為我是一個菜鳥,希望只使用開箱即用的 android 主題)。 啟動該應用程序后,我假設存在對導致錯誤的舊主題的僵屍引用,因為在恢復我的更改后,錯誤消失了。

我確實刪除了

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

和同步項目。 添加此行並再次同步后。 接下來,清理項目以刪除舊的 dex 文件(可能會導致 dex 存檔合並出現問題),這很有幫助。

我正在使用Xamarin android。 只需清理解決方案並嘗試重建項目,然后運行您的應用程序即可。 這對我有用。

我遇到過同樣的問題

在 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類中覆蓋attachBaseContext ,如下所示:

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

並且不要忘記啟用 mutlidex 支持,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM