gpt4 book ai didi

Android w/Kotlin pure-java-Junit NoClassDefFoundError 解决方法不起作用

转载 作者:行者123 更新时间:2023-11-29 19:13:56 27 4
gpt4 key购买 nike

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 金丝雀 2

  • 构建工具 26.0.0-rc2

  • Kotlin 版本 1.1.2-4

  • Android-Gradle 插件 3.0.0-alpha2

  • Gradle gradle-4.0-milestone-1

  • 目标 SDK 25

  • 最小 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 代码。

最佳答案

事实证明,解决方法的目标复制目录不正确。

我一直在使用:

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 添加到运行配置中,以使其在干净的构建中运行。

关于Android w/Kotlin pure-java-Junit NoClassDefFoundError 解决方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44225110/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com