R. Campos
R. Campos

Reputation: 957

Can not import Cocoapod library into a Native Kotlin module with shared code

I have an android project, which has a Kotlin Native SharedCode module. The purpose is to share code between an android app, and an iOS app.

I have a big and complex class that parses HTML with help from the Jsoup library. I was planning to use the same class in iOS, but delegating the parsing work to the SwiftSoup library, which works pretty much the same as the Jsoup library.

I moved my class to the SharedCode module, I did the needed adaptations (with actual and expect and so on), and the Android version is working very well from this ShareCode module. The tutorial that I followed was this tutorial.

The SharedCode module was compiled by Android Studio, and I could use it in XCode. So far so good.

My next step was to include the SwiftSoup Cocoadpod in this shared module, to be able to use this pod in Koltin Native. I followed this documentation, and after some initial problems, I managed to get it to work.

Now there is no need to build the SharedCode module from Android Studio anymore. The build process is fired by Xcode when building the app. The Kotlin Native Shared module is included as Cocoapod in the Xcode project, and this includes the Swift soup cocoapod, which is referenced in the build.gradle.kts file.

After building the app in Xcode, the build folder is generated below SharedCode/build. Then I should be able to import the SwiftSoup package in my Kotlin Native class, to use it.

Indeed, if I write "import cocopapods.SwiftSoup", the code completion works, and the package is found. But then I receive the following error message: "Packages can not be imported". When writing the import, the code completion feature shows some contents below cocoapods.SwiftSoup, but not the class that I need. Only some datatypes:

enter image description here

I'm missing the classes that this library contains, like the SwiftSoup.swift class and so on.

Maybe somebody has an idea of what I'm doing wrong.

Upvotes: 0

Views: 1078

Answers (1)

R. Campos
R. Campos

Reputation: 957

As @Arytom Degtyarev has said, the problem is the Swift pod "SwiftSoup" lacking the needed interop with Objective-C. The public functions of this library were not marked with the @objec.

I found a workaround, by implementing my own "framework" in iOS. It has a class that is a wrapper exposing the functions from SwiftSoup that I need to call in the Kotlin code. That is, for each function of the pod SwiftSoup that I need to call in Kotlin, I exposed the function in my custom Framework project with the @objc operator.

Then this framework is imported in the Kotlin Native project, and used in the iOS variant of the kotlin class.

The whole process is a little bit confusing, difficult, and costly. The goal was to spare time not developing the same code in iOS, but I spent a lot of time until I learned how to implement my own iOS framework, then manage to import it in the SharedCode Kotlin Native module, then managed to get the Kotlin SharedModule code recognized into Xcode, and so on.

Maybe in the future, the whole process is easier.

Upvotes: 3

Related Questions