Java
Kotlin
build.gradle.kts
compileJava task
compileKotlin task

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

When working on a project that involves both Java and Kotlin codes, it is crucial to ensure that both compileJava and compileKotlin tasks are configured to use the same Java version for compatibility and performance reasons. This article guides you through setting the JVM target for both tasks in a Kotlin-based Gradle script (build.gradle.kts).

Understanding JVM Target Compatibility

JVM target compatibility refers to the version of Java bytecode that the compiler generates. This is important because it determines which versions of the Java Virtual Machine (JVM) can execute the compiled bytecodes. For example, if you compile your code for Java 11, the resulting bytecodes will not run on a Java 8 JVM.

Configuring compileJava

In a Gradle Kotlin DSL script (build.gradle.kts), configuring the compileJava task to use a specific Java version can be achieved by setting the sourceCompatibility and targetCompatibility properties. These properties instruct the Java compiler which version of the Java language to use when compiling .java files and what version of bytecodes to generate, respectively.

kotlin
1tasks.compileJava {
2    sourceCompatibility = JavaVersion.VERSION_11
3    targetCompatibility = JavaVersion.VERSION_11
4}

Configuring compileKotlin

The Kotlin compiler also needs to be informed about the target JVM version it should generate bytecodes for. This is done using the kotlinOptions block inside the compileKotlin task.

kotlin
1tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
2    kotlinOptions {
3        jvmTarget = "11"
4    }
5}

Ensuring Consistency between Java and Kotlin Compilation

To ensure that Java and Kotlin are compatible and can interact seamlessly in the same project, it is advisable to set both to the same JVM target. This avoids issues such as UnsupportedClassVersionError, which occurs when a class compiled with a higher version of Java is loaded by an older JVM.

Example Configuration in build.gradle.kts

Combining the above configurations, here’s how you can set both compile tasks in your build.gradle.kts script:

kotlin
1plugins {
2    kotlin("jvm") version "1.8"
3    java
4}
5
6java {
7    sourceCompatibility = JavaVersion.VERSION_11
8    targetCompatibility = JavaVersion.VERSION_11
9}
10
11tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
12    kotlinOptions {
13        jvmTarget = "11"
14    }
15}

Key Points Summary Table

AspectConfigurationDescription
Java Compilation VersionsourceCompatibility, targetCompatibilitySet both to JavaVersion.VERSION_11
Kotlin JVM TargetkotlinOptions.jvmTargetSet to "11"
Gradle Filebuild.gradle.ktsKotlin DSL script
Compatibility ImportanceEnsures no runtime errorsImportant for mixed-language projects

Additional Tips

  1. Sync IDE Settings: When you update JVM targets in Gradle, ensure to also update your IDE settings to match the project configuration.
  2. Gradle Wrapper: Use the Gradle wrapper to ensure a consistent build environment irrespective of the Gradle version installed locally.
  3. Continuous Integration (CI): Ensure that your CI pipeline also uses the same Java version specified in your build.gradle.kts, to avoid discrepancies between development and production environments.

Conclusion

Setting the JVM target compatibility uniformly across Java and Kotlin modules in a single project is essential to prevent compatibility issues. This unified configuration not only simplifies the build process but also ensures a seamless operation across various environments where the application is deployed. With these settings, developers can leverage the full capabilities of both Java and Kotlin while maintaining optimal compatibility and performance.


Course illustration
Course illustration

All Rights Reserved.