Java is a programming language no programmer is unaware of. In fact, it is the language that created a stir in the world of programming when it is launched. Since then it has become a core of programming. When it is used to that extent, it is common to get errors. The error warning “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 in Java” is the one that you may encounter.
Don’t get scared of the long error. It is basically a compilation error that you get when you try to compile Kotlin or Java. The error doesn’t let you compile and execute the code. As the code says, the reason for getting the error warning is the different version of the JVM target and compiler installed. Let’s figure out the error warning
How the error shows up
When you run the following program, you end up with the error warning
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")
As soon as you compile the code, you will get the following warning
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 Error Occurs
Let’s check out the solutions to fix the error
Solutions to fix “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 in Java”
Set the Java Version
Setting up the Java version will help you fix the issue. Use the following code
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
Set the Gradle JVM
This is another way to fix the error. Using this, you can find JDK 11 by instructing Gradle and also compile Javadoc and testing tasks. If you don’t have JDK 11 locally installed, your Gradle will install it automatically. Check the code out
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(11))
}
Set JVM for Kotlin
Follow the code below to set the JVM for Kotlin
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "11"
}
}
Set the build.gradle
This is also a solution to fix the error. Have look at the code
Android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget=11
}
...
}
Conclusion
Many ways are discussed to fix the error “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 in Java”. We leave the decision to you to choose the way you find suitable.
Hope it helps you fix the error!
Reference Source: https://www.stackubuntu.com/react-native-compilejava-taskcurrent-target-is-1-8-and-compilekotlin-task