新的 MockWebServer 失败并显示 java.lang.NoClassDefFoundError ( TaskRunner$RealBackend )

new MockWebServer fails with java.lang.NoClassDefFoundError ( TaskRunner$RealBackend )

我正在尝试使用 okhttp3.mockwebserver.MockWebServer 在 Android 上进行一些仪器测试。这是我的 build.gradle (:app) 文件中用于仪器测试的内容:

    androidTestImplementation 'androidx.annotation:annotation:1.1.0'
    androidTestImplementation 'androidx.test:runner:1.3.0'
    androidTestUtil 'androidx.test:orchestrator:1.3.0'
    androidTestImplementation 'androidx.test:runner:1.3.0'
    androidTestImplementation 'androidx.test:rules:1.3.0'
    androidTestImplementation 'androidx.test:core:1.3.0'
    androidTestImplementation 'org.mockito:mockito-android:2.7.22'
    androidTestImplementation 'com.squareup.okhttp3:mockwebserver:4.9.0'

这是我在 andoidTest/java 文件夹中的 java 文件:

import android.content.Intent;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;

import org.junit.Rule;
import org.junit.Test;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;


public class MainActivityTest {

  MockWebServer server = new MockWebServer();

  @Rule
  public ActivityTestRule<MainActivity> activityRule
      = new ActivityTestRule<>(MainActivity.class);

  @Test
  public void testNoSessionData() {
    try {
      server.enqueue(new MockResponse().setBody("[]").setResponseCode(200));
      server.start(8080);
      activityRule.launchActivity(
          new Intent(
              InstrumentationRegistry.getInstrumentation().getContext(),
              MainActivity.class
          ).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
      );
    } catch (Exception e) {
      ExceptionTracker.log(e);
    }
  }

这是我看到的例外情况:

java.lang.NoClassDefFoundError: Failed resolution of: Lokhttp3/internal/concurrent/TaskRunner$RealBackend;
at okhttp3.mockwebserver.MockWebServer.<init>(MockWebServer.kt:101)
at and.dev.cell.MainActivityTest.<init>(MainActivityTest.java:18)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2189)
Caused by: java.lang.ClassNotFoundException: Didn't find class "okhttp3.internal.concurrent.TaskRunner$RealBackend" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/and.dev.cell.test-BTg7mf7fGT0rZNPUutH4Kg==/base.apk", zip file "/data/app/and.dev.cell-zdMzg_u88rFRy9yIIP8HqQ==/base.apk"],nativeLibraryDirectories=[/data/app/and.dev.cell.test-BTg7mf7fGT0rZNPUutH4Kg==/lib/x86, /data/app/and.dev.cell-zdMzg_u88rFRy9yIIP8HqQ==/lib/x86, /data/app/and.dev.cell.test-BTg7mf7fGT0rZNPUutH4Kg==/base.apk!/lib/x86, /data/app/and.dev.cell-zdMzg_u88rFRy9yIIP8HqQ==/base.apk!/lib/x86, /system/lib, /system/product/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 29 more

验证所有库的 okhttp 版本是否相同,如果不确定可以使用下一个代码强制执行:

resolutionStrategy {
    force "com.squareup.okhttp3:okhttp:$okhttp3_version"
}

或在您的应用程序 gradle 文件中明确添加。