83

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'
    }
}
SolveSoul
  • 2,448
  • 4
  • 25
  • 34

7 Answers7

116

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'
    }
}
hotkey
  • 140,743
  • 39
  • 371
  • 326
  • 2
    That works, I thought compileKotlin was applied to all build variants but apparently not. Thanks! – SolveSoul May 24 '17 at 06:13
  • in which file and where we need to put these lines – SIVAKUMAR.J Apr 23 '18 at 10:52
  • 1
    @SIVAKUMAR.J, you can put them in the `build.gradle` file of the project's modules. For example, in an Android project with a single module `app`, put them into `app/build.gradle`. Just append them in the end of the file to make sure the Kotlin plugin has already been applied when they are reached. – hotkey Apr 23 '18 at 11:19
  • 3
    Should be `tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class)` – Diolor Feb 09 '19 at 15:55
  • 2
    @Diolor This is how I do it: `tasks.withType { ... }` – EntangledLoops Nov 15 '20 at 20:54
  • Use the `kotlin-android` solution: https://stackoverflow.com/a/55418991/9636 – Heath Borders Oct 01 '21 at 20:18
  • Caused by: groovy.lang.MissingPropertyException: Could not get unknown property 'org' for extension 'android' of type com.android.build.gradle.LibraryExtension. or Caused by: groovy.lang.MissingPropertyException: Could not get unknown property 'org' for project ':XXX' of type org.gradle.api.Project. if used in the gradle outside android – desgraci Apr 20 '22 at 07:29
  • @EntangledLoops I get Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: – desgraci Apr 20 '22 at 07:31
47

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
    }
}
G00fY
  • 4,545
  • 1
  • 22
  • 26
  • 3
    The best answer! – Skullper Sep 30 '20 at 06:19
  • groovy.lang.MissingMethodException: No signature of method: base_android_configuration_2j7ahxwm32c8n4wt5ejh7kbub.android() is applicable for argument types: (base_android_configuration_2j7ahxwm32c8n4wt5ejh7kbub$_run_closure1) values: [base_android_configuration_2j7ahxwm32c8n4wt5ejh7kbub$_run_closure1@64c6e15e] – desgraci Apr 20 '22 at 07:32
5

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'
Chris Lacy
  • 4,222
  • 3
  • 35
  • 33
1

In some projects I've Just used refresh gradle dependencies and solved the problem:

Right click on project's name and select refresh gradle dependencies.

enter image description here

WhoisAbel
  • 1,519
  • 1
  • 9
  • 19
1

android\build.gradle

buildscript {
    ext {
       // ADDED
       kotlin_version = "1.8.22"
    }
}
dependencies {
    // ADDED
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

android\app\build.gradle

apply plugin: 'kotlin-android'

android {
        // ADDED
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_11
            targetCompatibility JavaVersion.VERSION_11
        }
        kotlinOptions {
            jvmTarget=11
        }
    }
Lightz
  • 11
  • 3
0

I know sounds weird but I solved my problem with this solution. Android Studio recommends some improvements such as "Remove Explicit Type Arguments" but when I do it, I got this error.

So, my suggestion is that please add your "Explicit Type Arguments", then try again.

Explicit Type Arguments Example: enter image description here

canerkaseler
  • 6,204
  • 45
  • 38
0

For those like me that are struggling with gradles, these at least compile:

Top-level project build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext {
        kotlin_version = "1.8.22"
        appcompat_version = "1.6.1"
        google_services_version = "4.3.15"
        maps_version = "18.1.0"
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:8.0.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.gms:google-services:$google_services_version"
    }
}
plugins {
    id 'com.android.application' version '8.0.2' apply false
    id 'com.android.library' version '8.0.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.22' apply false
    id 'com.google.gms.google-services' version '4.3.15' apply false
}

App build.gradle

// app build file where you can add configuration options common to all sub-projects/modules.

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'com.google.gms.google-services'
}
android {
    namespace 'com.test.foobar'
    compileSdk 33

    defaultConfig {
        applicationId "com.test.foobar"
        minSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            debuggable true
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    buildFeatures {
        viewBinding true
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
    sourceSets{
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "androidx.appcompat:appcompat:$appcompat_version"
    implementation 'com.google.android.material:material:1.9.0'
    implementation "com.google.android.gms:play-services-maps:$maps_version"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1'
    implementation 'androidx.compose.ui:ui-graphics:1.4.3'
    implementation 'androidx.viewpager2:viewpager2:1.0.0'

}
smirkingman
  • 6,167
  • 4
  • 34
  • 47