18
Kotlin Native Running Android code on iOS 30 September 2019

Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

  • Upload
    others

  • View
    23

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

Kotlin Native Running Android code on iOS

30 September 2019

Page 2: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

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.

Page 3: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

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.

Page 4: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

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

Page 5: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

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.

Page 6: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

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.

Page 7: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

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.

Page 8: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

Be a hero.Adopt a dog. Or a cat.

You can do it.

Page 9: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

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.

Page 10: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

Biased comparisons

• Native Development• Xamarin• React Native• Cordova• Flutter

Photo by Artem Bali from Pexels

Page 11: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

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)

Page 12: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

JVM Code

internal actual fun writeLogMessage(message: String, logLevel: LogLevel) { println("[$logLevel]: $message") }

Page 13: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

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) } }

Page 14: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

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)     } }

Page 15: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

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)     } }

Page 16: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

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

Page 17: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

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.

Page 18: Kotlin Native Talk 30 Sep 2019 - NoFuss · • Kotlin is the official language for developing Android apps. • It runs on the JVM, Android, Browser and as Native code. • Plays

https://tears.org.za/