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:

None

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:

None

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:

None

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