Share
Embed
Kotlin All The Things!
Kotlin is taking the development community by storm. Since Google announced they are officially partnering with JetBrains to support the language for Android development, mobile developers are flocking en masse.
But what about everyone else? Good news - Kotlin is not just for mobile. In this talk, we will take a look at the overall landscape. For Java developers, we will look at Kotlin on the server. For Front End developers, we will check out Kotlin for JavaScript. For everyone else, we'll poke around Kotlin Native, which can compile Kotlin for platforms that don't have a virtual machine such as IOS.
Michael Yotive
April 12, 2019
Transcript
139 slides
-
Why All The Things? Kotlin All The Things • Save
time/money ◦ Caution: Big investment • Sharing code within/across company • Increased Quality • Feature teams -
• Kotlin on the Server • Client Side Kotlin •
Kotlin/Native • Kotlin Multiplatform Kotlin All The Things Agenda -
Kotlin on the Server Backend • Read dynamic data •
Authenticate • Write/Persist data Kotlin All The Things -
Kotlin on the Server Why Kotlin? • Great Modern language
◦ Higher order functions ◦ Extension functions ◦ Type Safety ◦ Coroutines • Allows you to stay on Java 8 Kotlin All The Things -
Kotlin on the Server Kotlin All The Things KotlinConf 2018
- Komparing Kotlin Server Frameworks by Ken Yee https://www.youtube.com/watch?v=8xfQA10Cd7g -
Spring and Spring Boot Kotlin All The Things What is
Spring? • Most popular framework for building Java applications. • Flexible, modular, and backwards compatible • Aims at making development easier by reducing boilerplate. • Large and active community • Open Source, but back by a large company. -
Spring and Spring Boot Kotlin All The Things What is
Spring Boot? • Another project built of top of Spring. • Main goals: ◦ Decrease friction by taking an opinionated approach to building applications ◦ Reduce configurations ◦ Streamline deployment approach -
Sample Kotlin All The Things @SpringBootApplication class DemoApplication fun main(args:
Array<String>) { runApplication<DemoApplication>(*args) } @RestController class HelloWorldController{ @GetMapping("/") fun helloWorld() = "Hello World!" } -
Sample Kotlin All The Things @SpringBootApplication class DemoApplication fun main(args:
Array<String>) { runApplication<DemoApplication>(*args) } @RestController class HelloWorldController{ @GetMapping("/") fun helloWorld() = "Hello World!" } -
Sample Kotlin All The Things @SpringBootApplication class DemoApplication fun main(args:
Array<String>) { runApplication<DemoApplication>(*args) } @RestController class HelloWorldController{ @GetMapping("/") fun helloWorld() = "Hello World!" } -
Sample Kotlin All The Things @SpringBootApplication class DemoApplication fun main(args:
Array<String>) { runApplication<DemoApplication>(*args) } @RestController class HelloWorldController{ @GetMapping("/") fun helloWorld() = "Hello World!" } -
Sample Kotlin All The Things @SpringBootApplication class DemoApplication fun main(args:
Array<String>) { runApplication<DemoApplication>(*args) } @RestController class HelloWorldController{ @GetMapping("/") fun helloWorld() = "Hello World!" } -
Sample Kotlin All The Things data class SimpleObject(val data:String =
"Sample") @RestController class HelloWorldController{ @GetMapping("/Sample") fun sample() = (1 .. 10).map { SimpleObject() } } -
Sample Kotlin All The Things data class SimpleObject(val data:String =
"Sample") @RestController class HelloWorldController{ @GetMapping("/Sample") fun sample() = (1 .. 10).map { SimpleObject() } } -
Sample Kotlin All The Things data class SimpleObject(val data:String =
"Sample") @RestController class HelloWorldController{ @GetMapping("/Sample") fun sample() = (1 .. 10).map { SimpleObject() } } -
KTor Kotlin All The Things • Jetbrain’s official server framework
• Written in pure Kotlin using Coroutines • Unopinionated • Lots of features ◦ Websockets ◦ Authentication ◦ HTML Templates https://ktor.io/ -
KTor Kotlin All The Things • Can be hosted on
any Servlet 3.0+ API (such as TomCat) ◦ Supports standalone Netty and Jetty, too. • Includes a cross-platform HTTP Client that includes: ◦ Authentication (includes OAuth 1a and 2) ◦ Cookies ◦ WebSockets ◦ JSON Processing https://ktor.io/ -
KTor Kotlin All The Things const val defaultPort = 8080
fun main() { embeddedServer(Netty, defaultPort, module = Application::main) .start(wait = true) } -
KTor Kotlin All The Things const val defaultPort = 8080
fun main() { embeddedServer(Netty, defaultPort, module = Application::main) .start(wait = true) } -
KTor Kotlin All The Things const val defaultPort = 8080
fun main() { embeddedServer(Netty, defaultPort, module = Application::main) .start(wait = true) } -
KTor Kotlin All The Things fun Application.main(){ install(DefaultHeaders){ header("Access-Control-Allow-Origin", "*")
} install(ContentNegotiation) { gson { setDateFormat(DateFormat.LONG) setPrettyPrinting() } } installRoutes() } -
KTor Kotlin All The Things fun Application.main(){ install(DefaultHeaders){ header("Access-Control-Allow-Origin", "*")
} install(ContentNegotiation) { gson { setDateFormat(DateFormat.LONG) setPrettyPrinting() } } installRoutes() } -
KTor Kotlin All The Things fun Application.main(){ install(DefaultHeaders){ header("Access-Control-Allow-Origin", "*")
} install(ContentNegotiation) { gson { setDateFormat(DateFormat.LONG) setPrettyPrinting() } } installRoutes() } -
KTor Kotlin All The Things fun Application.installRoutes() { routing {
get("/") { ... } get("/sessions") { ... } get("/sessions/{id}") { ... } } } -
KTor Kotlin All The Things get("/") { call.respondHtml { head
{ title("Ping") } body { h1 { +"The API is up." } } } } https://github.com/Kotlin/kotlinx.html -
KTor Kotlin All The Things get("/") { call.respondHtml { head
{ title("Ping") } body { h1 { +"The API is up." } } } } https://github.com/Kotlin/kotlinx.html -
KTor Kotlin All The Things get("/sessions") { val sessions =
SessionDB.sessionRepository.all() call.respond(sessions) } -
KTor Kotlin All The Things get("/sessions") { val sessions =
SessionDB.sessionRepository.all() call.respond(sessions) } -
KTor Kotlin All The Things get("/sessions/{id}") { val sessionId =
call.parameters["id"] val data = SessionDB.sessionRepository.find(Session::id eq sessionId) .firstOrDefault() call.respond(data) } -
KTor Kotlin All The Things get("/sessions/{id}") { val sessionId =
call.parameters["id"] val data = SessionDB.sessionRepository.find(Session::id eq sessionId) .firstOrDefault() call.respond(data) } -
KTor Kotlin All The Things data class HelloWorld(val hello: String)
post("/route") { val helloWorld = call.receive<HelloWorld>() } -
• kotlinc-js ◦ Available for use since Kotlin 1.1 ◦
Validate and transpile Kotlin code to Javascript • Targets ECMAScript 5.1 ◦ Plans in the work to support ECMAScript 2015 Kotlin/JS Kotlin All The Things -
Goals • Output readable Javascript. • Produce Javascript that is
optimal in size. • Optional interoperability with existing module systems: ◦ CommonJS (NodeJS) ◦ AMD (RequireJS) ◦ UMD Kotlin/JS Kotlin All The Things -
var x = 12345; x = "string"; var x =
12345; x = "string"; x = { key: "value" }; Dynamically Typed Statically Typed -
fun main(args: Array<String>) { println("Hello JavaScript!") } kotlinc-js helloworld.kt -output
helloworld.js File: helloworld.kt -
var HelloJS = function (_, Kotlin) { 'use strict'; var
println = Kotlin.kotlin.io.println_s8jyv4$; function main(args) { println('Hello JavaScript!'); } _.main_kand9s$ = main; main([]); Kotlin.defineModule('HelloJS', _); return _; }(typeof HelloJS === 'undefined' ? {} : HelloJS, kotlin); -
var HelloJS = function (_, Kotlin) { 'use strict'; var
println = Kotlin.kotlin.io.println_s8jyv4$; function main(args) { println('Hello JavaScript!'); } _.main_kand9s$ = main; main([]); Kotlin.defineModule('HelloJS', _); return _; }(typeof HelloJS === 'undefined' ? {} : HelloJS, kotlin); -
var HelloJS = function (_, Kotlin) { 'use strict'; var
println = Kotlin.kotlin.io.println_s8jyv4$; function main(args) { println('Hello JavaScript!'); } _.main_kand9s$ = main; main([]); Kotlin.defineModule('HelloJS', _); return _; }(typeof HelloJS === 'undefined' ? {} : HelloJS, kotlin); OutputToConsoleLog.prototype.println_s8jyv4$ = function (message) { console.log(message); }; -
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello Kotlin/JS</title> </head>
<body> <script type="text/javascript" src="helloworld.js"></script> </body> </html> -
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello Kotlin/JS</title> </head>
<body> <script type="text/javascript" src="helloworld.js"></script> </body> </html> -
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello Kotlin/JS</title> </head>
<body> <script type="text/javascript" src="out/production/HelloJS/lib/kotlin.js"></script> <script type="text/javascript" src="out/production/HelloJS/HelloJS.js"></script> </body> </html> -
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello Kotlin/JS</title> </head>
<body> <script type="text/javascript" src="out/production/HelloJS/lib/kotlin.js"></script> <script type="text/javascript" src="out/production/HelloJS/HelloJS.js"></script> </body> </html> -
Calling JavaScript from Kotlin Kotlin All The Things Javascript ⬌
Kotlin fun main(args: Array<String>) { js("console.log('Hello World')") } -
Calling JavaScript from Kotlin Kotlin All The Things Javascript ⬌
Kotlin fun main(args: Array<String>) { js("console.log('Hello World')") } -
Calling JavaScript from Kotlin Kotlin All The Things Javascript ⬌
Kotlin external fun alert(message: Any?): Unit fun main(args: Array<String>) { alert("All The Things!") } -
Calling JavaScript from Kotlin Kotlin All The Things Javascript ⬌
Kotlin external fun alert(message: Any?): Unit fun main(args: Array<String>) { alert("All The Things!") } -
Calling JavaScript from Kotlin Kotlin All The Things Javascript ⬌
Kotlin import org.w3c.dom.Window external val window: Window fun main(args: Array<String>) { window.document.location?.href ="https://www.google.com" } -
Calling JavaScript from Kotlin Kotlin All The Things Javascript ⬌
Kotlin https://kotlinlang.org/docs/reference/dynamic-type.html The Dynamic Type val dyn: dynamic = null dyn.foo().bar.baz() -
Calling Kotlin from JavaScript Kotlin All The Things Javascript ⬌
Kotlin Kotlin Module: myModule fun foo() = "Hello" JavaScript alert(myModule.foo()); -
Calling Kotlin from JavaScript Kotlin All The Things Javascript ⬌
Kotlin Kotlin Module: myModule package my.qualified.packagename fun foo() = "Hello" JavaScript alert(myModule.my.qualified.packagename.foo()); -
Calling Kotlin from JavaScript Kotlin All The Things Javascript ⬌
Kotlin Kotlin Module: myModule class Person(val name: String) { fun hello() = println("Hello $name!") fun hello(greeting: String) = println("$greeting $name!") } -
Calling Kotlin from JavaScript Kotlin All The Things Javascript ⬌
Kotlin Kotlin Module: myModule class Person(val name: String) { fun hello() = println("Hello $name!") fun hello(greeting: String) = println("$greeting $name!") } -
Calling Kotlin from JavaScript Kotlin All The Things Javascript ⬌
Kotlin Kotlin Module: myModule class Person(val name: String) { fun hello() = println("Hello $name!") @JsName("helloWithGreeting") fun hello(greeting: String) = println("$greeting $name!") } -
Calling Kotlin from JavaScript Kotlin All The Things Javascript ⬌
Kotlin Kotlin Module: myModule class Person(val name: String) { fun hello() = println("Hello $name!") @JsName("helloWithGreeting") fun hello(greeting: String) = println("$greeting $name!") } JavaScript var person = new myModule.Person("Dmitry"); myModule.hello(); myModule.helloWithGreeting("Servus"); -
Kotlin All The Things Kotlin/JS Tooling apply plugin: 'org.jetbrains.kotlin.frontend' kotlinFrontend
{ npm { dependency "style-loader" devDependency "karma" } } -
Kotlin All The Things Kotlin/JS Tooling apply plugin: 'org.jetbrains.kotlin.frontend' kotlinFrontend
{ webpackBundle { bundleName = "main" sourceMapEnabled = true publicPath = "/" host = "localhost" port = 8088 } } -
Kotlin All The Things Kotlin/JS + React David Ford -
Talking Kotlin http://talkingkotlin.com/react-with-kotlin/ -
Kotlin All The Things Kotlin/JS + React JavaScript class ShoppingList
extends React.Component { render() { return ( <div className="shopping-list"> <h1>Shopping List for {this.props.name}</h1> <ul> <li>Instagram</li> <li>WhatsApp</li> <li>Oculus</li> </ul> </div> ); } } -
Kotlin All The Things Kotlin/JS + React JavaScript class ShoppingList
extends React.Component { render() { return ( <div className="shopping-list"> <h1>Shopping List for {this.props.name}</h1> <ul> <li>Instagram</li> <li>WhatsApp</li> <li>Oculus</li> </ul> </div> ); } } HTML <ShoppingList name="Mark" /> -
Kotlin All The Things Kotlin/JS + React JavaScript class ShoppingList
extends React.Component { render() { return ( <div className="shopping-list"> <h1>Shopping List for {this.props.name}</h1> <ul> <li>Instagram</li> <li>WhatsApp</li> <li>Oculus</li> </ul> </div> ); } } -
Kotlin All The Things Kotlin/JS + React Kotlin class ShoppingList
: ReactDOMComponent<ShoppingList.Props, ShoppingList.State>(){ override fun ReactDOMBuilder.render() { div(classes = "shopping-list") { h1 { +"Shopping List for ${props.name}" } ul{ li{ + "Instagram" } li{ + "WhatsApp" } li{ + "Oculus" } } } } } -
Kotlin All The Things Kotlin/JS + React Kotlin class ShoppingList
: ReactDOMComponent<ShoppingList.Props, ShoppingList.State>(){ override fun ReactDOMBuilder.render() { div(classes = "shopping-list") { h1 { +"Shopping List for ${props.name}" } ul{ li{ + "Instagram" } li{ + "WhatsApp" } li{ + "Oculus" } } } } } -
Kotlin All The Things Kotlin/JS + React Create React Kotlin
App • Scaffold a kotlin react app • Based on the “create-react-app” command • Maintained by JetBrains ◦ https://github.com/JetBrains/create-react-kotlin-app -
Kotlin All The Things Kotlin/JS Wrappers https://github.com/JetBrains/kotlin-wrappers • React •
Redux • Mocca • Styled Components (https://www.styled-components.com) https://github.com/Kotlin/js-externals • jQuery -
• Compile Kotlin Code to Native Binaries (non-VM world) •
LLVM based backend • Supports two-way interoperability ◦ Executable on multiple platforms ◦ Static Library with C headers for C/C++ ◦ Use existing static/dynamic libraries ◦ Supports Objective-C types • Produces executables and libraries/frameworks. Kotlin Native Kotlin All The Things -
Kotlin Native Kotlin All The Things Source KONANC LLVM Native
Binary LLVM IR Front End Back End -
Supported Platforms • Android (arm32, arm64) • Windows (mingw, x86_64)
• Linux (x86_64, arm32, MIPSm, MIPS little endian) • WebAssembly (wasm32) • MacOS (x86_64) • iOS (arm32, arm64, emulator x86_64) Kotlin Native Kotlin All The Things -
Supported Platforms • Android (arm32, arm64) • Windows (mingw, x86_64)
• Linux (x86_64, arm32, MIPSm, MIPS little endian) • WebAssembly (wasm32) • MacOS (x86_64) • iOS (arm32, arm64, emulator x86_64) Kotlin Native Kotlin All The Things -
Getting Started • Download Intellij and make sure to update
the Kotlin tooling to 1.3+ Or • Download the compiler: https://github.com/JetBrains/kotlin/releases/tag/v1.3.11 • Extract and add to your path. Kotlin Native Kotlin All The Things -
Kotlin Native Kotlin All The Things package sample fun hello():
String = "Hello, Kotlin/Native!" fun main() { println(hello()) } -
Kotlin Native Kotlin All The Things package sample fun hello():
String = "Hello, Kotlin/Native!" fun main() { println(hello()) } konanc MySample.kt -e sample.main -
Kotlin Native Kotlin All The Things package sample fun hello():
String = "Hello, Kotlin/Native!" fun main() { println(hello()) } konanc MySample.kt -e sample.main -
Kotlin Native Kotlin All The Things konanc MySample.kt -e sample.main
-produce framework -target <ios_x64 | ios_arm64> -output <directory> https://kotlinlang.org/docs/tutorials/native/apple-framework.html -
Kotlin Native Kotlin All The Things konanc MySample.kt -e sample.main
-produce framework -target <ios_x64 | ios_arm64> -output <directory> https://kotlinlang.org/docs/tutorials/native/apple-framework.html -
#import <Foundation/Foundation.h> @class DemoKotlinArray; @protocol DemoKotlinIterator; NS_ASSUME_NONNULL_BEGIN @interface KotlinBase :
NSObject - (instancetype)init __attribute__((unavailable)); + (instancetype)new __attribute__((unavailable)); + (void)initialize __attribute__((objc_requires_super)); @end; ... __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("MySampleKt"))) @interface DemoMySampleKt : KotlinBase + (void)mainArgs:(DemoKotlinArray *)args __attribute__((swift_name("main(args:)"))); @end; ... -
#import <Foundation/Foundation.h> @class DemoKotlinArray; @protocol DemoKotlinIterator; NS_ASSUME_NONNULL_BEGIN @interface KotlinBase :
NSObject - (instancetype)init __attribute__((unavailable)); + (instancetype)new __attribute__((unavailable)); + (void)initialize __attribute__((objc_requires_super)); @end; ... __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("MySampleKt"))) @interface DemoMySampleKt : KotlinBase + (void)mainArgs:(DemoKotlinArray *)args __attribute__((swift_name("main(args:)"))); @end; ... -
Kotlin Native Kotlin All The Things https://github.com/JetBrains/kotlin-native/tree/master/samples/opengl import platform.GLUT.* import
platform.OpenGL.* import platform.OpenGLCommon.* -
Kotlin Native Kotlin All The Things https://github.com/JetBrains/kotlin-native/tree/master/samples/opengl fun main() {
// initialize and run program memScoped { ... } // Display Mode glutInitDisplayMode((GLUT_RGB or GLUT_DOUBLE or GLUT_DEPTH).convert()) // Set window size glutInitWindowSize(windowWidth, windowHeight) // create Window glutCreateWindow("The GLUT Teapot") initialize() // run GLUT mainloop glutMainLoop() } -
Kotlin Native Kotlin All The Things https://github.com/JetBrains/kotlin-native/tree/master/samples/opengl fun main() {
// initialize and run program memScoped { ... } // Display Mode glutInitDisplayMode((GLUT_RGB or GLUT_DOUBLE or GLUT_DEPTH).convert()) // Set window size glutInitWindowSize(windowWidth, windowHeight) // create Window glutCreateWindow("The GLUT Teapot") initialize() // run GLUT mainloop glutMainLoop() } -
Kotlin Native Kotlin All The Things https://github.com/JetBrains/kotlin-native/tree/master/samples/opengl fun initialize() {
// select projection matrix glMatrixMode(GL_PROJECTION) // set the viewport glViewport(0, 0, windowWidth, windowHeight) // set matrix mode glMatrixMode(GL_PROJECTION) // reset projection matrix glLoadIdentity() val aspect = windowWidth.toDouble() / windowHeight // set up a perspective projection matrix gluPerspective(45.0, aspect, 1.0, 500.0) // specify which matrix is the current matrix glMatrixMode(GL_MODELVIEW) glShadeModel(GL_SMOOTH) ... } -
Kotlin Native Kotlin All The Things https://github.com/JetBrains/kotlin-native/tree/master/samples/opengl fun initialize() {
// select projection matrix glMatrixMode(GL_PROJECTION) // set the viewport glViewport(0, 0, windowWidth, windowHeight) // set matrix mode glMatrixMode(GL_PROJECTION) // reset projection matrix glLoadIdentity() val aspect = windowWidth.toDouble() / windowHeight // set up a perspective projection matrix gluPerspective(45.0, aspect, 1.0, 500.0) // specify which matrix is the current matrix glMatrixMode(GL_MODELVIEW) glShadeModel(GL_SMOOTH) ... } -
• Set of tools to help share app data/domain code
natively across platforms ◦ Gradle Plugin ◦ Compile for multiple “targets” ◦ Common + Platform specific code ▪ Expect in common code, Actual in platform code. • Still experimental • It’s not meant to share all code Kotlin Multiplatform Kotlin All The Things -
• Data Models • Validation Rules • Network Client Code
• HTML Generation What to share? Kotlin All The Things -
• Rx anything • Threading • Database • UI What
not to share? Kotlin All The Things -
Kotlin Multiplatform Kotlin All The Things https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html plugins { id
'kotlin-multiplatform' version '1.3.21' } -
Kotlin Multiplatform Kotlin All The Things https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html kotlin { jvm()
js() { compilations.all { kotlinOptions { moduleKind = "umd" } } } android("android") iosX64("ios") { compilations.main.outputKinds("framework") } sourceSets { ... } } -
Kotlin Multiplatform Kotlin All The Things https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html kotlin { ...
sourceSets { commonMain { dependencies { implementation kotlin('stdlib-common') } } commonTest { dependencies { implementation kotlin('test-common') implementation kotlin('test-annotations-common') } } jvmMain { dependencies {...} } jvmTest { dependencies {...} } jsMain { dependencies {...} } jsTest { dependencies {...} } androidMain { dependencies {...} } androidTest { dependencies {...} } iosMain {} iosTest {} } } -
Kotlin Multiplatform Kotlin All The Things https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html kotlin { ...
sourceSets { commonMain { dependencies { implementation kotlin('stdlib-common') } } commonTest { dependencies { implementation kotlin('test-common') implementation kotlin('test-annotations-common') } } jvmMain { dependencies {...} } jvmTest { dependencies {...} } jsMain { dependencies {...} } jsTest { dependencies {...} } androidMain { dependencies {...} } androidTest { dependencies {...} } iosMain {} iosTest {} } } -
Kotlin Multiplatform Kotlin All The Things https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html kotlin { ...
sourceSets { commonMain { dependencies { implementation kotlin('stdlib-common') } } commonTest { dependencies { implementation kotlin('test-common') implementation kotlin('test-annotations-common') } } jvmMain { dependencies {...} } jvmTest { dependencies {...} } jsMain { dependencies {...} } jsTest { dependencies {...} } androidMain { dependencies {...} } androidTest { dependencies {...} } iosMain {} iosTest {} } } -
Kotlin Multiplatform Kotlin All The Things https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html kotlin { ...
sourceSets { commonMain { dependencies { implementation kotlin('stdlib-common') } } commonTest { dependencies { implementation kotlin('test-common') implementation kotlin('test-annotations-common') } } jvmMain { dependencies {...} } jvmTest { dependencies {...} } jsMain { dependencies {...} } jsTest { dependencies {...} } androidMain { dependencies {...} } androidTest { dependencies {...} } iosMain {} iosTest {} } } -
Kotlin Multiplatform Kotlin All The Things https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html kotlin { ...
sourceSets { commonMain { dependencies { implementation kotlin('stdlib-common') } } commonTest { dependencies { implementation kotlin('test-common') implementation kotlin('test-annotations-common') } } jvmMain { dependencies {...} } jvmTest { dependencies {...} } jsMain { dependencies {...} } jsTest { dependencies {...} } androidMain { dependencies {...} } androidTest { dependencies {...} } iosMain {} iosTest {} } } -
Kotlin Multiplatform Kotlin All The Things https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html kotlin { ...
sourceSets { commonMain { dependencies { implementation kotlin('stdlib-common') } } commonTest { dependencies { implementation kotlin('test-common') implementation kotlin('test-annotations-common') } } jvmMain { dependencies {...} } jvmTest { dependencies {...} } jsMain { dependencies {...} } jsTest { dependencies {...} } androidMain { dependencies {...} } androidTest { dependencies {...} } iosMain {} iosTest {} } } -
Kotlin Multiplatform Kotlin All The Things https://github.com/JetBrains/kotlin-mpp-example/ class Calculator {
companion object { fun sum(a: Int, b: Int): Int = a + b } } -
Kotlin Multiplatform Kotlin All The Things https://github.com/JetBrains/kotlin-mpp-example/ class Greeting {
fun greeting(): String = "Hello, ${Platform().platform}" } -
Kotlin Multiplatform Kotlin All The Things https://github.com/JetBrains/kotlin-mpp-example/ class Greeting {
fun greeting(): String = "Hello, ${Platform().platform}" } -
Kotlin Multiplatform Kotlin All The Things https://github.com/JetBrains/kotlin-mpp-example/ > commonMain expect
class Platform() { val platform: String } class Greeting { fun greeting(): String = "Hello, ${Platform().platform}" } -
Kotlin Multiplatform Kotlin All The Things https://github.com/JetBrains/kotlin-mpp-example/ > iosMain actual
class Platform actual constructor() { actual val platform: String = "iOS" } > androidMain actual class Platform actual constructor() { actual val platform: String = "Android" } -
Kotlin Multiplatform Kotlin All The Things https://github.com/JetBrains/kotlin-mpp-example/ > commonMain expect
class Date > jvmMain actual typealias Date = java.util.Date > jsMain actual typealias Date = kotlin.js.Date -
Kotlin Multiplatform Kotlin All The Things https://github.com/JetBrains/kotlin-mpp-example/ > commonMain expect
class Date > jvmMain actual typealias Date = java.util.Date > jsMain actual typealias Date = kotlin.js.Date -
Why Expect/Actual vs Interface? Kotlin All The Things GOTO 2017
• Kotlin - One Language, All Tiers: Developing Multiplatform Projects • Dmitry Jemerov • Much more flexible • You can “expect” top level functions and extension functions, too. • Allows you to reuse existing implementation if needed. -
• Kotlin is a modern, pragmatic, industry built language with
a great community. • Kotlin became popular on the JVM, but that’s not the only platform for the language. • Multiplatform projects enable us to use Kotlin natively everywhere. -
• Docs https://kotlinlang.org/ • Talking Kotlin http://talkingkotlin.com/ • Kotlin Slack
http://slack.kotlinlang.org/ • Github https://www.github.com/myotive/kotlin-all-the-things • Slides https://speakerdeck.com/myotive/kotlin-all-the-things