https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
k

Kruger Brent

06/06/2019, 2:08 PM
Hello guys, I am working on implementing kotlin/native in one of our projects. I would like to know if there is a way to import and use objective-c or Swift libraries into kotlin? Because I saw that, in the documentation it says, the Kotlin/native plugin brings about a lot of iOS libraries to Kotlin.
s

svyatoslav.scherbina

06/06/2019, 2:10 PM
k

Kruger Brent

06/06/2019, 2:19 PM
Yeah, I read that. Thank you. But does that mean, if I am using a library in my iPhone app, is it possible to use the same in Kotlin common module by importing them here?
Did that make sense?
my usecase
k

Kris Wong

06/06/2019, 2:22 PM
Obj-C: yes, Swift: no
k

Kruger Brent

06/06/2019, 2:23 PM
Cool, that was helpful 👍
s

svyatoslav.scherbina

06/06/2019, 2:24 PM
if I am using a library in my iPhone app, is it possible to use the same in Kotlin common module by importing them here?
Could you clarify? Do you want to import Objective-C library into Kotlin running on JVM?
k

Kris Wong

06/06/2019, 2:26 PM
I hope it's clear that it only works for iOS/macOS targets
k

Kruger Brent

06/06/2019, 2:35 PM
Okay. I was wondering if it is compiled in Kotlin, is there a way to use them in common code. And can it be used in the android app as well? I wouldn't think that's possible!
k

Kris Wong

06/06/2019, 2:40 PM
you are correct, that's not possible
k

Kruger Brent

06/06/2019, 2:41 PM
I tested it out the other way, by importing an obj-c library in a kotlin file and it worked as per the documentation. If I don't have a library that's available in android and only in iOS, or in C/C++, then I would be pretty much stuck I guess!
s

svyatoslav.scherbina

06/06/2019, 2:44 PM
If I don't have a library that's available in android and only in iOS, or in C/C++, then I would be pretty much stuck I guess!
Could you clarify?
k

Kruger Brent

06/06/2019, 2:45 PM
okay. Here is my use case
As part of Kotlin/Native, we are importing obj-c libraries and using them in Kotlin common code or iosMain right?
I was wondering if I am using a library that's unique to iOS and not available in Android
if there is a chance that it could be used for Andorid because we are getting it converted to Kotlin anyways. Should I be even asking that question?
s

svyatoslav.scherbina

06/06/2019, 2:48 PM
You can't use iOS library on Android. It is not converted to Kotlin.
k

Kruger Brent

06/06/2019, 2:48 PM
@svyatoslav.scherbina
does that make sense?
g

gildor

06/06/2019, 4:11 PM
Kotlin MPP doesn't work as you probably think, you can use only pure Kotlin code in common source set, also you can define expect declarations and implement them on each platform, but it's impossible to use platform code of one platform on another, each platform should have own implementation (or use pure Kotlin implementation, which may be compiled for target platform)
k

Kruger Brent

06/06/2019, 4:44 PM
ah, I understand now. So if there is a functionality or feature missing in our project, we can write it in Kotlin and compile for both the platform. But the downside is we will end up writing a lot of code when there is a library that's only available in only one platform for example iOS and there will be some sort of re-invention of the wheel when it's not available in kotlin. Is there a way to cross-compile an iOS library to Kotlin, that could be used for iOS and Android.
o

olonho

06/06/2019, 5:44 PM
k

Kruger Brent

06/06/2019, 6:15 PM
I read this, created a sample project as well. But I had few doubts on cross-compiling!
o

olonho

06/06/2019, 6:19 PM
cross-compiling usually means compiling binaries for one machine on another machine, i.e. compiling iOS/arm64 binaries on macOS/x86_64
👍 1
2 Views