The right kind, whole, 2024, settings to configure the Java model in Gradle for Android.
JDK model problems all the time confuse me. In the event you don’t set the correct Java model stage to your venture, you come back throughout an entire plethora of mysterious mistakes on the maximum inconvenient occasions. I transfer venture that every now and then that it’s arduous to keep in mind the way it was once mounted final time!
So right here I’m writing down set the Java stage to your Android venture. This is; set the JVM goal, set your venture Java compatability, set your goal Java model, how to select your Java toolchain, what model of the JVM your Android venture makes use of, how to select Java in Gradle Android … you get the theory, then again you need to explain it, that is it!
All of this knowledge is already in the market and defined totally, the issue is it’s modified time and again all over the years and Google has a tendency to revel in indexing outdated issues. Subsequently I’m scripting this as a non-public TLDR.
As of Jan 2024 I’ve discovered I generally need to be atmosphere your Java model to 17 if imaginable. In case you are discovering you have got different compatibility problems when doing this, then cross with Java 11 however spend a while writing down what isn’t running and tackling the ones problems as neatly, then bump to Java 17. In case you are nonetheless focused on Java 8 … I want you neatly for your struggles.
When atmosphere the Java model to your Android Gradle venture. Every module is similar, it’s no other for the Android software module than it’s for any Android library modules. You wish to have to set this declaration for each module independently. It is advisable to glance into pulling this duplication out right into a composite construct or customized gradle plugin, however that’s some other weblog solely.
The Code
For each and every module:
Within the android block:
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
...
}
And outdoor the android block by itself:
kotlin {
jvmToolchain(17)
}
References to the above Gradle choices:
1) sourceCompatibility / targetCompatibility
2) jvmTarget
3) Android doctors jvmToolchain, Gradle doctors jvmToolchain
(Assuming you might be operating Gradle >= v6.7 & Kotlin >= v1.7.2.)
N.b. You probably have any Java best modules, your gradle record best wishes to incorporate:
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
N.b. You probably have any Kotlin (& Java) best modules, your gradle record best wishes to incorporate:
kotlin {
jvmToolchain(17)
}
In addition to fending off mistakes, atmosphere particular Java variations for all modules will transparent up warnings about Java goals being misaligned, corresponding to:
'compileDebugJavaWithJavac' job (present goal is 11) and 'kaptGenerateStubsDebugKotlin' job (present goal is 1.8) jvm goal compatibility will have to be set to the similar Java model.
By means of default will turn out to be an error since Gradle 8.0+! Learn extra: https://kotl.in/gradle/jvm/target-validation
Believe the use of JVM toolchain: https://kotl.in/gradle/jvm/toolchain
And that’s it, so long as you do this for each module for your venture you will have a constant Java model.
Any questions, you’ll to find me on:
Threads @Blundell_apps