为什么我的Kotlin代码在IDEA中编译而不在Gradle中?

时间:2018-03-09 05:39:26

标签: gradle intellij-idea kotlin

我有一个带有以下导入的kotlin(test)类:

import com.sun.tools.attach.VirtualMachine

在IDEA中,我已将tools.jar添加到SDK类路径中。 它在IDEA中运行良好,但在compileTestKotlin中从gradle运行时出现错误:

Unresolved reference: tools
Unresolved reference: VirtualMachine

Caused by: org.gradle.api.GradleException: Compilation error. See log for more details
    at org.jetbrains.kotlin.gradle.tasks.TasksUtilsKt.throwGradleExceptionIfError(tasksUtils.kt:8)
    at org.jetbrains.kotlin.gradle.tasks.KotlinCompile.processCompilerExitCode(Tasks.kt:415)
    at org.jetbrains.kotlin.gradle.tasks.KotlinCompile.callCompiler$kotlin_gradle_plugin(Tasks.kt:389)
    at org.jetbrains.kotlin.gradle.tasks.KotlinCompile.callCompiler$kotlin_gradle_plugin(Tasks.kt:251)
    at org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile.execute(Tasks.kt:215)

如何让gradle构建使用tools.jar

build.gradle的关键部分如下所示:

buildscript {
    ext.kotlin_version = '1.2.21'

    repositories {
        jcenter()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin'
apply plugin: 'idea'

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        languageVersion = "1.1"
        apiVersion = "1.1"
        jvmTarget = "1.8"
        javaParameters = true   // Useful for reflection.
    }
}

0 个答案:

没有答案