Android w / Kotlin pure-java-Junit NoClassDefFoundError解决方法无效

时间:2017-05-28 08:08:18

标签: android junit kotlin

Repository that reliably reproduces the error: explodes/bug-kotlin-junit

我的Android应用程序包含一个应用程序模块和4个库模块。

我的测试是用纯Java编写的。不接触Kotlin代码的测试运行得很好,但是当我运行使用(MyKotlinObj::methodRef)或类(new MyKotlinObj())的测试时,我的测试将失败并显示以下内容:

java.lang.NoClassDefFoundError: com/explod/api/UserCreated

    at com.explod.api.Bug.initUserCreated_shouldNotTriggerNoClassDefFoundError(Bug.java:19)
    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.explod.api.UserCreated
    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

使用:

  • Android Studio 3.0 Canary 2

  • 构建工具26.0.0-rc2

  • Kotlin版本1.1.2-4

  • Android-Gradle Plugin 3.0.0-alpha2

  • Gradle gradle-4.0-milestone-1

  • 目标SDK 25

  • Min SDK 19

建议的解决方法here对我不起作用:

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

它看起来像Android Studio team possibly has a solution,但我希望有一个有效的解决方法。我的问题不同吗?我的测试中没有Kotlin代码。

1 个答案:

答案 0 :(得分:2)

原来,解决方法的目标副本目录不正确。

我一直在使用:

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

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

但以下内容对我有用:

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

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

在干净版本中,SDK类不可用。我还需要将:api:assembleDevDebug添加到运行配置中,以使其在干净的构建中运行。