Chris
Chris

Reputation: 40623

How to generate xcframework with KMM; embedAndSignAppleFrameworkForXcode only generates framework

I've created a KMM project, and also an Xcode project with a build phase that calls ./gradlew embedAndSignAppleFrameworkForXcode. It runs successfully, but generates a .framework, not an .xcframework; and we really need the latter for modern Xcode.

Am i missing something? I read that modern versions of kotlin can generate the xcframework directly, but i'm not sure how. Is there a gradle task for this?

Thanks so much :)

According to Kdoctor, I have: Xcode 14.1 Android studio 2021.3 Kotlin Plugin: 213-1.7.20 Kotlin MM plugin: 0.5.1(213)-60

Upvotes: 4

Views: 6028

Answers (2)

Alex Mortimer
Alex Mortimer

Reputation: 72

I don't remember if this solution was an option when this question was first posted, but I recently had the same issue and found another solution in addition to the manual way in the other answer.

When creating the shared module, the dialog presents an option to select the iOS framework distribution method. It defaults to Regular framework, and all of the tutorials for multiplatform modules that I've seen use this, so I didn't find the other options for a while.

Distribution Method Options

If you select XCFramework here, the resulting module will automatically have the assembleXCFramework gradle task available in the Tasks/build folder.

Gradle Task list

Executing that will generate the XCFramework for you, and the logs will point you to the destination path.

Result Path Logs

It still has the embedAndSignAppleFrameworkForXcode task as well, although I don't know if it produces something different than when 'Regular framework" is selected for the module.

Note: Executing the assembleXCFramework task again appears to not generate the framework or display the result path unless you delete the existing framework files.

Upvotes: 1

Mohammad Rahchamani
Mohammad Rahchamani

Reputation: 5220

You should declare your XCFrameworks, then it'll register three tasks for you. assembleXCFramework, assembleDebugXCFramework, and assembleReleaseXCFramework.

Here is an example of build.gradle:

import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework

plugins {
  kotlin("multiplatform")
}

kotlin {
   val xcf = XCFramework()
   val iosTargets = listOf(iosX64(), iosArm64(), iosSimulatorArm64())

   iosTargets.forEach {
      it.binaries.framework {
          baseName = "shared"
          xcf.add(this)
      }
   }
}

For more information, visit kotlin docs.

Upvotes: 8

Related Questions