简体   繁体   中英

NoClassDefFoundError on Gson not working on Lollipop only

So, everything with my app was working totally fine until one day I tried to perform a test using my Lollipop 5.1 phone and it crashed when using the Retrofit and OkHttp libraries.

UncaughtException: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/gson/Gson;

The error log takes me to my Application class, to the initialization of the Gson object (the Gson class is totally there).

public static Gson getGson() { return gson; }

private void initGson() {
    gson = new Gson();
}

Many answers suggest me to use Multidex. I've already tried enabling multidex, and there's no difference:

defaultConfig {
    applicationId yourApplicationId
    minSdkVersion 16
    targetSdkVersion 22
    multiDexEnabled true
    versionCode 74
    versionName '3.0.1'
}

And on the Application class, inside the onCreate method:

MultiDex.install(getAppContext());

The app crashes exclusively on Android 5.0 and 5.1, it works fine on Kitkat , Marshmallow and Nougat .

EDIT I'll include the full build.gradle file:

apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'com.jakewharton.butterknife'


android {
signingConfigs {
    ------
}
compileSdkVersion 25
buildToolsVersion '25.0.2'
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
defaultConfig {
    applicationId yourApplicationId
    minSdkVersion 16
    targetSdkVersion 22
    multiDexEnabled true
    versionCode 74
    versionName '3.0.1'

}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}
repositories {
    jcenter()
    mavenCentral()
    maven { url "https://jitpack.io" }

}
dataBinding {
    enabled = true
}

productFlavors {
    -----
}
}
dependencies {

//noinspection GradleCompatible

//noinspection UseOfBundledGooglePlayServices

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

//compile files('libs/gson-2.2.4.jar')
compile files('libs/android-query.0.26.7.jar')
compile('com.code-troopers.betterpickers:library:2.5.4') {
    exclude group: 'com.nineoldandroids', module: 'library'
}
compile('com.github.sd6352051:NiftyNotification:1.2') {
    exclude group: 'com.nineoldandroids', module: 'library'
}
compile('com.hwangjr.rxbus:rxbus:1.0.3') {
    exclude group: 'com.hwangjr.utils', module: 'timber'
}
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:animated-vector-drawable:25.3.1'
compile 'com.android.support:support-v13:25.3.1'
compile 'com.android.support:mediarouter-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:customtabs:25.3.1'
compile 'com.android.support:multidex:1.0.1'
compile 'com.mixpanel.android:mixpanel-android:5.1.4'
compile 'com.google.android.gms:play-services:11.4.2'
compile 'com.github.traex.rippleeffect:library:1.3'
compile 'com.facebook.android:facebook-android-sdk:4.11.0'
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1'
compile 'com.github.orangegangsters:swipy:1.2.2@aar'
compile 'com.daimajia.swipelayout:library:1.2.0@aar'
compile 'com.jakewharton:butterknife:8.6.0'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.google.code.gson:gson:2.8.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.github.jjobes:slideDateTimePicker:1.0.2'
compile 'com.github.paolorotolo:appintro:4.1.0'
compile 'com.github.ybq:Android-SpinKit:1.1.0'
compile 'com.theartofdev.edmodo:android-image-cropper:2.3.1'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.joanzapata.pdfview:android-pdfview:1.0.4@aar'
compile 'me.zhanghai.android.materialprogressbar:library:1.4.1'
compile 'com.github.halysongoncalves:pugnotification:1.8.1'
compile 'biz.kasual:materialnumberpicker:1.2.1'
compile 'com.github.iwgang:simplifyspan:1.1'
compile 'uk.co.chrisjenx:calligraphy:2.3.0'
compile 'com.afollestad:sectioned-recyclerview:0.2.3'
compile 'com.marshalchen.ultimaterecyclerview:library:0.7.2'
compile 'me.relex:circleindicator:1.2.2@aar'
compile 'org.apache.commons:commons-lang3:3.4'
compile 'io.conekta:conektasdk:2.1'
compile 'com.google.zxing:core:3.2.1'
compile 'net.hockeyapp.android:HockeySDK:4.0.0'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.basgeekball:awesome-validation:2.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
}
apply plugin: 'com.google.gms.google-services'

I solved this by importing only the Google services libraries that I needed instead of the whole package, so instead of doing this:

compile 'com.google.android.gms:play-services:11.4.2'

I import each library separately. Apparently it was loading too many libraries at the same time and it caused some weird issues to pop up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM