在android中,主机PC可以在ip 10.0.2.2上访问.有没有一种方法可以让通过USB插入的设备访问主机



在android中,主机可以在ip 10.0.2.2上访问(根据此文档)。是否有通过USB插入的设备访问主机的方法?

上下文是,作为gradle构建过程的一部分,我正在运行一个wiremock服务器,以帮助测试。Wiremock在没有大量工作的情况下无法在android中运行,因此在主机PC上运行它似乎是一个简单得多的选项。

WireMock自2016年1月起在安卓应用程序中运行,无需执行任何特殊操作这个问题在几周前通过2.0版本的WireMock 2.0.8-beta版本修复。我已经更新了WireMock GitHub的问题,并创建了一个示例项目来展示它的工作原理。

这使您不必担心通过USB连接到主机。您可以在Android设备上运行WireMock服务器并连接到localhost。

以下是您需要使用的build.gradle依赖项:

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+'

最新更新