How to set compileJava' task ( 11) and 'compileKotlin' task (1.8) jvm target compatibility to the same Java version in build.gradle.kts?

Viewed 37759

Build.gradle.kts

buildscript {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath ("com.android.tools.build:gradle:7.0.2")
        classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30")
        classpath("gradle.plugin.com.github.spotbugs.snom:spotbugs-gradle-plugin:${Versions.spotbugsGradlePluginVersion}")
        classpath("se.bjurr.violations:violations-gradle-plugin:${Versions.violationsVersion}")

    }
}
//android {
//    compileOptions {
//        sourceCompatibility = JavaVersion.VERSION_11
//                targetCompatibility = JavaVersion.VERSION_11
//    }
//
//    kotlinOptions {
//        jvmTarget = JavaVersion.VERSION_11.toString()
//    }
//}
plugins {
    `maven-publish`
    `java-gradle-plugin`
    `kotlin-dsl`
    id ("io.gitlab.arturbosch.detekt") version ("1.18.1")
}
repositories {
    google()
    mavenCentral()
    gradlePluginPortal()
}

dependencies {
    compileOnly(gradleApi())
    testImplementation(gradleTestKit())
    testImplementation("junit:junit:${Versions.jUnitVersion}")
}
val generatedSources = tasks.register<GenerateVersionsFileTask>("generateSources")

ERROR : 'compileJava' task (current target is 11) and 'compileKotlin' task (current target is 1.8) jvm target compatibility should be set to the same Java version.

When I uncomment android {} Error : Script compilation errors:

 Line 15: android {
           ^ Unresolved reference: android

Thanks for your time and effort :) Jitendra

5 Answers

You can set java version for java with

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

or alternatively:

java {
    toolchain.languageVersion.set(JavaLanguageVersion.of(11))
}

and for kotlin with:

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
    kotlinOptions {
        jvmTarget = "11"
    }
}

All samples are in gradle kotlin dsl.

@Marian's answer didn't quite help me.

I end up setting the following in the app build.gradle

Android {
...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }

    kotlinOptions {
        jvmTarget=11
    }
...
}

If anyone keeps getting warnings about a java compilation task being set to java 1.8 you can add this to the gradle script:

afterEvaluate {
    tasks.withType<JavaCompile>().configureEach {
        sourceCompatibility = JavaVersion.VERSION_11.toString()
        targetCompatibility = JavaVersion.VERSION_11.toString()
    }
}

I was getting warnings on some java compile tasks on AS and this solved it for me.

This worked nicely for me:

kotlin {
  jvmToolchain {
    languageVersion.set(JavaLanguageVersion.of("11"))
  }
}

That way I only need to set it once.

This solution worked for me:

Just replace the statement (or similar to this):

kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8.toString() 
}

with

kotlinOptions {
    jvmTarget = "1.8"
}

in all the module level build.gradle files and then sync gradle.

Hope this works. Happy Coding...!

Related