java.lang.NoClassDefFoundError: com.google.android

2019-02-22 09:11发布

问题:

I am having a little trouble with the complier, Same code I use on Nexus 5, no error. as Soon as I use it in Tablet, it crash right away and the error said

java.lang.NoClassDefFoundError: com.google.android.gms.R$string with brunch of unknown source...

and if i remove

multiDexEnabled true 

and remove

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

then it works on both, does anybody know the reason why? Below is my 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'

}

Blow is the 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

回答1:

I look into my code many time, and I look at each library I am using and I was able to fix it.

First, like @BrainMiz said mutiDexEnabled should set it off. I just comment it instead of set it as false.

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

Second, it is the dependencies. Since I don't have any jar in my libs folder I remove

  compile fileTree(dir: 'libs', include: ['*.jar'])

also remove all not being used gms library, only add the one that being used. I have to give some credits to @Radix because I did found some error in my code regarding to the code that where I check if the device has google play store.

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:

Change this line

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

with this

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

Also i see a lot of anomaly in your manifest with respect to how you have declared GCM . Have a look at the technical docs.



回答3:

I believe you should try turning multiDexEnabled false and get rid of the compile 'com.google.android.gms:play-services-gcm:8.4.0' . You have two play-services which forces you to turn multiDexEnabled to true



回答4:

Got the same problem.. just clean the project, then make it and finally run it



回答5:

In my case, I changed the theme of my app in the AndroidManifest.xml file and cleared out my styles.xml file (because I'm a noob and hoped to just use an out-of-the-box android theme). After launching the app, I assume there were zombie references to the old theme that caused the error because, after reverting my changes, the error disappeared.



回答6:

I did remove

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

and sync project. After added this line and synced again. Next, cleaned project to remove old dex files (may cause problem with dex archives merging), and it helped.



回答7:

I had the same issue

Had in gradle.build dependencies :

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

And in AndroidManifest.xml :

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


回答8:

Having same issue and was stuck for a whole day. Finally got solution.

Add in your app level gradle.build:

repositories {
  jcenter()
}

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

Then override attachBaseContext in your Application class like below:

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

And don't forgot to enable mutlidex support as below:

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