
UPDATE
此处已针对此问题提交了一个错误:
https://youtrack.jetbrains.com/issue/KT-17951
更新2
该错误已在AndroID Studio 3.0 Canary 3中修复
原帖
我刚刚开始使用AndroID Studio 3.0,我从一开始就启用了kotlin支持.我在我的项目中写了一个非常简单的Kotlin类:
data class Wallet(val coins: Int) { fun add(value: Int): Wallet = Wallet(coins + value) fun substract(value: Int): Wallet = if (coins > value) Wallet(coins + value) else throw InsufficIEntFundsException()}现在我想测试这个类,首先我在Kotlin写了一个本地运行的unittest(测试目录):
class WalletTestKotlin { @Throws(Exception::class) @Test fun add() { Assert.assertEquals(22, Wallet(20).add(2).coins.tolong()) Assert.assertNotEquals(5, Wallet(2).add(13).coins.tolong()) }}它编译并运行但错误消息:
Class not found:
“com.agentknopf.hachi.repository.model.WalletTestKotlin”Empty test
suite.
因此我用Java重新编写了测试:
public class WalletTest { @Throws(exceptionClasses = Exception.class) @Test public voID add() { Assert.assertEquals(22, new Wallet(20).add(2).getCoins()); Assert.assertNotEquals(5, new Wallet(2).add(13).getCoins()); }}然而,该测试也失败了 – 这次Kotlin类“Wallet”无法找到:
java.lang.NoClassDefFoundError: com/example/repository/model/Wallet
我想知道我是否遗漏了某些东西……运行Java测试,不是指Kotlin类,而是java类只能成功完成.
我的项目build.gradle文件是默认文件:
// top-level build file where you can add configuration options common to all sub-projects/modules.buildscript { ext.kotlin_version = '1.1.2-4' repositorIEs { maven { url 'https://maven.Google.com' } jcenter() } dependencIEs { classpath 'com.androID.tools.build:gradle:3.0.0-Alpha1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencIEs here; they belong // in the indivIDual module build.gradle files }}allprojects { repositorIEs { jcenter() maven { url 'https://maven.Google.com' } mavenCentral() }}task clean(type: Delete) { delete rootProject.buildDir}我的特定于模块的build.gradle的依赖项:
dependencIEs { compile filetree(dir: 'libs', include: ['*.jar']) //Kotlin support compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" //Testing librarIEs androIDTestCompile('com.androID.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.androID.support', module: 'support-annotations' }) testCompile 'junit:junit:4.12' testCompile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"}解决方法:
解决方法(暂时):
把它放到你的(app-level)build.gradle中:
task copyTestClasses(type: copy) { from "build/tmp/kotlin-classes/deBUGUnitTest" into "build/intermediates/classes/deBUG"}然后在“启动前”的底部向下修改测试JUnit Run / DeBUG配置,其中包含“Gradle-aware make”,另一部分,选择gradle任务,选择它所在的项目build.gradle文件,然后键入copyTestClasses. Click here for screenshots的不同测试框架,但管道工作方式相同.
您可能需要根据构建类型更改/添加更多目录管道.您找到这些奇怪位置的方法是通过在项目树中搜索相关的.class文件.
总结以上是内存溢出为你收集整理的Android Studio 3.0 Canary 1:Kotlin测试或Java测试引用Kotlin类失败全部内容,希望文章能够帮你解决Android Studio 3.0 Canary 1:Kotlin测试或Java测试引用Kotlin类失败所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)