Last active
December 5, 2023 13:11
-
-
Save epool/b55f9b8f64427efc5717510e0246c278 to your computer and use it in GitHub Desktop.
Builds a XcFramework from a shared module in KMM.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//region XcFramework tasks | |
val xcFrameworkPath = "xcframework/${project.name}.xcframework" | |
tasks.create<Delete>("deleteXcFramework") { delete = setOf(xcFrameworkPath) } | |
val buildXcFramework by tasks.registering { | |
dependsOn("deleteXcFramework") | |
group = "build" | |
val mode = "Release" | |
val frameworks = arrayOf("iosArm64", "iosX64") | |
.map { kotlin.targets.getByName<KotlinNativeTarget>(it).binaries.getFramework(mode) } | |
inputs.property("mode", mode) | |
dependsOn(frameworks.map { it.linkTask }) | |
doLast { buildXcFramework(frameworks) } | |
} | |
fun Task.buildXcFramework(frameworks: List<org.jetbrains.kotlin.gradle.plugin.mpp.Framework>) { | |
val buildArgs: () -> List<String> = { | |
val arguments = mutableListOf("-create-xcframework") | |
frameworks.forEach { | |
arguments += "-framework" | |
arguments += "${it.outputDirectory}/${project.name}.framework" | |
} | |
arguments += "-output" | |
arguments += xcFrameworkPath | |
arguments | |
} | |
exec { | |
executable = "xcodebuild" | |
args = buildArgs() | |
} | |
} | |
//endregion |
Someone pointed to this as well as something similar: https://github.com/ge-org/multiplatform-swiftpackage/blob/b1e0f240d305dcea831fd3c088e12f432d62eb2c/src/main/kotlin/com/chromaticnoise/multiplatformswiftpackage/task/CreateXCFrameworkTask.kt
Thanks again!
how to use BuildXcFramework.kt?
Right, I would love to know how to use the script above.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, this is awesome! My framework is a different name than the
${project.name}
. I ended up grabbing theframework.baseName
in order to make this dynamic and work in that case.val xcFrameworkPath = "xcframework/${project.kotlin.iosArm64().binaries.getFramework(mode).baseName}.xcframework"
and
arguments += "${it.outputDirectory}/${it.baseName}.framework"
Is there a cleaner way to do this? It works. I have a framework name that is different because my module name in Gradle is
client-side
and the framework name ends up beingclient_side
.