Unable to Setup Wiremock on Android Project with P

2019-06-28 02:47发布

问题:

I've been reading all posts now and at this time I'm really struggling.

I was trying to use WireMock in my android project to help on testing. However, Proguard has always something to say.

My gradle dependencies file is this:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')

// Fine Tuned Google Play Services
compile "com.google.android.gms:play-services-ads:${PLAY_SERVICES_VERSION}"
compile "com.google.android.gms:play-services-plus:${PLAY_SERVICES_VERSION}"
compile "com.google.android.gms:play-services-identity:${PLAY_SERVICES_VERSION}"
compile "com.google.android.gms:play-services-location:${PLAY_SERVICES_VERSION}"
// we need base for "Google Cloud Messaging"
compile "com.google.android.gms:play-services-base:${PLAY_SERVICES_VERSION}"
// Android Wear
compile "com.google.android.gms:play-services-wearable:${PLAY_SERVICES_VERSION}"
compile 'pl.tajchert:exceptionwear:0.1.2'

// Handheld application
compile 'de.greenrobot:greendao:1.3.7'
compile "com.android.support:support-v13:21.0.3"
compile "com.android.support:appcompat-v7:${SUPPORT_LIB_VERSION}"
compile "com.android.support:support-v4:${SUPPORT_LIB_VERSION}"
compile "com.android.support:support-annotations:${SUPPORT_LIB_VERSION}"
compile 'de.hdodenhof:circleimageview:1.2.2'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.3.0'
compile 'com.squareup.okhttp:okhttp:2.3.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup:otto:1.3.6'
compile 'com.github.johnkil.android-robototextview:robototextview:2.4.0'
compile 'com.mixpanel.android:mixpanel-android:4.5.3@aar'
compile('com.crashlytics.sdk.android:crashlytics:2.2.3@aar') {
    transitive = true;
}
compile 'com.facebook.android:facebook-android-sdk:4.0.1'
compile "com.hannesdorfmann.fragmentargs:annotation:${FRAGARGS_VERSION}"
apt "com.hannesdorfmann.fragmentargs:processor:${FRAGARGS_VERSION}"
compile 'com.sothree.slidinguppanel:library:3.0.0'
compile 'com.adjust.sdk:adjust-android:4.0.4'
compile 'commons-io:commons-io:2.4'
compile 'org.apache.commons:commons-math3:3.5'
compile 'se.emilsjolander:stickylistheaders:2.6.0'
compile 'com.melnykov:floatingactionbutton:1.3.0'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.afollestad:material-dialogs:0.7.5.1'
provided 'javax.annotation:jsr250-api:1.0' // to enable guava proguard

// only on debug
compile 'com.facebook.stetho:stetho:1.1.1'
compile 'com.facebook.stetho:stetho-okhttp:1.1.1'

// Dagger related dependencies
compile 'com.google.dagger:dagger:2.0'
apt 'com.google.dagger:dagger-compiler:2.0'
provided 'org.glassfish:javax.annotation:10.0-b28'

// androidTest dependencies
androidTestCompile 'com.google.dagger:dagger:2.0'
androidTestApt 'com.google.dagger:dagger-compiler:2.0'
androidTestProvided 'org.glassfish:javax.annotation:10.0-b28'

