You are currently viewing Newbie question about Kotlin Native and sharing code between Android and iOS

Newbie question about Kotlin Native and sharing code between Android and iOS

Dear Friends,

I developed an Android app that makes intensive use of the JSoup library to parse HTML. As an example, I will say that this happens inside a class called CustomHtmlAnalyzer.kt. After a couple of years developing this app, my CustomHtmlAnalyzer is grown, is complex, and works very well. I have covered it with a lot of unit tests, and I have all under control.

Now I’m porting the Android app to a native iOS app, and I reached the point where a I need the same CustomHtmlAnalyzer class but for iOS. Fortunately, there is a pod library for iOS called SwiftSoup, which works like Jsoup. I could write a copy of my CustomHtmlAnalyzer but in Swift. Besides that, I could translate each function and line of code. They would be pretty much the same, only written in Swift.

Would it be possible to reuse the class that I wrote in Kotlin, using kotlin native? I can abstract this class from jsoup, and then somehow configure the shared code that in case of the Android app, the jsoup library is used, and in case of the iOS version, the SwiftSoup library is used.

I saw a similar sample regarding the Android SharedPreferences, and the iOS UserDefaults. In this case, using the expected/actual feature, it is possible to call platform-dependent functions. Of course, the android SharedPreferences and iOS UserDefaults are supported natively by Kotlin Native.

And because of that my question. Would it be achievable somehow, using libraries that are not natively supported, like in this case Jsoup and SwiftSoup?

If you know some examples or post in the internet where this is explained, I would be very grateful.

Tanks in advance.

submitted by /u/Cylon999
[link] [comments]