Gson上的NoClassDefFoundError不仅仅适用于Lollipop

时间:2017-10-20 17:04:45

标签: java android retrofit okhttp multidex

所以,我的应用程序的所有内容都工作得很好,直到有一天我尝试使用我的Lollipop 5.1手机进行测试,并在使用改造 OkHttp 时崩溃库。

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

错误日志将我带到我的Application类,初始化Gson对象(Gson类完全在那里)。

public static Gson getGson() { return gson; }

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

很多答案都建议我使用Multidex。我已经尝试过启用multidex,但没有区别:

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

在Application类中,在onCreate方法中:

MultiDex.install(getAppContext());

该应用程序仅在Android 5.0和5.1上崩溃,它可以在 Kitkat Marshmallow Nougat 上正常运行。

编辑我将包含完整的build.gradle文件:

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'

1 个答案:

答案 0 :(得分:0)

我通过仅导入所需的Google服务库而不是整个软件包来解决此问题,所以不要这样做:

selectAll () { this.notify.emit(this.names); }

我分别导入每个库。显然,它同时加载了太多的库,这引发了一些奇怪的问题。