Building an iOS Fat Framework for a Kotlin Multiplatform Project

We don’t want no skinny frameworks

Kris Wong
VMware 360
2 min readDec 16, 2019

--

This is the second post in a series on Kotlin Multiplatform. In my first post, I discussed why we chose Kotlin Multiplatform, and looked at a number of considerations when getting started with this framework. In this post, I’ll walk you through the steps to build a fat framework for your iOS app.

Depending on how you decide to integrate your Kotlin Multiplatform library into your iOS apps, you may not need to build a fat framework. However, I have found that when creating a new Multiplatform Project (MPP), it is useful to start with a fat framework until you have had the chance to get setup with something like CocoaPods or Carthage. Since the Multiplatform plugin has support for fat frameworks, it’s relatively straightforward to set up.

The changes we’ll be making will all be made in the build.gradle file:

  • At the top of the file, add the following import line:
import org.jetbrains.kotlin.gradle.tasks.FatFrameworkTask
  • Within the Kotlin block, add targets for each iOS ABI you would like to include.
kotlin {
iosX64('ios')
iosArm32('iosArm32')
iosArm64('iosArm64')
}

Note that one of these targets must be named to match your iOS source set names. In my case (and by default), this is iosMain and iosTest. If you do not do this, IDEA will not understand any of the code in your iOS sources, which is a major problem.

  • You will leave iosMain and iosTest within your kotlin.sourceSets block, but add the following:
configure([iosArm32Main, iosArm64Main]) {
dependsOn iosMain
}
configure([iosArm32Test, iosArm64Test]) {
dependsOn iosTest
}

This way all of your iOS targets will include the sources in these source sets.

  • Within your kotlin.targets block, configure your iOS targets to have the same framework name:
configure([ios, iosArm32, iosArm64]) {
binaries.framework {
baseName = "$ios_framework_name"
}
}

$ios_framework_name is defined elsewhere in the build script (in the buildscript block).

  • Lastly, add the fat framework task:

This will build a release fat framework under build/fat-framework. You could alternatively build a debug framework by specifying “DEBUG”, or have separate tasks each for release and debug.

To build the fat framework, find fatFramework under your Gradle other build tasks, right click, and choose to run it. You could also run gradle from the command line using the fatFramework target.

That’s all there is to it! In my next post, I will discuss the steps necessary to add an Android target to your Kotlin MPP.

--

--

Kris Wong
VMware 360

Software engineer+architect. Entrepreneur. Real estate investor.