android-studio - 运行时 JUnit 测试中 Kotlin 类出现 NoClassDefFoundError

标签 android-studio kotlin

我的 Kotlin 类在模拟器上编译和运行没有问题。但是当我测试它时,Android Studio说找不到该类。

这是 Kotlin 类及其使用示例:

// Product.kt
data class Product(
    val id: Int,
    val name: String,
    val manufacturer: String)

// MainActivity.java
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Product product = new Product(0, "foo", "bar");
        TextView textView = findViewById(R.id.textView);
        textView.setText(product.getName());
    }
}

enter image description here

但是,当我为 Kotlin 类编写 JUnit 测试时:

// ProductTest.java
public class ProductTest {
    @Test
    public void getName() throws Exception {
        Product p = new Product(0, "foo", "bar");
        assertThat(p.getName(), equalTo("foo"));
    }
}

Android Studio 编译代码,但拒绝运行测试:

java.lang.NoClassDefFoundError: com/example/Product

  at com.example.ProductTest.getName(ProductTest.java:15)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
  at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Caused by: java.lang.ClassNotFoundException: com.example.Product
  at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  ... 28 more

这里是模块级 build.gradle 中的 dependency 部分:

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
    testCompile 'junit:junit:4.12'
}

我做错了什么?

最佳答案

解决方案是在模块build.gradle文件中创建一个新任务。

android {
    task copyTestClasses(type: Copy) {
        from "build/tmp/kotlin-classes/debugUnitTest"
        into "build/intermediates/classes/debug"
    }

然后将该任务添加到测试运行配置的“启动前”部分。

enter image description here

关于android-studio - 运行时 JUnit 测试中 Kotlin 类出现 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44085898/

相关文章:

java - 如何访问android中的下载文件夹?

android - 在 EditText Kotlin 中设置文本

Android Studio 模拟器框架尺寸比屏幕尺寸大得多

java - 某些 Android 设备上应用程序崩溃的原因是什么

android - 将recyclerview的焦点更改为某个项目位置?

android - 如何使用Gradle库在Android中使用自定义字体?

android - 可组合项中出现意外边界。即使边框宽度为零,边框也会显示。?

android - 类型推断失败。 Firebase JobDispatcher

android - 如何为 material.Slider View 创建绑定(bind)适配器?

android - 分享CoroutineExecutorExtension的定义