androidTestCompile('org.mockito:mockito-core:1.10.19') {
    exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
androidTestCompile('com.google.dexmaker:dexmaker-mockito:1.2') {
    exclude group: 'org.mockito', module: 'mockito-core'
}
androidTestCompile ('com.squareup.assertj:assertj-android:1.0.0') {
    exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile 'com.squareup.spoon:spoon-client:1.1.2'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
androidTestCompile ('com.github.tomakehurst:wiremock:1.56:standalone') {
    exclude group: 'org.json', module: 'json'
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    exclude group: 'commons-io', module: 'commons-io'
    exclude group: 'commons-cli', module: 'commons-cli'
}

// Unit testing dependencies
testCompile 'junit:junit:4.12'
testCompile('org.mockito:mockito-core:1.10.19') {
    exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
}

My testing proguard-file is this:

# Proguard rules run against the test module (split since Gradle plugin v 1.1.0)
-dontobfuscate
-ignorewarnings

# Specific classes that common test libs warn about
-dontwarn java.beans.**
-dontwarn javax.lang.model.element.Modifier
-dontwarn org.apache.tools.ant.**
-dontwarn org.assertj.core.internal.cglib.asm.util.TraceClassVisitor
-dontwarn org.easymock.**
-dontwarn org.jmock.core.**
-dontwarn org.w3c.dom.bootstrap.**
-dontwarn sun.misc.Unsafe
-dontwarn sun.reflect.**
-dontwarn wiremock.**
-dontwarn org.mockito.**
-dontwarn org.custommonkey.**
-dontwarn android.test.**
-dontwarn com.jayway.**
-dontwarn android.support.test.**
-dontwarn org.skyscreamer.**
-dontwarn org.slf4j.**
-dontwarn com.github.tomakehurst.wiremock.http.ResponseDefinition
-dontwarn com.google.common.net.InternetDomainName

My main project dependencies:

+--- com.google.android.gms:play-services-ads:7.0.0
|    +--- com.google.android.gms:play-services-base:7.0.0
|    |    \--- com.android.support:support-v4:22.0.0 -> 22.2.0
|    |         \--- com.android.support:support-annotations:22.2.0 -> 22.1.1
|    \--- com.google.android.gms:play-services-analytics:7.0.0
|         \--- com.google.android.gms:play-services-base:7.0.0 (*)
+--- com.google.android.gms:play-services-plus:7.0.0
|    \--- com.google.android.gms:play-services-base:7.0.0 (*)
+--- com.google.android.gms:play-services-identity:7.0.0
|    \--- com.google.android.gms:play-services-base:7.0.0 (*)
+--- com.google.android.gms:play-services-location:7.0.0
|    +--- com.google.android.gms:play-services-base:7.0.0 (*)
|    \--- com.google.android.gms:play-services-maps:7.0.0
|         \--- com.google.android.gms:play-services-base:7.0.0 (*)
+--- com.google.android.gms:play-services-base:7.0.0 (*)
+--- com.google.android.gms:play-services-wearable:7.0.0
|    \--- com.google.android.gms:play-services-base:7.0.0 (*)
+--- pl.tajchert:exceptionwear:0.1.2
+--- de.greenrobot:greendao:1.3.7
+--- com.android.support:support-v13:21.0.3
|    \--- com.android.support:support-v4:21.0.3 -> 22.2.0 (*)
+--- com.android.support:appcompat-v7:22.1.1 -> 22.2.0
|    \--- com.android.support:support-v4:22.2.0 (*)
+--- com.android.support:support-v4:22.1.1 -> 22.2.0 (*)
+--- com.android.support:support-annotations:22.1.1
+--- de.hdodenhof:circleimageview:1.2.2
+--- com.squareup.retrofit:retrofit:1.9.0
|    \--- com.google.code.gson:gson:2.3.1
+--- com.squareup.okhttp:okhttp-urlconnection:2.3.0
|    \--- com.squareup.okhttp:okhttp:2.3.0
|         \--- com.squareup.okio:okio:1.3.0
+--- com.squareup.okhttp:okhttp:2.3.0 (*)
+--- com.squareup.picasso:picasso:2.5.2
+--- com.squareup:otto:1.3.6
+--- com.github.johnkil.android-robototextview:robototextview:2.4.0
|    \--- com.android.support:appcompat-v7:22.1.0 -> 22.2.0 (*)
+--- com.mixpanel.android:mixpanel-android:4.5.3
+--- com.crashlytics.sdk.android:crashlytics:2.2.3
|    +--- com.crashlytics.sdk.android:answers:1.1.2
|    |    \--- io.fabric.sdk.android:fabric:1.2.0 -> 1.3.0
|    +--- io.fabric.sdk.android:fabric:1.3.0
|    \--- com.crashlytics.sdk.android:beta:1.1.2
|         \--- io.fabric.sdk.android:fabric:1.2.0 -> 1.3.0
+--- com.facebook.android:facebook-android-sdk:4.0.1
|    +--- com.android.support:support-v4:[21,22) -> 22.2.0 (*)
|    \--- com.parse.bolts:bolts-android:1.1.4
+--- com.hannesdorfmann.fragmentargs:annotation:2.1.0
+--- com.sothree.slidinguppanel:library:3.0.0
|    +--- com.android.support:support-v4:19.1.0 -> 22.2.0 (*)
|    \--- com.nineoldandroids:library:2.4.0
+--- com.adjust.sdk:adjust-android:4.0.4
+--- commons-io:commons-io:2.4
+--- org.apache.commons:commons-math3:3.5
+--- se.emilsjolander:stickylistheaders:2.6.0
+--- com.melnykov:floatingactionbutton:1.3.0
|    +--- com.android.support:support-annotations:21.0.3 -> 22.1.1
|    +--- com.android.support:recyclerview-v7:21.0.3 -> 22.2.0
|    |    +--- com.android.support:support-annotations:22.2.0 -> 22.1.1
|    |    \--- com.android.support:support-v4:22.2.0 (*)
|    \--- com.nineoldandroids:library:2.4.0
+--- com.jakewharton:butterknife:6.1.0
+--- com.afollestad:material-dialogs:0.7.5.1
|    +--- com.android.support:support-v4:22.2.0 (*)
|    +--- com.android.support:appcompat-v7:22.2.0 (*)
|    +--- com.android.support:recyclerview-v7:22.2.0 (*)
|    \--- com.android.support:support-annotations:22.2.0 -> 22.1.1
+--- com.facebook.stetho:stetho:1.1.1
|    +--- commons-cli:commons-cli:1.2
|    \--- com.google.code.findbugs:jsr305:2.0.1
+--- com.facebook.stetho:stetho-okhttp:1.1.1
|    +--- com.squareup.okhttp:okhttp:2.2.0 -> 2.3.0 (*)
|    +--- com.facebook.stetho:stetho:1.1.1 (*)
|    \--- com.google.code.findbugs:jsr305:2.0.1
+--- com.google.dagger:dagger:2.0
|    \--- javax.inject:javax.inject:1
+--- :pullToRefresh:
+--- project :PhotoViewLib
|    \--- com.android.support:support-v4:22.0.0 -> 22.2.0 (*)
+--- project :h19sharedsources
|    +--- com.google.android.gms:play-services-wearable:6.+ -> 7.0.0 (*)
|    \--- com.android.support:support-annotations:21.0.3 -> 22.1.1
+--- project :h19base
+--- javax.annotation:jsr250-api:1.0
\--- org.glassfish:javax.annotation:10.0-b28

My test project dependencies:

+--- com.google.dagger:dagger:2.0
|    \--- javax.inject:javax.inject:1
+--- org.mockito:mockito-core:1.10.19
|    \--- org.objenesis:objenesis:2.1
+--- com.google.dexmaker:dexmaker-mockito:1.2
|    \--- com.google.dexmaker:dexmaker:1.2
+--- com.squareup.assertj:assertj-android:1.0.0
|    \--- org.assertj:assertj-core:1.6.1
+--- com.squareup.spoon:spoon-client:1.1.2
+--- com.android.support.test.espresso:espresso-core:2.1
|    +--- com.android.support.test:rules:0.2
|    |    \--- com.android.support.test:runner:0.2
|    |         +--- junit:junit-dep:4.10
|    |         |    \--- org.hamcrest:hamcrest-core:1.1
|    |         +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
|    |         \--- com.android.support:support-annotations:22.0.0 -> 22.1.1
|    +--- com.squareup:javawriter:2.1.1
|    +--- org.hamcrest:hamcrest-integration:1.1
|    |    \--- org.hamcrest:hamcrest-core:1.1
|    +--- com.android.support.test.espresso:espresso-idling-resource:2.1
|    +--- org.hamcrest:hamcrest-library:1.1
|    |    \--- org.hamcrest:hamcrest-core:1.1
|    +--- javax.inject:javax.inject:1
|    +--- com.google.code.findbugs:jsr305:2.0.1
|    +--- com.android.support.test:runner:0.2 (*)
|    +--- javax.annotation:javax.annotation-api:1.2
|    \--- org.hamcrest:hamcrest-core:1.1
   \--- com.github.tomakehurst:wiremock:1.56
     +--- org.slf4j:slf4j-api:1.7.6
     +--- xmlunit:xmlunit:1.5
     +--- org.mortbay.jetty:jetty:6.1.26
     |    +--- org.mortbay.jetty:jetty-util:6.1.26
     |    \--- org.mortbay.jetty:servlet-api:2.5-20081211
     +--- org.skyscreamer:jsonassert:1.2.3
     +--- com.jayway.jsonpath:json-path:0.8.1
     |    +--- net.minidev:json-smart:1.1.1
     |    \--- commons-lang:commons-lang:2.6
     +--- com.fasterxml.jackson.core:jackson-annotations:2.4.2
     +--- com.fasterxml.jackson.core:jackson-core:2.4.2
     +--- com.google.guava:guava:18.0
     +--- net.sf.jopt-simple:jopt-simple:4.7
     \--- com.fasterxml.jackson.core:jackson-databind:2.4.2
          +--- com.fasterxml.jackson.core:jackson-annotations:2.4.0 -> 2.4.2
          \--- com.fasterxml.jackson.core:jackson-core:2.4.2

My error output:

Note: there were 149 duplicate class definitions.
      (http://proguard.sourceforge.net/manual/troubleshooting.html#duplicateclass)
Warning:can't write resource [META-INF/LICENSE.txt] (Duplicate zip entry [commons-lang-2.6.jar:META-INF/LICENSE.txt])
Warning:can't write resource [META-INF/maven/org.mortbay.jetty/jetty/pom.properties] (Duplicate zip entry [wiremock-1.56-standalone.jar:META-INF/maven/org.mortbay.jetty/jetty/pom.properties])
Warning:can't write resource [META-INF/maven/org.mortbay.jetty/jetty/pom.xml] (Duplicate zip entry [wiremock-1.56-standalone.jar:META-INF/maven/org.mortbay.jetty/jetty/pom.xml])
Warning:can't write resource [META-INF/maven/com.google.guava/guava/pom.properties] (Duplicate zip entry [wiremock-1.56-standalone.jar:META-INF/maven/com.google.guava/guava/pom.properties])
Warning:can't write resource [META-INF/maven/com.google.guava/guava/pom.xml] (Duplicate zip entry [wiremock-1.56-standalone.jar:META-INF/maven/com.google.guava/guava/pom.xml])
Warning:can't write resource [META-INF/LICENSE] (Duplicate zip entry [wiremock-1.56-standalone.jar:META-INF/LICENSE])
Warning:can't write resource [META-INF/NOTICE] (Duplicate zip entry [wiremock-1.56-standalone.jar:META-INF/NOTICE])
Warning:can't write resource [META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.properties] (Duplicate zip entry [wiremock-1.56-standalone.jar:META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.properties])
Warning:can't write resource [META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.xml] (Duplicate zip entry [wiremock-1.56-standalone.jar:META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.xml])
Warning:can't write resource [META-INF/services/com.fasterxml.jackson.core.JsonFactory] (Duplicate zip entry [wiremock-1.56-standalone.jar:META-INF/services/com.fasterxml.jackson.core.JsonFactory])
Warning:can't write resource [META-INF/LICENSE] (Duplicate zip entry [wiremock-1.56-standalone.jar:META-INF/LICENSE])
Warning:can't write resource [META-INF/NOTICE] (Duplicate zip entry [wiremock-1.56-standalone.jar:META-INF/NOTICE])
Warning:can't write resource [META-INF/LICENSE] (Duplicate zip entry [wiremock-1.56-standalone.jar:META-INF/LICENSE])
Warning:can't write resource [META-INF/NOTICE] (Duplicate zip entry [wiremock-1.56-standalone.jar:META-INF/NOTICE])
:app:proguardStagingDebugAndroidTest FAILED
Error:Execution failed for task ':app:proguardStagingDebugAndroidTest'.
> java.io.IOException: Can't write [/Users/fabiocarballo/h19-android/app/build/intermediates/classes-proguard/androidTest/staging/debug/classes.jar] (Can't read [/Users/fabiocarballo/.gradle/caches/modules-2/files-2.1/com.github.tomakehurst/wiremock/1.56/4f6c96ea684cfa6375bbd7d2c2e4c8a47ccf615d/wiremock-1.56-standalone.jar(;;;;;;!META-INF/MANIFEST.MF)] (Duplicate zip entry [wiremock-1.56-standalone.jar:org/custommonkey/xmlunit/AbstractNodeTester.class]))

Thank you for all. Any help will be very welcome!

回答1:

Do not try to wrestle with Wiremock and Android studio, Instead start wiremock as a parallel process independently.

Download this code base

https://github.com/nenick/android-gradle-template

run Scripts/run-tests.sh from project root

Then run your project.

Whenever you want to customize the response try to make restful calls to admin panel of wiremock

Search for the word "curl" in http://wiremock.org/running-standalone.html, make that call (via HttpClient or AndroidAnnotations REST) from expresso or robolectric tests or running application

You can actually start wiremock from gradle (suppose before connected tests)

task startWire(type: Exec) {
        commandLine 'sh', '..\\Scripts\\start-wiremock.sh','&'
    }



    tasks.whenTaskAdded { task ->
        if (task.name == 'assembleDebugAndroidTest') {
            task.dependsOn startWire
        }
    }


回答2:

WireMock now works in an Android app in Android Studio as of January 2016. This is fixed as of a couple of weeks ago with WireMock 2.0.8-beta from the 2.0-beta branch. I've updated that WireMock GitHub issue and have created a sample project showing it working.

Here are the build.gradle dependencies you'll need to use it:

androidTestCompile("com.github.tomakehurst:wiremock:2.0.8-beta") {
    //Allows us to use the Android version of Apache httpclient
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'

    //Resolves the Duplicate Class Exception
    //Error:Execution failed for task ':app:transformClassesWithJarMergingForDebugAndroidTest'.
    //       > com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/objectweb/asm/AnnotationVisitor.class
    exclude group: 'asm', module: 'asm'

    //Fixes conflict with Android's version
    //Warning:Dependency org.json:json:20090211 is ignored for debugAndroidTest as it may be conflicting with the internal version provided by Android.
    //In case of problem, please repackage with jarjar to change the class packages
    exclude group: 'org.json', module: 'json'
}
androidTestCompile 'org.apache.httpcomponents:httpclient-android:4.3.5+'

Related Stack Overflow question: NoSuchMethodError when using WireMock in an Android integration test