When building my multiplatform app from Xcode I am...
# multiplatform
u
When building my multiplatform app from Xcode I am suddenly getting the following error while building the shared library:
Copy code
/bin/sh -c /Users/ubuntudroid/Library/Developer/Xcode/DerivedData/Clear-exiyzzachjfrvmbualscrdesogez/Build/Intermediates.noindex/Pods.build/Debug-iphonesimulator/shared.build/Script-46EB2E00010940.sh

                REPO_ROOT="$PODS_TARGET_SRCROOT"
                "$REPO_ROOT/../gradlew" -p "$REPO_ROOT" $KOTLIN_PROJECT_PATH:syncFramework                     -Pkotlin.native.cocoapods.platform=$PLATFORM_NAME                     -Pkotlin.native.cocoapods.archs="$ARCHS"                     -Pkotlin.native.cocoapods.configuration="$CONFIGURATION"
> Task :buildSrc:compileKotlin FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':buildSrc:compileKotlin'.
> Error while evaluating property 'compilerOptions.jvmTarget' of task ':buildSrc:compileKotlin'.
   > Failed to calculate the value of property 'jvmTarget'.
      > Unknown Kotlin JVM target: 20
How can I pin the JVM target for iOS builds of the shared library? Setting
Copy code
export JAVA_HOME=/Users/ubuntudroid/Library/Java/JavaVirtualMachines/azul-17.0.7/Contents/Home
in the shared build step mentioned above works, but that obviously won’t work on any other than my machine including the CI. Can I somehow force JAVA_HOME for the build script to be the same like its set on the machine its running on?
Alright, I was dumb: the issue is not in the shared module, but in the buildSrc module which only contains the Dependencies.kt. And what worked was simply adding the following to the build.gradle.kts of the buildSrc module:
Copy code
kotlin {
    jvmToolchain(17)
}
I’d still love feedback on whether that’s an acceptable solution or whether there are better ways.
j
Declaring the JVM toolchain is what I've done in my projects. I believe it's the recommended approach now for ensuring a consistent JVM language level.
u
@Jeff Lockhart Thanks for your input! Where do you usually declare it?
j
As you have it, in the
kotlin { ... }
configuration block of build.gradle.kts scripts, wherever a Kotlin plugin is applied. You could do it in a convention plugin as well. If you have a pure Java module, without Kotlin, you can do the same with:
Copy code
java {
    toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}
u
Ah, no, I meant in which module? Would be great if I could somehow define this for all Kotlin modules at once.
j
I don't think there's a way to apply it once in the top-level build.gradle.kts in
allprojects
or
subprojects
because you need a Kotlin plugin applied in order to use the
kotlin { ... }
configuration block. If you want to enforce the same JVM toolchain for multiple modules, you should be able to do this in a convention plugin. You could also make a shared variable for the Java language level that's passed to the
jvmToolchain()
function in each project.
u
Convention plugins sound super useful, thanks for the heads-up! 🤘
b
Hello there, I'm writing convention plugin for kmp project. Faced same problem as @ubuntudroid and applied solution set jvmToolChain to 17 but then I got the error in attached image. I also attached convention module gradle. @Jeff Lockhart Can you also take a look please 🙏
similar case discussed here: applied plugin:
Copy code
// settings.gradle.kts
plugins {
    id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}
and then it worked) ios build time take about 30 minf for some reason but eventually it worked.
j
id("org.gradle.toolchains.foojay-resolver-convention")
I was going to suggest this plugin. You need to make sure you have the proper JDK installed. This plugin will do that for you.
I would expect after the JDK is installed, subsequent builds should be faster.
b
I see, if the plugin to install JDK, after initial build it gets all the necessary files so that I can remove it once it's completed, right?
j
I'm not sure. I install JDKs manually myself. I don't rely on that plugin in my projects.
b
How to do it manually. I am using JDK 17. I'm not sure where or what version that plugin installed. I also applied convention plugin that sets version 17 in all gradle files. Other than build-logics(convention) module's gradle👀
638 Views