Skip to content

Commit 2f92787

Browse files
committed
chore: use javac -release and kotlinc -Xjdk-release so 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-nosuchmethoderror/
1 parent 021f43b commit 2f92787

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

buildSrc/build.gradle.kts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ dependencies {
3535
}
3636

3737
java {
38-
sourceCompatibility = JavaVersion.toVersion(toolchainVersion)
39-
targetCompatibility = JavaVersion.toVersion(toolchainVersion)
40-
4138
toolchain {
4239
languageVersion = JavaLanguageVersion.of(toolchainVersion)
4340
vendor = JvmVendorSpec.ADOPTIUM
4441
}
4542
}
4643

44+
tasks.withType<JavaCompile>().configureEach {
45+
options.release = toolchainVersion
46+
}
47+
4748
kotlin {
4849
jvmToolchain(toolchainVersion)
49-
compilerOptions { jvmTarget = JvmTarget.fromTarget(toolchainVersion.toString()) }
50+
compilerOptions {
51+
jvmTarget = JvmTarget.fromTarget(toolchainVersion.toString())
52+
freeCompilerArgs.add("-Xjdk-release=$toolchainVersion")
53+
}
5054
}

buildSrc/src/main/kotlin/pklAllProjects.gradle.kts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import com.diffplug.gradle.spotless.KotlinGradleExtension
1717
import org.gradle.accessors.dm.LibrariesForLibs
1818
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
19-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
19+
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
2020

2121
plugins { id("com.diffplug.spotless") }
2222

@@ -52,15 +52,16 @@ configurations.all {
5252
}
5353

5454
plugins.withType(JavaPlugin::class).configureEach {
55-
val java = project.extensions.getByType<JavaPluginExtension>()
56-
java.sourceCompatibility = JavaVersion.VERSION_17
57-
java.targetCompatibility = JavaVersion.VERSION_17
55+
tasks.withType<JavaCompile>().configureEach {
56+
options.release = 17
57+
}
5858
}
5959

60-
tasks.withType<KotlinCompile>().configureEach {
60+
tasks.withType<KotlinJvmCompile>().configureEach {
6161
compilerOptions {
6262
jvmTarget = JvmTarget.JVM_17
6363
freeCompilerArgs.addAll("-Xjsr305=strict", "-Xjvm-default=all")
64+
freeCompilerArgs.add("-Xjdk-release=17")
6465
}
6566
}
6667

buildSrc/src/main/kotlin/pklJavaLibrary.gradle.kts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,9 @@ val libs = the<LibrariesForLibs>()
3434
val info = project.extensions.getByType<BuildInfo>()
3535

3636
java {
37-
val jvmTarget = JavaVersion.toVersion(info.jvmTarget)
38-
3937
withSourcesJar() // creates `sourcesJar` task
4038
withJavadocJar()
4139

42-
sourceCompatibility = jvmTarget
43-
targetCompatibility = jvmTarget
44-
4540
toolchain {
4641
languageVersion = info.jdkToolchainVersion
4742
vendor = info.jdkVendor
@@ -112,10 +107,8 @@ tasks.compileJava {
112107
}
113108

114109
tasks.withType<JavaCompile>().configureEach {
115-
val jvmTarget = JavaVersion.toVersion(info.jvmTarget)
116110
javaCompiler = info.javaCompiler
117-
sourceCompatibility = jvmTarget.majorVersion
118-
targetCompatibility = jvmTarget.majorVersion
111+
options.release = info.jvmTarget
119112
}
120113

121114
tasks.withType<JavaExec>().configureEach { jvmArgs(info.jpmsAddModulesFlags) }

buildSrc/src/main/kotlin/pklKotlinLibrary.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@ tasks.compileKotlin {
4242
}
4343

4444
tasks.withType<KotlinJvmCompile>().configureEach {
45-
compilerOptions { jvmTarget = JvmTarget.fromTarget(buildInfo.jvmTarget.toString()) }
45+
compilerOptions {
46+
jvmTarget = JvmTarget.fromTarget(buildInfo.jvmTarget.toString())
47+
freeCompilerArgs.addAll("-Xjdk-release=${buildInfo.jvmTarget}")
48+
}
4649
}

0 commit comments

Comments
 (0)