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.
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.
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:
Key Points Summary Table
| Aspect | Configuration | Description |
| Java Compilation Version | sourceCompatibility, targetCompatibility | Set both to JavaVersion.VERSION_11 |
| Kotlin JVM Target | kotlinOptions.jvmTarget | Set to "11" |
| Gradle File | build.gradle.kts | Kotlin DSL script |
| Compatibility Importance | Ensures no runtime errors | Important for mixed-language projects |
Additional Tips
- Sync IDE Settings: When you update JVM targets in Gradle, ensure to also update your IDE settings to match the project configuration.
- Gradle Wrapper: Use the Gradle wrapper to ensure a consistent build environment irrespective of the Gradle version installed locally.
- 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.

