单元测试的测试用例成功,但带有NoSuchMethodError

时间:2019-01-26 08:12:47

标签: java android junit

我有一个要在我的Android应用中使用的Java Maven库(This是该库) 当我尝试像 ZoldWts api = new RestfulZoldWts("key"); 我收到NoSuchMethodError。

  

java.lang.NoSuchMethodError:类Lorg / apache / http / client / protocol / RequestDefaultHeaders中没有直接方法(Ljava / util / Collection;)V;或它的超类(“ org.apache.http.client.protocol.RequestDefaultHeaders”的声明出现在/system/framework/org.apache.http.legacy.boot.jar中)

如何将库添加到我的成绩中? compile 'com.amihaiemil.web:zold-java-client:0.0.1' 但是,出现以下错误

  

发现有多个文件具有与操作系统无关的路径“ META-INF / DEPENDENCIES”

我通过将它添加到我的gradle中来解决了它

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
}

为确保库未损坏,我编写了一个单元测试,它成功运行。不幸的是,仪器测试没有。 我尝试通过useProguard false禁用proguard无效。 我还尝试将以下内容添加到我的保障规则中

  

-keep类org / apache / http / client / protocol/。*{*; }

还是没用。

我的gradle文件:

apply plugin: 'com.android.application'

android {
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
    }
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.zold.ammar.zold_android"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    // Zold Java
    compile 'com.amihaiemil.web:zold-java-client:0.0.1'
}

我的测试代码(完全相同的代码在单元测试中有效,而在仪表化测试中无效)

@Test
public void zoldinittest() {
    ZoldWts api = new RestfulZoldWts("key");
    assertNotNull(api);
}

2 个答案:

答案 0 :(得分:0)

来自here

在build.gradle android部分中放置此代码

useLibrary 'org.apache.http.legacy'

答案 1 :(得分:0)

通过将zold-java-client中的Apache版本更改为5.x来解决问题 ThisThis问题显示了所有讨论,该解决方案在该库的0.0.2版中可用。