Sol*_*oul 43 android gradle kotlin
我正在尝试将Kotlin配置为在我的Android项目中使用Java 1.8.我已经尝试compileKotlin在我的build.gradle文件底部添加块,但如果我这样做,我会收到错误.
发生的错误如下:
错误:(38,0)无法在类型为org.gradle.api.Project的项目':core'上找到参数[build_dvcqiof5pov8xt8flfud06cm3 $ _run_closure4 @ 66047120]的方法compileKotlin().
没有这个块,项目运行良好.我错过了什么?这是完整的build.gradle文件,它是非常基本的东西:
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'
}
}
Run Code Online (Sandbox Code Playgroud)
hot*_*key 67
您获得的错误意味着compileKotlin项目中没有任务,这是Android项目的预期.
在Android的项目科特林编译任务名称包含构建变量名称(这些是从建设类型,产品风味和其他设置相结合,看起来像debug或releaseUnitTest-任务是compileDebugKotlin和compileReleaseUnitTestKotlin分别).没有compileKotlin任务,通常是为main普通Java + Kotlin项目中的源集创建的.
最有可能的是,您希望在项目中配置所有 Kotlin编译任务,为此,您可以按如下方式应用块:
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'
}
}
Run Code Online (Sandbox Code Playgroud)
G00*_*0fY 16
Android插件还向android部分添加了kotlinOptions扩展,以设置所有kotlin任务的选项:
android {
kotlinOptions {
jvmTarget = '1.8'
noStdlib = true
}
}
Run Code Online (Sandbox Code Playgroud)
不是问题的直接答案,但谷歌在经历同样的情况时引导我找到了这个答案compileKotlin block in build.gradle file throws error \xe2\x80\x9cCould not find method compileKotlin() for arguments [\xe2\x80\xa6]\xe2\x80\x9d错误时引导我找到了这个答案。
就我而言,问题是由于将代码模块导入到仅包含 Java 代码的项目而引起的。修复是为了确保以下内容位于 Java 特定模块的中build.gradle:
apply plugin: 'kotlin-android'\nRun Code Online (Sandbox Code Playgroud)\n
小智 6
android\build.gradle
buildscript {
ext {
// ADDED
kotlin_version = "1.8.22"
}
}
dependencies {
// ADDED
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
Run Code Online (Sandbox Code Playgroud)
android\app\build.gradle
apply plugin: 'kotlin-android'
android {
// ADDED
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget=11
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9832 次 |
| 最近记录: |