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

kevinskrei

12/04/2018, 12:53 AM
I'm looking at the tutorials for mpp (https://kotlinlang.org/docs/tutorials/native/mpp-ios-android.html) and see that it says KN comes with some pre-imported frameworks to use for iOS. Is there a way to access Android specific resources in this example? It seems that all of the examples I've seen require Android code to be in a separate module like in multiplatform-settings.
Copy code
actual fun platformName(): String {
  return "Android"
}
Copy code
import platform.UIKit.UIDevice

actual fun platformName(): String {
  return UIDevice.currentDevice.systemName() +
         " " +
         UIDevice.currentDevice.systemVersion
}
s

Sam

12/04/2018, 1:17 AM
You can access Android specific resources from Android mpp modules. Android requires some finesse in that it often needs a context object to do anything environment specific. Injecting that kind of dependency into a function isn’t easy to do cleanly. It gets easier if that function is part of an object. Then you could have an Android specific constructor that took a context. You wouldn’t be able to instantiate that class from common code. You would have to pass it to the common code from Android. Your iOS code would be structured similarly but without the Android specific pieces such as the constructor taking an Android context.
k

kevinskrei

12/04/2018, 1:35 AM
Thanks for the response. Right, so I'm trying to do something similar to the multiplatform-settings project where he has
public actual class PlatformSettings public constructor(private val delegate: SharedPreferences)
in the android module after declaring
expect class PlatformSettings
in the common module. But in mutliplatform-settings he's got the android code in a completely separate module. It's a bit confusing because in the tutorial source code I can access some iOS resources from the same module as the common code using
import platform.UIKit....
. I was expecting to be able to do something like
import platform.content.Context
from the Android code. Just wondering how I have to structure the project to be able to access platform specific resources for both iOS and Android while having some common code in between
r

russhwolf

12/04/2018, 4:45 AM
That tutorial uses the newer multiplatform plugin, whereas multiplatform-settings is still on an older setup from before KotlinConf. The newer setup uses sourceSets the way the older setup uses modules. So in that tutorial, you can access iOS stuff from
src/iosMain
and Android stuff from
src/androidMain
. For example you could make the Android implementation match iOS a little better by defining
actual fun platformName(): String = "Android ${Build.VERSION.RELEASE}"
s

Sam

12/04/2018, 4:45 AM
Multiplatform settings is using the previous version of the Kotlin Multiplatform Gradle plugin so it is structured a little differently. The reason UIKit is namespaced under platform is that it was done that way in the cinterop def file. For Android code just import the package as you normally would.
k

kevinskrei

12/06/2018, 6:14 PM
Thanks guys. Is the IDE up to date (IntelliJ CE) to support code completion with the new formats (srcSets)? I'm trying to import a
android.Content.Context
and it doesn't appear to find it. Is there something I need to setup to make this work?