Use javac -release and kotlinc -Xjdk-release (#1080)

The change ensures the generated bytecode adheres to the APIs
for the target Java release.

Previously, only sourceCompatibility and targetCompatibility were used,
and they might cause issues like NoSuchMethodError when a newer javac
compiles with -target older_release.

Note: it is good to use a new an up to date javac to avoid issues
in the compiler itself, so having a proper `-release ..` configuration
is vital.

See https://www.morling.dev/blog/bytebuffer-and-the-dreaded-nosuchmethoderr
This commit is contained in:
Vladimir Sitnikov
2025-05-28 19:38:53 +03:00
committed by GitHub
parent a19e6bf684
commit 7d50c46c29
4 changed files with 15 additions and 18 deletions
@@ -34,14 +34,9 @@ val libs = the<LibrariesForLibs>()
val info = project.extensions.getByType<BuildInfo>()
java {
val jvmTarget = JavaVersion.toVersion(info.jvmTarget)
withSourcesJar() // creates `sourcesJar` task
withJavadocJar()
sourceCompatibility = jvmTarget
targetCompatibility = jvmTarget
toolchain {
languageVersion = info.jdkToolchainVersion
vendor = info.jdkVendor
@@ -112,10 +107,8 @@ tasks.compileJava {
}
tasks.withType<JavaCompile>().configureEach {
val jvmTarget = JavaVersion.toVersion(info.jvmTarget)
javaCompiler = info.javaCompiler
sourceCompatibility = jvmTarget.majorVersion
targetCompatibility = jvmTarget.majorVersion
options.release = info.jvmTarget
}
tasks.withType<JavaExec>().configureEach { jvmArgs(info.jpmsAddModulesFlags) }