compileKotlin block in build.gradle file throws error "Could not find method compileKotlin() for arguments [...]"

AndroidGradleKotlin

Android Problem Overview


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:

> Error:(38, 0) Could not find method compileKotlin() for arguments > [build_dvcqiof5pov8xt8flfud06cm3$_run_closure4@66047120] on project > ':core' of type org.gradle.api.Project.

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'
    }
}

Android Solutions


Solution 1 - Android

The error you are getting means that there's no compileKotlin task in the project, and that's expected for Android projects.

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.

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'
    }
}

Solution 2 - Android

From the kotlin-android documentation:

Android plugin also adds kotlinOptions extension to android section to set options for all kotlin tasks:

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

Solution 3 - Android

Not a direct answer to the question, but Google led me to this answer when experiencing the same compileKotlin block in build.gradle file throws error “Could not find method compileKotlin() for arguments […]” error.

In my case the problem was caused by importing a code module to my project that contained only Java code. The fix was to ensure the following is in the Java-specific module's build.gradle:

apply plugin: 'kotlin-android'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSolveSoulView Question on Stackoverflow
Solution 1 - AndroidhotkeyView Answer on Stackoverflow
Solution 2 - AndroidG00fYView Answer on Stackoverflow
Solution 3 - AndroidChris LacyView Answer on Stackoverflow