【问题标题】:compileKotlin block in build.gradle file throws error "Could not find method compileKotlin() for arguments [...]"build.gradle 文件中的 compileKotlin 块抛出错误“找不到参数的方法 compileKotlin() [...]”
【发布时间】:2017-10-23 18:38:15
【问题描述】:

我正在尝试将 Kotlin 配置为在我的 Android 项目中使用 Java 1.8。我尝试在 build.gradle 文件的底部添加 compileKotlin 块,但如果这样做会出现错误。

发生的错误如下:

错误:(38, 0) 找不到参数的方法 compileKotlin() [build_dvcqiof5pov8xt8flfud06cm3$_run_closure4@66047120] 在项目上 org.gradle.api.Project 类型的 ':core'。

没有这个块,项目运行良好。我错过了什么?这是完整的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'
    }
}

【问题讨论】:

    标签: android gradle kotlin


    【解决方案1】:

    您收到的错误意味着项目中没有 compileKotlin 任务,这是 Android 项目所期望的。

    Android 项目中的 Kotlin 编译任务名称包含 build variant 名称(这些名称由构建类型、产品风味和其他设置组合而成,类似于 debugreleaseUnitTest -- 任务是 compileDebugKotlin 和 @ 987654326@ 分别)。没有compileKotlin 任务,通常是为普通Java + Kotlin 项目中的main 源集创建的。

    很可能,您想在项目中配置所有 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'
        }
    }
    

    【讨论】:

    • 那行得通,我认为 compileKotlin 已应用于所有构建变体,但显然不是。谢谢!
    • 在哪个文件中以及我们需要将这些行放在哪里
    • @SIVAKUMAR.J,您可以将它们放在项目模块的build.gradle 文件中。例如,在具有单个模块app 的Android 项目中,将它们放入app/build.gradle。只需将它们附加到文件末尾,以确保在到达它们时已经应用了 Kotlin 插件。
    • 应该是tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class)
    • @Diolor 我就是这样做的:tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { ... }
    【解决方案2】:

    来自kotlin-android documentation

    Android 插件还向 android 部分添加了 kotlinOptions 扩展,以设置所有 kotlin 任务的选项:

    android {
        kotlinOptions {
            jvmTarget = '1.8'
            noStdlib = true
        }
    }
    

    【讨论】:

    • 最佳答案!
    【解决方案3】:

    不是该问题的直接答案,但当遇到相同的 compileKotlin block in build.gradle file throws error “Could not find method compileKotlin() for arguments […]” 错误时,Google 将我引导至此答案。

    在我的情况下,问题是由将代码模块导入到我的项目中导致的,该代码模块仅包含 Java 代码。修复是为了确保 Java 特定模块的 build.gradle 中有以下内容:

    apply plugin: 'kotlin-android'
    

    【讨论】:

      猜你喜欢
      • 2019-08-22
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 2020-07-12
      相关资源
      最近更新 更多