Upload
others
View
23
Download
0
Embed Size (px)
Citation preview
Kotlin Native Running Android code on iOS
30 September 2019
Please ask questions as we go along.
Fair warning: I reserve the right to either not know at all, or give you the wrong answer.
Topics
• What.• Why.• How.• Resources.• Assorted.• Other.• Miscellaneous.• Terrible Jokes.• Lies.• Alternate truths.
This talk contains language and references that are rated PG-99, or stronger.
This slide has been recycled from some other stupid talk.
• Terrible freelance developer, probably looking for work.
• Works remotely because I like my dog. A lot.
• Tweets can go to @EwaldHorn
• https://about.me/ewaldhorn
What?• Kotlin is the official language for developing Android
apps.
• It runs on the JVM, Android, Browser and as Native code.
• Plays well with others and can be used with native libraries on most platforms it supports.
• Kotlin DSL is replacing Groovy as the Gradle DSL of choice. Slowly.
• Kotlin Native is experimental. Be warned.
Native?
• Builds native apps, just like the platform tooling. Sort of.
• Allows for interaction with Android or iOS code.
• Compiles ahead of time to native code, not at runtime.
• Allows use of native libraries.
How does it work?• LLVM based backend for the standard Kotlin compiler.
• Supports a lot of targets:
• iOS (arm32, arm64, simulator) and MacOS (x86_64)
• Android (arm32, arm64)
• Windows (mingw x86_64, x86)
• Linux (x86_64, arm32, MIPS, MIPS le, Raspberry Pi)
• WebAssembly (wasm32)
• Creates executables, static and dynamic libraries and Apple frameworks.
Be a hero.Adopt a dog. Or a cat.
You can do it.
Why though?
• One code base makes code sharing easier.
• Solves many performance problems of cross-platform.
• Builds native code, so doesn’t need a runtime.
• Allows for platform-specific implementations via Kotlin MultiPlatform development.
Biased comparisons
• Native Development• Xamarin• React Native• Cordova• Flutter
Photo by Artem Bali from Pexels
Shared Codeenum class LogLevel { DEBUG, WARN, ERROR }
internal expect fun writeLogMessage(message: String, logLevel: LogLevel)
fun logDebug(message: String) = writeLogMessage(message, LogLevel.DEBUG) fun logWarn(message: String) = writeLogMessage(message, LogLevel.WARN) fun logError(message: String) = writeLogMessage(message, LogLevel.ERROR)
JVM Code
internal actual fun writeLogMessage(message: String, logLevel: LogLevel) { println("[$logLevel]: $message") }
JavaScript Code
internal actual fun writeLogMessage(message: String, logLevel: LogLevel) { when (logLevel) { LogLevel.DEBUG -> console.log(message) LogLevel.WARN -> console.warn(message) LogLevel.ERROR -> console.error(message) } }
Android Codeactual fun writeLogMessage(message: String, logLevel: LogLevel) { val tag = "SirLogAlot"
when (logLevel) { LogLevel.VERBOSE -> Log.v(tag, message) LogLevel.DEBUG -> Log.d(tag, message) LogLevel.WARNING -> Log.w(tag, message) LogLevel.ERROR -> Log.e(tag, message) } }
iOS Codeactual fun writeLogMessage(message: String, logLevel: LogLevel) { when (logLevel) { LogLevel.VERBOSE ->
_os_log_internal(__dso_handle.ptr, OS_LOG_DEFAULT, OS_LOG_TYPE_INFO, message) LogLevel.DEBUG ->
_os_log_internal(__dso_handle.ptr, OS_LOG_DEFAULT, OS_LOG_TYPE_DEBUG, message) LogLevel.WARNING ->
_os_log_internal(__dso_handle.ptr, OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, message) LogLevel.ERROR ->
_os_log_internal(__dso_handle.ptr, OS_LOG_DEFAULT, OS_LOG_TYPE_ERROR, message) } }
iOS UI Code@ExportObjCClass class ViewController : UIViewController { @OverrideInit constructor(coder: NSCoder) : super(coder)
@ObjCOutlet lateinit var label: UILabel
@ObjCOutlet lateinit var textField: UITextField
@ObjCOutlet lateinit var button: UIButton
@ObjCAction fun buttonPressed() { label.text = "Konan says: 'Hello, ${textField.text}!'" } }
From https://github.com/JetBrains/kotlin-native/blob/master/samples/uikit/src/iosMain/kotlin/main.kt
Kotlin Native Links
• https://kotlinlang.org/docs/reference/native-overview.html
• https://kotlinlang.org/docs/reference/multiplatform.html
• https://github.com/JetBrains/kotlin-nativePlenty of videos on YouTube and articles on the web.
https://tears.org.za/