NoClassDefFoundError on Kotlin class in JUnit test

2019-07-16 01:51发布

问题:

My Kotlin class compiles and runs on an emulator without a problem. But when I test it, Android Studio says the class is not found.

Here's the Kotlin class and an example of using it:

// 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());
    }
}

However when I write a JUnit test for the Kotlin class:

// 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 compiles the code, but refuses to run the test:

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

Here comes the dependencies section in the module-level build.gradle:

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

What did I do wrong?

回答1:

This currently doesn't work with the latest tools. There is an open issue for this in the Jetbrains tracker.



回答2:

The solution is to create a new task in the module build.gradle file.

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

Then add that task to the "Before launch" section of the test run configuration.



回答3:

This is an open issue, I guess it will be fixed soon:

https://youtrack.jetbrains.com/issue/KT-17951

https://issuetracker.google.com/issues/38454212

Meanwhile, you can use the following workaround:

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

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

Add it to your Gradle file (after android {...} section)

After that, open your tests configuration and add new "before launch" step which will execute those tasks:



回答4:

Someone confirmed this bug has been fixed in Android Studio 3 Canary 3: https://issuetracker.google.com/issues/38454212