Share
Embed
Transcript
115 slides
-
Kotlin Multiplatform for Android/iOS devs Paolo Rotolo Mobile developer @
Nextome Anna Labellarte Mobile developer @ Nextome -
What is Kotlin Multiplatform Kotlin/Native • No Vi rt ual
Machine • Targets • iOS • Windows, Linux, Mac • Small devices • Interop with C and obj-C Kotlin/JVM • 100 % interop with Java -
KMM Roadmap 2017 Kotlin multiplatform Coroutines First class language for
Android Support for iOS Alpha Beta 2018 2019 2020 2022 2022 -
Why we decided to give it a shot - SDK
product workflow -
Roadmap 2. Transform the existing android project Room Retro fi
t Resources DI DateTime -
Roadmap 4. Go on with multipla tf orm 5. Share
as much code as we can -
Pros & Cons ✅ Easy to use ✅ Low risks
✅ Easy integration with native code ✅ Shared codebase ✅ Active Community -
Pros & Cons ❌ Code generated not Swi ft -friendly
❌ Build time can be slow on Xcode ❌ Few multipla tf orm libraries -
Tools Android Studio Kotlin Multipla tf orm Plugin Xcode /
AppCode Kotlin Native Plugin -
kotlin { android() ios() sourceSets { val commonMain by getting
{ dependencies { implementation( "io.ktor:ktor-client-core:$ktor_version") } } val androidMain by getting { dependencies { implementation( "androidx.work:work-runtime-ktx:$work_version") } } val iosMain by getting } } -
val commonMain by getting { dependencies { implementation( "io.ktor:ktor-client-core:$ktor_version") }
} val androidMain by getting { dependencies { implementation( "androidx.work:work-runtime-ktx:$work_version") } } val iosMain by getting } } val commonTest by getting val androidTest by getting val iosTest by getting -
plugins { kotlin("multiplatform") kotlin("native.cocoapods") id("com.android.library") } kotlin { android() ios()
cocoapods { pod("SSZipArchive") } sourceSets { val commonMain by getting { dependencies { implementation( "io.ktor:ktor-client-core:$ktor_version" } } -
package com.nextome.hellokmm class Greeting { private val platform: Platform =
getPlatform() fun greet(): String { return "Hello, ${platform.name}!" } } -
package com.nextome.hellokmm interface Platform { val name: String } expect
fun getPlatform(): Platform -
package com.nextome.hellokmm import android.os.Build.VERSION class AndroidPlatform : Platform { override
val name: String = "Android ${VERSION.SDK_INT}" } actual fun getPlatform(): Platform = AndroidPlatform() -
package com.nextome.hellokmm import platform.UIKit.UIDevice class IOSPlatform: Platform { override val
name: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion } actual fun getPlatform(): Platform = IOSPlatform() -
/ / build.gradle.kts (:shared) plugins { kotlin("multiplatform") id(“com.android.library”) id("maven-publish") }
group = "com.nextome.hellokmm" version = "1.0.0" -
// build.gradle.kts (:shared) plugins { kotlin("multiplatform") id(“com.android.library”) id("maven-publish") } group
= "com.nextome.hellokmm" version = “1.0.0" kotlin { android{ publishLibraryVariants("release", "debug") } // [ ... ] } -
./gradlew publishToMavenLocal // build.gradle.kts (:shared) plugins { kotlin("multiplatform") id(“com.android.library”) id("maven-publish")
} group = "com.nextome.hellokmm" version = “1.0.0" kotlin { android{ publishLibraryVariants("release", "debug") } -
Ktor suspend fun main() { val client = HttpClient() val
response: HttpResponse = client.get("https: / / ktor.io/") println(response.status) client.close() } -
Ktor suspend fun main() { val client = HttpClient() {
install(ContentNegotiation) { json() } } val response: HttpResponse = client.get("https: / println(response.status) client.close() } -
install(Auth) { bearer { loadTokens { bearerTokenStorage.last() } refreshTokens {
val refresh = this.oldTokens.refreshToken getNewToken(refresh) } } } suspend fun main() { val client = HttpClient() { install(ContentNegotiation) { json() } } -
bearerTokenStorage.last() } refreshTokens { val refresh = this.oldTokens.refreshToken getNewToken(refresh) }
} } } / install(Logging) { logger = Logger.DEFAULT level = LogLevel.HEADERS filter { request -> request.url.host.contains("ktor.io") } } -
/ filter { request -> request.url.host.contains("ktor.io") } } HttpResponseValidator {
handleResponseExceptionWithRequest { exception, request -> val clientException = exception as? ClientRequestException ?: return@handleResponseExceptionWithRequest when (clientException.response.status) { HttpStatusCode.NotFound - > { throw MissingPageException(exceptionResponse) } } } } -
kotlinx-serialization @Serializable data class User(val name: String, val email: String)
fun main() { val data = User("Paolo", “[email protected]”) val serializedData: String = Json.encodeToString(data) println(serializedData) // {“name”:"Paolo","email":"[email protected]"} val deserializedData: User = Json.decodeFromString(serializedData) println(deserializedData) // Project(name=Paolo, [email protected]) } -
Room Rx*, LiveData Timber Hilt, Koin SqlDelight / Realm Coroutines
& Flow Kermit Koin ➡ ➡ ➡ ➡ Retrofit, OkHttp Gson, Moshi ➡ Ktor ➡ kotlinx-serialization -
Data Types fun insertNewTodo( title: String, priority: Int ) repository.insertNewTodo(
title: String, priority: ? Int32 ) -
Data Types fun insertNewTodo( title: String, priority: Int ) repository.insertNewTodo(
title: String, priority: ? ) KotlinInt? -
Coroutines class TodoRepository { suspend fun fetchTodo(): List<Todo> { val
todos = getTodoFromServer() saveToDb(todos) return todos } } -
Coroutines class TodoRepository { suspend fun fetchTodo(): List<Todo> { val
todos = getTodoFromServer() saveToDb(todos) return todos } } scope.launch { val todo = TodoRepository().fetchTodo() } -
Coroutines class TodoRepository { suspend fun fetchTodo(): List<Todo> { val
todos = getTodoFromServer() saveToDb(todos) return todos } } TodoRepository().fetchTodo {todos, error in } func loadTodo() async throws { let todo = try await TodoRepository().fetchTodo() } -
Coroutines class TodoRepository { suspend fun fetchTodo(): List<Todo> { val
todos = getTodoFromServer() saveToDb(todos) return todos } } scope.launch { val todo = TodoRepository().fetchTodo() } -
Coroutines class TodoRepository { suspend fun fetchTodo(): List<Todo> { val
todos = getTodoFromServer() saveToDb(todos) return todos } } scope.launch { val todo = TodoRepository().fetchTodo() } scope.cancel() -
Koru Inspired by https://touchlab.co/kotlin-coroutines-rxswift/ plugins { // add ksp and
koru compiler plugin id("com.google.devtools.ksp") version "1.6.21-1.0.6" id("com.futuremind.koru").version("0.11.1") } kotlin { sourceSets { val commonMain by getting { dependencies { // add library dependency implementation("com.futuremind:koru:0.11.1") } } val iosMain by creating { ... } } } koru { nativeSourceSetNames = listOf("iosMain") } -
Coroutines class TodoRepository { suspend fun fetchTodo(): List<Todo> { val
todos = getTodoFromServer() saveToDb(todos) return todos } } -
Coroutines @ToNativeClass(name = "TodoRepositoryIos") class TodoRepository { suspend fun fetchTodo():
List<Todo> { val todos = getTodoFromServer() saveToDb(todos) return todos } } -
// build/generated/ksp/TodoRepositoryIos.kt public class TodoRepositoryIos( private val wrapped: TodoRepository, private
val scopeProvider: ScopeProvider?, ) { public constructor(wrapped: TodoRepository) : this(wrapped,exportedScopeProvider_mainScopeProvider) public fun fetchTodo(): SuspendWrapper<List<Todo> = SuspendWrapper(scopeProvider, false) { wrapped.fetchTodo() } } -
Coroutines @ToNativeClass(name = "TodoRepositoryIos") class TodoRepository { suspend fun fetchTodo():
List<Todo> { val todos = getTodoFromServer() saveToDb(todos) return todos } } -
Coroutines @ToNativeClass(name = "TodoRepositoryIos") class TodoRepository { suspend fun fetchTodo():
List<Todo> { val todos = getTodoFromServer() saveToDb(todos) return todos } } let repo = TodoRepositoryIos( wrapped: TodoRepository(), scope: coroutineScope) repo.getTodoWrapped().subscribe( onSuccess: { (array: NSArray?) -> () in }, onThrow: { throwable in }) -
Coroutines @ToNativeClass(name = "TodoRepositoryIos") class TodoRepository { suspend fun fetchTodo():
List<Todo> { val todos = getTodoFromServer() saveToDb(todos) return todos } } let repo = TodoRepositoryIos( wrapped: TodoRepository(), scope: coroutineScope) repo.getTodoWrapped().subscribe( onSuccess: { (array: NSArray?) -> () in }, onThrow: { throwable in }) SuspendWrapper<List<Todo >> -
@ToNativeClass(name = "TodoRepositoryIos") class TodoRepository { suspend fun fetchTodo(): List<Todo>
{ val todos = getTodoFromServer() saveToDb(todos) return todos } } -
@ToNativeClass(name = "TodoRepositoryIos", launchOnScope = MainScopeProvider :: class) class TodoRepository
{ suspend fun fetchTodo(): List<Todo> { val todos = getTodoFromServer() saveToDb(todos) return todos } } @ExportedScopeProvider class MainScopeProvider : ScopeProvider { override val scope : CoroutineScope = MainScope() } -
@ToNativeClass(name = "TodoRepositoryIos", launchOnScope = MainScopeProvider :: class) class TodoRepository
{ suspend fun fetchTodoList(): TodoList { val todos = getTodoFromServer() saveToDb(todos) return TodoList(todos) } } @ExportedScopeProvider class MainScopeProvider : ScopeProvider { override val scope : CoroutineScope = MainScope() } data class TodoList(val list: List<Todo>) -
@ToNativeClass(name = "TodoRepositoryIos", launchOnScope = MainScopeProvider :: class) class TodoRepository
{ suspend fun fetchTodoList(): TodoList { val todos = getTodoFromServer() saveToDb(todos) return TodoList(todos) } } Coroutines let repo = TodoRepositoryIos(wrapped: TodoRepository()) repo.fetchTodoList().subscribe(onSuccess: { (list: TodoList?) in }, onThrow: { (throwable: KotlinThrowable) in }) -
@ToNativeClass(name = "TodoRepositoryIos", launchOnScope = MainScopeProvider :: class) class TodoRepository
{ suspend fun fetchTodoList(): TodoList { val todos = getTodoFromServer() saveToDb(todos) return TodoList(todos) } } Coroutines let repo = TodoRepositoryIos(wrapped: TodoRepository()) let job = repo.fetchTodoList().subscribe(onSuccess: { (list: TodoList?) in }, onThrow: { (throwable: KotlinThrowable) in }) job.cancel(cause: KotlinCancellationException(message: "Stop it!")) -
Flow class RandomIntGenerator { fun generateEach(interval: Long) = flow {
while(true) { delay(interval) emit(Random.nextInt()) } } } -
Flow class RandomIntGenerator { fun generateEach(interval: Long) = flow {
while(true) { delay(interval) emit(Random.nextInt()) } } } lifecycleScope.launch { RandomIntGenerator().generateEach(ONE_SECOND).collect { print(it) } } -
Flow class RandomIntGenerator { fun generateEach(interval: Long) = flow {
while(true) { delay(interval) emit(Random.nextInt()) } } } RandomIntGenerator() .generateEachTest(interval: Int64(1000)) .collect(collector: Collector()) { error in } class Collector: Kotlinx_coroutines_coreFlowCollector{ func emit(value: Any?, completionHandler: @escaping (Error?) -> Void) { print(value) completionHandler(nil) } } -
RandomIntGenerator() .generateEachTest(interval: Int64(1000)) .collect(collector: Collector()) { error in } class
Collector: Kotlinx_coroutines_coreFlowCollector{ func emit(value: Any?, completionHandler: @escaping (Error?) -> Void) { print(value) completionHandler(nil) } } -
RandomIntGenerator() .generateEach(interval: ONE_SECOND) .collect(collector: Collector<KotlinInt>() { value in print(value) })
{ (error) in print(error?.localizedDescription) } class Collector<T>: Kotlinx_coroutines_coreFlowCollector { let callback:(T) -> Void init(callback: @escaping (T) -> Void) { self.callback = callback } func emit(value: Any?, completionHandler: @escaping (Error?) -> Void) { callback(value as! T) completionHandler(nil) } } -
class RandomIntGenerator { fun generateEach(interval: Long) = flow { while(true)
{ delay(interval) emit(Random.nextInt()) } } } -
class RandomIntGenerator { fun generateEach(interval: Long) = flow { while(true)
{ delay(interval) emit(Random.nextInt()) } } } fun <T> Flow<T>.wrap(): CFlow<T> = CFlow(this) class CFlow<T>(private val origin: Flow<T>) : Flow<T> by origin { fun watch(block: (T) -> Unit): Closeable { val job = Job() onEach { block(it) }.launchIn(CoroutineScope(Dispatchers.Main + job)) return object : Closeable { override fun close() { job.cancel() } } } } -
class RandomIntGenerator { fun generateEach(interval: Long): CFlow<Int> = flow {
while(true) { delay(interval) emit(Random.nextInt()) } }.wrap() } fun <T> Flow<T>.wrap(): CFlow<T> = CFlow(this) class CFlow<T>(private val origin: Flow<T>) : Flow<T> by origin { fun watch(block: (T) > val job = Job() onEach { block(it) }.launchIn(CoroutineScope(Dispatchers.Main + job)) return object : Closeable { override fun close() { job.cancel() } } } } -
class RandomIntGenerator { fun generateEach(interval: Long): CFlow<Int> = flow {
while(true) { delay(interval) emit(Random.nextInt()) } }.wrap() } RandomIntGenerator() .generateEach(interval: ONE_SECOND) .watch { (value: KotlinInt?) in print(value) } -
let disposable = RandomIntGenerator() .generateEach(interval: ONE_SECOND) .watch { (value: KotlinInt?)
in print(value) } disposable.close() class RandomIntGenerator { fun generateEach(interval: Long): CFlow<Int> = flow { while(true) { delay(interval) emit(Random.nextInt()) } }.wrap() } -
Sealed Class when (uiState) { is UIState.Data -> TODO() is
UIState.Error - > TODO() is UIState.Loading - > TODO() } -
In the end… Share common logic in Kotlin Work together
to make it work be tt er! -
In the end… Share common logic in Kotlin Work together
to make it work be tt er! Sometimes it could be ugly but we’re ge tt ing there (fast)