使用Gradle Kotlin DSL时无法解析的参考Kotlintest

时间:2018-09-18 07:08:10

标签: android gradle-kotlin-dsl kotlintest

这是我的build.gradle.kts

import org.jetbrains.kotlin.config.KotlinCompilerVersion

plugins {
    ...
}

android {
    compileSdkVersion(27)

    defaultConfig {
        ...
    }

    buildTypes {
        ...
    }

    sourceSets {
        getByName("main").java.srcDirs("src/main/java", "src/main/kotlin")
        getByName("test").java.srcDirs("src/test/java", "src/test/kotlin")
        getByName("androidTest").java.srcDirs("src/androidTest/java", "src/androidTest/kotlin")
    }

}

tasks.withType<Test> {
    useJUnitPlatform()
}

dependencies {
    implementation(kotlin("stdlib-jdk7", KotlinCompilerVersion.VERSION))
    implementation("com.android.support:appcompat-v7:27.1.1")

    ...

    //Test
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.1.1")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.1.1")

    androidTestImplementation("com.android.support.test:runner:1.0.2")
    androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.2")
    androidTestImplementation("android.arch.core:core-testing:1.1.1")

    testImplementation("io.kotlintest:kotlintest-runner-junit5:3.1.10")
    testImplementation("io.mockk:mockk:1.8.7")
}

使用./gradlew build构建它时,不会发生任何错误,但是如果我使用文档中指定的代码:

val test by tasks.getting(Test::class) {
    useJUnitPlatform { }
}

我收到以下错误:Task with name 'test' not found in project ':app'.

我的settings.gradle.kts

include(":app")

有人知道这是怎么回事吗? AS可以建议我自动补全,这很奇怪,但是在编译时,我得到了这个未解决的参考。

1 个答案:

答案 0 :(得分:0)

看起来您的问题出在此区块上,

tasks.withType<Test> {
    useJUnitPlatform()
}
  

如果您不使用 test 任务,则只需删除该任务   代码,该错误将消失!


编辑:

如果要使用test任务。在项目级别 build.gradle

中尝试这样
tasks.register("test", Test::class) {
    useJUnitPlatform()
}
相关问题