Also JDK 22 support is somewhere planned? (Let me ...
# gradle
e
Also JDK 22 support is somewhere planned? (Let me see the Youtrack for the ticket)
m
For Gradle you mean? Gradle already supports java 22
e
I have android kotlin project that uses Java 17 source target version and I'm getting some issues if I try to run some tasks on JDK 22
m
What’s the error?
e
Probably also because our convenience plugins have the target version same
Usually something similar to
Copy code
Cannot inline bytecode built with JVM target 22 into bytecode that is being built with JVM target 17. Please specify proper '-jvm-target' option.
This is one of modules compile task
m
It probably misses an option somewhere. There’s a specific place for Android
e
Ah, that is probably because the bug in AGP test fixtures
m
Copy code
android {
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17             
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
}
e
It doesn't pickup the target language version
from module
m
Interesting!
e
We have jvmTarget set in the compatibility plugin
Copy code
internal fun Project.configureKotlin() {
    tasks.withType<KotlinCompile>().configureEach {
        compilerOptions {
            jvmTarget.set(JvmTarget.fromTarget(libs.versions.project.java.get()))
        }
    }
}
m
You need to set it in the android block IIRC
e
We also do
Copy code
context(Project)
internal fun CommonExtension<*, *, *, *, *, *>.withCommonAndroidConfiguration() {
    buildToolsVersion = libs.versions.project.buildTools.get()
    compileSdk = libs.versions.project.compileSdk.get().toInt()

    defaultConfig {
        minSdk = libs.versions.project.minSdk.get().toInt()
    }

    compileOptions {
        val javaVersion = JavaVersion.toVersion(libs.versions.project.java.get().toInt())
        sourceCompatibility = javaVersion
        targetCompatibility = javaVersion
    }
}
m
Try
Copy code
android {
    kotlinOptions {
        jvmTarget = "17"
    }
}
t
You are using a dependency compiled with jvm-target 22 in project with jvm-target 17
e
Yahor, that is error related to using class compiled for test fixture source by AGP
As above setting - we set compile options
but looks like it is not applied for that source set
t
please open then an issue in AGP issuetracker
e
it is experimental in AGP
That will be my next step
t
please post it here as well - I will ping AGP folks
e
First I will try the suggestion from Martin
Test Fixtures don't respect jvm target version (discovered with JDK 22) [355141779] - Issue Tracker (google.com)
1
thank you color 1
Not sure why I can post link here
Oke, when I put
Copy code
kotlinOptions {
        jvmTarget = "17"
    }
then I don't see error. But not sure why
Copy code
tasks.withType<KotlinCompile>().configureEach {
        compilerOptions {
            jvmTarget.set(JvmTarget.fromTarget(libs.versions.project.java.get()))
        }
    }
Is not same
m
This always confused me as well. But since it's an AGP API, I guess they have reasons
e
Oke, we have convention plugin, I wonder how can I replicate the first snippet with it? I'm trying to autocomplete
CommonExtension
and there is not
kotlinOptions
. When I try to see where is
kotlinOptions
defiled - it is a extension method of
com.android.build.gradle.LibraryExtension
. But I can not use
com.android.build.gradle.LibraryExtension.kotlinOptions
in the convention plugin.
m
you can check the instance of your extension in your convention plugin:
Copy code
if (extension is LibraryExtension) {
  kotlinOptions { ... }
}
e
That doesn't work. At least our variant like this
Copy code
pluginManager.applyAndConfigure(libs.plugins.android.library) {
            extensions.configure<LibraryExtension> {
               kotlinOptions {...}
gives
Unresolved reference: kotlinOptions
Probably need some import
Inside that lambda
this
is
LibraryExtension
When I use it in just gradle file and try to go to the declaration. I see it is defined as
Copy code
package org.gradle.kotlin.dsl

public fun com.android.build.gradle.LibraryExtension.kotlinOptions(configure: org.gradle.api.Action<org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions>): kotlin.Unit { /* compiled code */ }
But if I try in convention plugin to write import
import org.gradle.kotlin.dsl.kotlinOptions
that is not known. Maybe it is a java code and I should reference it differently.
m
Try this:
Copy code
android {
    (this as ExtensionAware).extensions.getByName("kotlinOptions").let {
      // stuff
    }
}
modulo making in robust for the cases where it’s not possible
e
And then use reflection to set field?
m
Mmm you should be able to cast from the .let
Copy code
(this as ExtensionAware).extensions.getByName("kotlinOptions").let {
        this as KotlinJvmOptions
    }
It’s a case of collaboration between AGP and KGP which explains why it can’t be a plain API and has to go through the type unsafe hoops of Gradle containers
e
Thanks!
Let me try to fit it again
Phew, done
Copy code
pluginManager.applyAndConfigure(libs.plugins.kotlin.android) {
            extensions.configure<KotlinAndroidProjectExtension> {
                compilerOptions {
                    jvmTarget.set(JvmTarget.fromTarget(libs.versions.project.java.get()))
                }
            }
        }
🏆 1
119 Views