简体   繁体   English

单元测试的测试用例成功,但使用 NoSuchMethodError 的 Instrumented Test 失败

[英]Test case success with Unit test but fails with Instrumented Test with NoSuchMethodError

I have a java maven library that I want to use in my Android app ( This is the library) When I try to initiate a class like ZoldWts api = new RestfulZoldWts("key");我有一个 java maven 库,我想在我的 Android 应用程序中使用它(是库)当我尝试启动像ZoldWts api = new RestfulZoldWts("key"); I get NoSuchMethodError.我得到 NoSuchMethodError。

java.lang.NoSuchMethodError: No direct method (Ljava/util/Collection;)V in class Lorg/apache/http/client/protocol/RequestDefaultHeaders; java.lang.NoSuchMethodError: 在类 Lorg/apache/http/client/protocol/RequestDefaultHeaders 中没有直接方法 (Ljava/util/Collection;)V; or its super classes (declaration of 'org.apache.http.client.protocol.RequestDefaultHeaders' appears in /system/framework/org.apache.http.legacy.boot.jar)或其超类('org.apache.http.client.protocol.RequestDefaultHeaders' 的声明出现在 /system/framework/org.apache.http.legacy.boot.jar 中)

How did I add the library to my grade?我如何将图书馆添加到我的成绩中? compile 'com.amihaiemil.web:zold-java-client:0.0.1' However, I got the following error compile 'com.amihaiemil.web:zold-java-client:0.0.1'但是,我收到以下错误

More than one file was found with OS independent path 'META-INF/DEPENDENCIES'发现多个文件与操作系统无关的路径“META-INF/DEPENDENCIES”

I solved it by adding this to my gradle我通过将其添加到我的 gradle 中解决了这个问题

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
}

To make sure that the library isn't broken I wrote a unit test and it runs successfully.为了确保库没有损坏,我编写了一个单元测试并成功运行。 unfortunately, the Instrumented test didn't.不幸的是,仪器测试没有。 I tried disabling proguard by useProguard false didn't work.我尝试通过useProguard false禁用 proguard 没有用。 I also tried adding the following to my proguard rules我还尝试将以下内容添加到我的 proguard 规则中

-keep class org/apache/http/client/protocol/.* { *; -保持类org/apache/http/client/protocol/.* { *; } }

Still didn't work.还是没用。

My gradle file:我的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'
}

My test code (The exact same code works in the unit test but not in the instrumented test)我的测试代码(完全相同的代码在单元测试中有效,但在插桩测试中无效)

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

from here这里

in build.gradle android section put this code在 build.gradle android 部分放置此代码

useLibrary 'org.apache.http.legacy'

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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