Table of Contents
A typical error message you might see while trying to compile Java or Kotlin on your computer is that compilation errors prevent the program from executing. You might also see this 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” if you try to run a custom build of Kotlin or Java. While there are many reasons these errors can occur, the most common cause is that the compiler and the JVM target have different versions of Java installed. Learn what you can do about this issue here!
How Does It Occur?
Here is the program that runs and the error occurs:
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")
And the error is displayed as follows:
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
What causes error?
The main cause of the above error is that the Android gradle plugin requires a higher version of Java, specifically from 11. Also it may be because your Kotlin plugin version is not suitable. You can fix the error by reinstalling the version you are using.
Solutions For “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.”
Here are some solutions we’ve put together, take a look and choose the one suitable for you.
1. Set Gradle JVM toolchain feature
This will instruct Gradle to find JDK 11 and use it to compile, javadoc and testing tasks. In the event that you do not have JDK 11 installed locally, Gradle will automatically download it.
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(11))
}
2. Using version 11
This way is very simple, you just need to install the new version for java (version 11), use one of the following commands:
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
3. Set jvmTarget For kotlin
Set jvmTarget if you’re using Kotlin. You can use this code.
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "11"
}
}
4. Import this code in app build.gradle
Android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget=11
}
...
}
Your mistake must now be corrected.
Conclusion
If you’re having trouble with 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“, the solutions above are the quickest ways out.
If you still need advice or have other concerns, there is a growing community where everyone is usually willing to help. Finally, we hope you’re having a good time with the amazing code options and appreciate your reading time.
I am not sure with your issue but it works for me with the following way