【问题标题】:Test case success with Unit test but fails with Instrumented Test with NoSuchMethodError单元测试的测试用例成功,但使用 NoSuchMethodError 的仪器测试失败
【发布时间】:2019-01-26 08:12:47
【问题描述】:

我有一个要在我的 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 无效。 我还尝试将以下内容添加到我的 proguard 规则中

-keep class 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);
}

【问题讨论】:

    标签: java android junit


    【解决方案1】:

    来自here

    在 build.gradle android 部分放这段代码

    useLibrary 'org.apache.http.legacy'

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2015-06-24
        • 2015-11-19
        • 1970-01-01
        • 2020-08-07
        • 1970-01-01
        • 2012-02-28
        • 2023-03-27
        • 2019-09-06
        • 1970-01-01
        相关资源
        最近更新 更多