Retrofit + SimpleXMLConverter适用于Lollipop,而不适用于Kitkat

时间:2015-10-09 22:27:51

标签: android android-5.0-lollipop retrofit android-4.4-kitkat

我有一个工作正常的应用程序。但是当我开始使用CompileSDKVersion 23时(由于工具栏没有在Lollipop中显示,我从22上升了)。现在它适用于Lollipop设备,但是当我在Kitkat上运行时崩溃。

错误:

java.lang.NoClassDefFoundError: retrofit.converter.SimpleXMLConverter
        at com.app.appname.api.AppAPI.<init>(AppAPI.java:80)
        at com.app.appname.api.AppAPI.getInstance(AppAPI.java:95)
        at com.app.appname.broker.DataBroker.<init>(DataBroker.java:31)
        at com.app.appname.broker.DataBroker.init(DataBroker.java:35)
        at com.app.appname.AppApplication.onCreate(AppApplication.java:18)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4417)
        at android.app.ActivityThread.access$1500(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5105)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
        at dalvik.system.NativeStart.main(Native Method)

我正在尝试初始化Retrofit API:

mAdapter = new RestAdapter.Builder()
            .setEndpoint(...)
            .setConverter(new SimpleXMLConverter())
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .build();

我的Gradle看起来像这样:

apply plugin: 'com.android.application'

repositories {
    mavenCentral()

    maven {
        url "https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/"
    }
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.app.appname"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "0.0.0.1"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:23.0.1'
    compile 'com.android.support:support-annotations:23.0.1'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'

    compile 'com.squareup.okhttp:okhttp-urlconnection:2.5.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile('com.squareup.retrofit:converter-simplexml:1.9.0'){
        exclude module: 'stax'
        exclude module: 'stax-api'
        exclude module: 'xpp3'
    }

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

    compile 'com.google.code.gson:gson:2.3.1'

    compile 'com.squareup.picasso:picasso:2.5.2'

    compile 'com.facebook.android:facebook-android-sdk:4.1.0'

    compile 'com.google.zxing:core:2.2'
    compile 'com.embarkmobile:zxing-android-minimal:1.2.1@aar'
}

我试过了:

  1. 将JCenter()添加到repo的
  2. 包括GSon库
  3. 将所有活动移至单个包并在清单中设置包名称
  4. 查找注释(我在首选项 - &gt; Build - &gt; Compiler下找不到它)
  5. 部署到具有相同API级别(4.2.2)的其他设备

1 个答案:

答案 0 :(得分:2)

我使用的是几乎相同的设置,包括:

改装1.9.0
SimpleXMLConverter 1.9.0
compileSdkVersion 23
targetSdkVersion 23
buildToolsVersion 23.0.1

我的应用程序工作正常,一直到API级别16(我的最低限度)。

我可以看到可能导致问题的主要区别是:

1)从SimpleXMLConverter中排除stax,stax-api和xpp3。我记得当我开始使用它时,我还要把它们排除在外,因为我有一些问题,但我不再需要排除它们,也许试着删除排除并看看?

2)包括Okhttp-urlconnection。我知道Retrofit会自动使用它,如果它包含在内,可能会引起问题(只是一个猜测)。

3)启用Multidex。我自己并没有弄乱这个,但是我已经在StackOverflow上阅读了它的问题,如果可能的话,尝试禁用它并看到?可能最好启用minify而不是运行multidex,以删除一些不需要的函数。