build.gradle文件中的compileKotlin块会抛出错误“Could not find method compileKotlin()for arguments [...]” [英] compileKotlin block in build.gradle file throws error "Could not find method compileKotlin() for arguments [...]"
问题描述
我试图在我的Android项目中配置Kotlin以使用Java 1.8。我已经尝试在我的 build.gradle
文件的底部添加 compileKotlin
块,但是我得到一个错误if我这样做。
I'm trying to configure Kotlin to work with Java 1.8 in my Android project. I've tried adding the compileKotlin
block at the bottom of my build.gradle
file, but I get an error if I do so.
发生的错误如下:
The error that occurs is the following:
错误:(38,0)对于项目
':核心'类型为org.gradle.api.Project的参数
[build_dvcqiof5pov8xt8flfud06cm3 $ _run_closure4 @ 66047120]找不到方法compileKotlin()。
Error:(38, 0) Could not find method compileKotlin() for arguments [build_dvcqiof5pov8xt8flfud06cm3$_run_closure4@66047120] on project ':core' of type org.gradle.api.Project.
没有这个块,项目运行良好。我错过了什么?这是完整的 build.gradle
文件,它非常基本:
The project runs fine without this block. What am I missing? Here's the complete build.gradle
file, it's pretty basic stuff:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
minSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName '1.0.0'
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:25.3.1'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile 'com.google.android.support:wearable:2.0.2'
}
repositories {
mavenCentral()
}
compileKotlin {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
kotlinOptions {
jvmTarget = '1.8'
apiVersion = '1.1'
languageVersion = '1.1'
}
}
推荐答案
你得到的错误意味着项目中没有 compileKotlin
任务,并且这对于Android项目来说是预期的。
The error you are getting means that there's no compileKotlin
task in the project, and that's expected for Android projects.
Android项目中的Kotlin编译任务名称包含构建变体名称(这些是结合构建类型,产品风格和其他设置,看起来像 debug
或 releaseUnitTest
- 任务是 compileDebugKotlin
和 compileReleas eUnitTestKotlin
)。没有 compileKotlin
任务,它通常是为普通Java + Kotlin项目中的主要
源集创建的。
The Kotlin compilation task names in Android projects contain the build variant names (those are combined from build type, product flavor and other settings and look like debug
or releaseUnitTest
-- the tasks are compileDebugKotlin
and compileReleaseUnitTestKotlin
respectively). There's no compileKotlin
task, which is usually created for the main
source set in ordinary Java + Kotlin projects.
大多数情况下,您想要在项目中配置所有 Kotlin编译任务,为此,您可以按如下方式应用该块:
Most probably, you want to configure all Kotlin compilation tasks in the project, and to do that, you can apply the block as follows:
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
kotlinOptions {
jvmTarget = '1.8'
apiVersion = '1.1'
languageVersion = '1.1'
}
}
这篇关于build.gradle文件中的compileKotlin块会抛出错误“Could not find method compileKotlin()for arguments [...]”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!