Kotlin Multiplatform iOS frameworks dependencies

Artem Sivcev
Dec 8, 2019 · 4 min read
Image for post
Image for post

How to add iOS frameworks to Kotlin Multiplatform Library using cocoapods and cinteropt.

Hey everyone! As developers we always making up problems to ourselves and then rejoice at solving them. I have a library for Android with many mathematical models, the task for me was not just to rewrite it for iOS, but to use Kotlin Native and common code. As an android developer I have been familiar with Kotlin for a long time and I liked it.

This is not a regular manual to rewrite documentation and pass it off as a training article, no. This is about what is not mentioned in the documentation, which can only be found in the heads of the people who invented it, through tickets on github and long sleepless nights of trials and errors. This is about how to add external iOS frameworks with and without headers in you KMP.

I hope you do not have any difficulties to create an empty project. If so, then you should already be familiar with the structure of the project. And you know about commonMain, androidMainand iOSMaindirectories. Blocks with similar names have our build.gradle file.

Add Android dependencies

Every android developer knows that our main language is Kotlin since 2018. And any dependencies you can add just like that:

As you see, we try to add Firebase ML KIT, because this framework contains headless types of it. There it is. Now you can write code in IDE without any problems. Just like that.

Add iOS dependencies

Now come back to iOS. Our task is to add two frameworks in project following the official documentation:

As original Kotlin documentation say: you can add framework using cocoapods plugin, or cinterop. Here I think: “Hm… It’s sounds pretty good to be true. Let’s try to use cocoapods!” But… There is some limitations:

Image for post
Image for post

That means this construction is wrong:

It seems that you can forget and throw out the cocoapods plugin, but no so fast! Just add it in you project with some informations fields, without any pods. Something like that:

Get back to cinerop and add this magic in you build.gradle file:

Also you need to add binary framework iniOSMain folder. And create .def file. If you are interested in how to get the framework out of the Pods, write it in the comments! The structure will look something like this:

Image for post
Image for post

You have already noticed that according to the documentation we need to connect two libraries, but there is only one. It’s all because the second one does not contain headers and cannot be connected this way, so all you need to add it in linkerOpts our .def file:

Do you remember we left cocapods plugin? Now it is useful for us. Make gradle podspec. We have got the standard podspec file. All that remains is to add our dependencies in this file:

Next, add our pod in iOS Podfile:

Then be attentive! Firstly, go to you library root folder and make gradle build. Secondly, open console, go to you iOS app folder and make pod install. If you did everything right now we can use dependencies in iosMain folder. Use imports like this:

import cocoapods.FirebaseMLVision.FIRVision
import cocoapods.FirebaseMLVision.FIRVisionImage
import cocoapods.FirebaseMLVision.FIRVisionText

Unfortunately, IDE will not highlight that you are writing the correct code, but by compiling it you will be surprised at the result!

Summary

That’s it! There was no one clear instruction on how to use iOS frameworks in KMP. But now you have learned another magic trick using Kotlin Native.

Links

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store