Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 970
jitendra kumar Avatar asked Sep 06 '21 20:09

jitendra kumar


People also ask

Does Kotlin support Java 11?

Yes, it fully supports Java 11.

In which file do we change the version of Kotlin in Android app?

Select your module in the Project window, and then select File > New, select any Android template, and then choose Kotlin as the Source language.

What is build gradle kts?

KTS: Refers to Kotlin script, a flavor of the Kotlin language used by Gradle in build configuration files. Kotlin script is Kotlin code that can be run from the command line.

What is gradle clean build?

The clean task is defined by the java plugin and it simply removes the buildDir folder, thus cleaning everything including leftovers from previous builds which are no longer relevant. Not doing so may result in an unclean build which may be broken due to build artifacts produced by previous builds.


2 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> {
    kotlinOptions {
        jvmTarget = "11"
    }
}

All samples are in gradle kotlin dsl.

like image 83
Marian Avatar answered Sep 21 '22 02:09

Marian


@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
    }
...
}
like image 30
LeoXJ Avatar answered Sep 20 '22 02:09

LeoXJ