I don't know if this question belongs here on in t...
# gradle
l
I don't know if this question belongs here on in the #C3PQML5NU channel. I have a project which compiles to JVM, JS and Linux. Now, this project is imported from a another project that is an Android UI on top of it. Of course it uses the JVM code, and it worked pretty well until recently. What happenes was that I started using a feature that is new in Java 21, and not available in earlier versions, and certainly not available on Android. It's just a single line of code that needs replacing with running on older JVM's. Is there a simple way to create some kind of shared source set between android and JVM which can contain everything except for this single line of code (which would be an
expect
function implemented differently on both of these platforms. It already has different implementations on JS and Linux).
If anyone is is wondering why I can't do some tricks with reflection, the feature I'm using is
ScopedValue
and it's being used because it gives a 5% performance boost in the most performance critical part of the application, so using reflection here is not an option.
t
Try with KotlinHierarchyBuilder. In general there is this issue which is not yet addressed.
l
Thanks. Would you say it's easy to get the hierarchybuilder to work for my usecase? The Android project isn't super important for me, so I may just wait a year or however long it may take to get this issue implemented.
t
There is the default template - you could try to extend it with your use-case
l
Thanks. I'll take a look.
e
yeah I think you could do it with
Copy code
kotlin {
    applyDefaultHierarchyTemplate {
        common {
            group("commonJvm") {
                withJvm()
                withAndroid()
            }
        }
    }
}
but for this case specifically, isn't
ScopedValue
still a preview feature? even if it's a performance win, I feel like it should perhaps be behind a feature check…
l
Yes, it is indeed a preview feature. But the benefit of it was too great. And limiting to Java 21 is fine for now.
But yes, this hierarchy thing could perhaps be used to build a non-Java 21 version as well, by falling back to the slow ThreadLocal.
@ephemient I tried to implement your solution, and added your code to the
build.gradle
file. But I'm getting this error:
Copy code
A problem occurred evaluating project ':array'.
> Could not find method common() for arguments [build_1i0wfhmjqf4vbrs7fnkm6eki1$_run_closure2$_closure5$_closure10@7da59b13] on extension 'kotlin' of type org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension.
This error message was not very helpful.
Do you happen to have any idea what the issue is?
t
Convert your scripts to Kotlin build scripts 🙂
I think example was for
gradle.kts
and you need to adopt it to groovy script
l
Thanks.
Is there any documentation on how to map one to the other?
t
no. Overall in KMP we are focusing on KTS support first
l
OK, I think I figured it out. I don't think this is the standard way to do it, but it seems to work. Is there a way to avoid the variables?
Copy code
applyDefaultHierarchyTemplate { x ->
        x.group("common") { y ->
            y.group("commonJvm") { z ->
                z.withJvm()
                z.withAndroid()
            }
        }
    }
I have finally gotten this stuff to work. Kinda. I have to set a property to enable/disable Android, because I cannot get the android target to work when doing a regular build. So I did this: https://codeberg.org/loke/array/src/branch/jvm-android-split/array/build.gradle#L12
Is this the best way to support both regular and android builds?