This gist demonstrates how to use a Kotlin MPP library which depends on an iOS Framework in a native iOS application (XCode project).
First, see how the Kotlin MPP Library is built.
Key ideas:
- We can currently only use a single Kotlin MPP library in an iOS project, so we need to create a MPP project local to our iOS XCode project whose sole purpose is to aggregate multiple MPP dependencies into a single Framework.
- To keep our working environment clean, we create the gradle project in a subdirectory instead of at the root of our iOS XCode project. I name my subdirectory
mpp
. - We need to have the Frameworks used by our dependencies locally, as we only have bindings provided by the
cinterop
klib
s, so we use Carthage again. - As the only goal of our gradle project is to create a Framework, we only use
ios*
targets. - Our project won't have any source code, but we NEED at least one source file, or Gradle will refuse to build anything, returning just a rude
NO-SOURCE
. Just create an emptyMain.kt
insrc/iosMain/kotlin
. - In addition to our empty source file, we need to export our dependencies using the
export(...)
function. - We need to build all required architectures then merge them into a single fat Framework using the
lipo
tool. lipo
only merges binaries, we need to have some common Framework metadata like theInfo.plist
, so we take the ones produced with thearm64
Framework. The only problem is that theInfo.plist
of that Framework states that it isarm64
, so we need to fix this with thePlistBuddy
tool.- Frameworks don't really need a version in the iOS world (at least when using Carthage), so our project doesn't have one, but if you want you can set one in the same
PlistBuddy
task. - We build the Framework using the
buildFramework
task. We then just import this Framework in XCode normally, it's location ismpp/build/ios/releaseFramework/Mpp.framework
. You can add./gradlew buildFramework
as a build step in your XCode project if you want. - Once the Framework is imported into XCode, just use
import Mpp
at the top of any Swift file to be able to use any of the dependencies of your local Gradle MPP project.