Share
Embed
Jetpack for KMP
2024년 07월 20일 (토) I/O Extended Android in Korea 2024 발표자료입니다.
https://festa.io/events/5509
Sungyong An
July 20, 2024
Transcript
94 slides
-
Intro Function, UseCase ١ਵ۽ ٘ܳ ܻ࠙ೞח Ѫ োझۣ. ೞա ۽ંী
߈ࠂغח ٘о ݶ? fun doSomething() { ... // repeated codes ... // repeated codes ... } fun doSomething() { ... repeat() ... repeat() ... } fun repeat() { // repeated codes } -
Project A fun doSomething() { repeat() } Project B Intro
زੌೠ ۖಬۄݶ ۄ࠳۞ܻ۽ ٘ܳ ܻ࠙ೞৈ ઁҕೠ. ৈ۞ ۽ંী ߈ࠂغח ٘о ݶ? Library fun repeat() { // repeated codes } fun doSomething() { repeat() } -
Intro JVM ߄ ߄٘ܳ ೧ࢳೞৈ যڃ ۖಬীࢲب زੌೞѱ प೯ೡ ࣻ
. ৈ۞ ۖಬী زੌೠ ٘ ࢎਊೞח ߑߨ? Link: h tt ps://en.wikipedia.org/wiki/Java_vi rt ual_machine JVM OS Windows Linux macOS Hardware Program Program ... ... -
Intro Kotlin Multiplatformਸ ࢎਊೞח Ѫب ೞա ߑߨ! ৈ۞ ۖಬী ߈ࠂغח
٘о ݶ? Link: h tt ps://www.jetbrains.com/kotlin-multipla tf orm/ Client Multiplatform Common Multiplatform Server Android iOS Desktop Web -
// commonMain fun greet(platform: String = "Kotlin Multiplatform"): String {
return "Hello, $platform!" } // androidMain greet(platform = "Android") // iosMain greet(platform = "iOS") // desktopMain greet(platform = "Desktop") ✅ -
// commonMain fun greet(): String { return "Hello, ${platform()}!" }
// androidMain fun platform() = "Android" // iosMain fun platform() = "iOS" // desktopMain fun platform() = "Desktop" ❌ -
Section 1 commonMain/ androidMain/ iosMain/ desktopMain/ expect - actual expect
actual actual actual ✅ Link: h tt ps://kotlinlang.org/docs/multipla tf orm-expect-actual.html -
// commonMain fun greet(): String { return "Hello, ${platform()}!" }
expect fun platform() // androidMain actual fun platform() = "Android" // iosMain actual fun platform() = "iOS" // desktopMain actual fun platform() = “Desktop" ✅ -
// commonMain fun greet(platform: Platform): String { return "Hello, ${platform.name}!"
} expect class Platform { val name: String } // androidMain actual class Platform { actual val name: String get() = "Android" } // iosMain actual class Platform { actual val name: String get() = "iOS" } // desktopMain actual class Platform { actual val name: String get() = "Desktop" } ⚠ Beta -
Supported Platforms Section 1 Link: h tt ps://www.jetbrains.com/help/kotlin-multipla tf orm-dev/suppo
rt ed-pla tf orms.html Android Stable iOS Stable Desktop (JVM) Stable Server-side (JVM) Stable Web based on Kotlin/Wasm Alpha Web based on Kotlin/JS Stable watchOS, tvOS Best effort -
Section 1 Targets kotlin { androidTarget() iosX64() iosArm64() iosSimulatorArm64() }
common jvm android native js androidNative apple mingw linux ios macos tvos watchos iosSimulatorArm64 iosX64 iosArm64 Link: h tt ps://kotlinlang.org/docs/multipla tf orm-discover-project.html#targets -
kotlin { sourceSets { commonMain.dependencies { implementation(libs.ktor.client.core) } androidMain.dependencies {
implementation(libs.ktor.client.okhttp) } iosMain.dependencies { implementation(libs.ktor.client.darwin) } } } Source sets Section 1 Link: h tt ps://kotlinlang.org/docs/multipla tf orm-discover-project.html#source-sets commonMain androidMain iosMain -
sourceSets { val commonMain by getting { dependencies { ...
} } val jvmAndroidMain by creating { dependsOn(commonMain) dependencies { ... } } val androidMain by getting { dependsOn(jvmAndroidMain) } val desktopMain by getting { dependsOn(jvmAndroidMain) } } Additional source sets Section 1 desktopMain jvmAndroidMain androidMain commonMain -
// Android - build.gradle.kts plugins { id("com.android.application") id("org.jetbrains.kotlin.android") } android
{ ... } dependencies { ... } -
// Kotlin Multiplatform - build.gradle.kts plugins { id("com.android.application") id("org.jetbrains.kotlin.multiplatform") }
android { ... } kotlin { androidTarget() sourceSets { commonMain.dependencies { ... } androidMain.dependencies { ... } } } -
🗂 org/jetbrains/kotlinx/ 🗂 kotlinx-coroutines-core/1.8.1/ - kotlinx-coroutines-core -1.8.1.jar 🗂 kotlinx-coroutines-core-jvm/1.8.1/ -
kotlinx-coroutines-core-jvm -1.8.1.jar 🗂 kotlinx-coroutines-core-iosarm64/1.8.1/ - kotlinx-coroutines-core-iosarm64 -1.8.1.klib KMP Library Section 1 Non-platform source set’s Refer to h tt ps://www.youtube.com/watch?v=vU48FVzBLtc -
kotlin { sourceSets { commonMain.dependencies { implementation("org.jetbrains.kotlinx: kotlinx-coroutines-core :1.8.1") }
jvmMain.dependencies { implementation("org.jetbrains.kotlinx: kotlinx-coroutines-core-jvm :1.8.1") } iosArm64Main.dependencies { implementation("org.jetbrains.kotlinx: kotlinx-coroutines-core-iosarm64 :1.8.1”) } } } ❌ -
kotlin { sourceSets { commonMain.dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1") } // jvmMain.dependencies
{ // implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.8.1") // } // iosArm64Main.dependencies { // implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-iosarm64:1.8.1") // } } } -
kotlin { sourceSets { commonMain.dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1") } } }
// jvmMain -> "org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.8.1" // iosArm64Main -> "org.jetbrains.kotlinx:kotlinx-coroutines-core-iosarm64:1.8.1" ✅ -
Android Jetpack Section 2 Link: h tt ps://developer.android.com/jetpack Collection Compose
DataStore Activity ConstraintLayout Annotation AppCompat Browser Camera Fragment Core Glance Lifecycle ViewPager Navigation Paging Preference RecyclerView Room ViewModel Window WorkManager -
App Domain UI App Architecture Section 2 Link: h tt
ps://developer.android.com/topic/architecture Data Collection Compose DataStore Activity ConstraintLayout Annotation AppCompat Browser Fragment Core Lifecycle ViewPager Navigation Paging Preference RecyclerView Room ViewModel Window WorkManager -
App Domain UI Jetpack supports KMP Section 2 Data Collection
Annotation Core Link: h tt ps://developer.android.com/kotlin/multipla tf orm DataStore Paging Preference Room WorkManager Compose Activity ConstraintLayout AppCompat Browser Fragment Lifecycle ViewPager Navigation RecyclerView ViewModel Window -
App Domain UI Jetpack supports KMP Section 2 Data Collection
Annotation Core Link: h tt ps://developer.android.com/kotlin/multipla tf orm DataStore Paging Preference Room WorkManager Compose Activity ConstraintLayout AppCompat Browser Fragment Lifecycle ViewPager Navigation RecyclerView ViewModel Window -
UI Domain Data To share Business Logic! Section 2 Collection
Link: h tt ps://developer.android.com/kotlin/multipla tf orm DataStore Room Repository UseCase Compose ViewModel Lifecycle -
• ArrayMap (JVM only) ਸ ઁ৻ೞݶ, ࠗ࠙ ஸ۩࣌ਸ Ӓ۽ ࢎਊೡ
ࣻ . • SparseArray, LruCache, ... • ArrayMap উਵ۽ ScatterMap ١ਸ ࢎਊೡ ࣻ . • Romain Guy “A Better Hash Map — 1” ଵҊ https://www.romainguy.dev/posts/2024/a-better-hashmap/ Collection Section 2 Link: h tt ps://developer.android.com/jetpack/androidx/releases/collection -
DataStore Section 2 androidx.datastore:datastore-core ✅ ✅ ✅ androidx.datastore:datastore-core-okio ✅ ✅
✅ androidx.datastore:datastore-preferences-core ✅ ✅ ✅ androidx.datastore:datastore-preferences ✅ ❌ ✅ module Android iOS Desktop Link: h tt ps://developer.android.com/jetpack/androidx/releases/datastore -
// Android // Setup dependencies { implementation("androidx.datastore:datastore-preferences:1.1.1") } // Create
val Context.dataStore: DataStore<Preferences> by preferencesDataStore("settings") Link: h tt ps://developer.android.com/topic/libraries/architecture/datastore -
// Android // Read val EXAMPLE_COUNTER = intPreferencesKey("example_counter") val exampleCounterFlow:
Flow<Int> = context.dataStore.data.map { preferences -> preferences[EXAMPLE_COUNTER] ?: 0 } // Write suspend fun incrementCounter() { context.dataStore.edit { settings -> val currentCounterValue = settings[EXAMPLE_COUNTER] ?: 0 settings[EXAMPLE_COUNTER] = currentCounterValue + 1 } } Link: h tt ps://developer.android.com/topic/libraries/architecture/datastore -
// Kotlin Multiplatform // Setup commonMain.dependencies { implementation("androidx.datastore:datastore-preferences-core:1.1.1") implementation("androidx.datastore:datastore-core-okio:1.1.1") implementation("com.squareup.okio:okio:3.9.0")
} Link: h tt ps://developer.android.com/kotlin/multipla tf orm/datastore -
// Kotlin Multiplatform // Create import okio.Path.Companion.toPath val dataStoreFileName =
"datastore-name.preferences_pb" val datastore: DataStore<Preferences> = PreferenceDataStoreFactory.createWithPath( produceFile = { produceFilePath().toPath() }, ) Link: h tt ps://developer.android.com/kotlin/multipla tf orm/datastore // androidMain fun produceFilePath(): String { return context.filesDir.resolve(dataStoreFileName).absolutePath } // iosMain fun produceFilePath(): String = “${fileDirectory()}/$dataStoreFileName" -
A modern I/O library Okio Section 2 Link: h tt
ps://square.github.io/okio/ -
Room, SQLite Section 2 Link: h tt ps://developer.android.com/kotlin/multipla tf orm
androidx.room:room-common ✅ ✅ ✅ androidx.room:room-runtime ✅ ✅ ✅ androidx.room:room-migration ✅ ✅ ✅ androidx.sqlite:sqlite ✅ ✅ ✅ androidx.sqlite:sqlite-framework ✅ ✅ ❌ androidx.sqlite:sqlite-bundled ✅ ✅ ✅ module Android iOS Desktop ❓ ❓ -
// Android // Setup dependencies { implementation("androidx.room:room-runtime:2.6.0") ksp("androidx.room:room-compiler:2.6.0") } //
Entity @Entity data class User( @PrimaryKey(autoGenerate = true) val id: Long = 0, @ColumnInfo(name = "name") val name: String, ) Link: h tt ps://developer.android.com/training/data-storage/room -
// Android // Data Access Object @Dao interface UserDao {
@Query("SELECT * FROM user”) fun getAll(): List<User> @Insert fun insertAll(vararg users: User) } // Database @Database(entities = [User::class], version = 1) abstract class AppDatabase : RoomDatabase() { abstract fun userDao(): UserDao } Link: h tt ps://developer.android.com/training/data-storage/room -
// Android // Create val dbFileName = "database-name" val db
= Room.databaseBuilder( applicationContext, AppDatabase::class.java, dbFileName, ).build() // Usage val userDao = db.userDao() val users: List<User> = userDao.getAll() Link: h tt ps://developer.android.com/training/data-storage/room -
// Kotlin Multiplatform // Setup commonMain.dependencies { implementation("androidx.room:room-runtime:2.7.0-alpha04") implementation("androidx.sqlite:sqlite-bundled:2.5.0-alpha04") }
dependencies { add("kspAndroid", "androidx.room:room-compiler:2.7.0-alpha04") add("kspIosSimulatorArm64", "androidx.room:room-compiler:2.7.0-alpha04") add("kspIosX64", "androidx.room:room-compiler:2.7.0-alpha04") add("kspIosArm64", "androidx.room:room-compiler:2.7.0-alpha04") } // Entity (Same) // Database (Same) Link: h tt ps://developer.android.com/kotlin/multipla tf orm/room -
// Kotlin Multiplatform // Data Access Object @Dao interface UserDao
{ // @Query("SELECT * FROM user") fun getAll(): List<User> @Query("SELECT * FROM user") fun getAll(): Flow<List<User>> // @Insert fun insertAll(vararg users: User) @Insert suspend fun insertAll(vararg users: User) } Link: h tt ps://developer.android.com/kotlin/multipla tf orm/room -
// Kotlin Multiplatform // Usage - androidMain val dbFileName =
"database-name.db" val db = Room.databaseBuilder<AppDatabase>( context = applicationContext, name = application.getDatabasePath(dbFileName), ) .setDriver(BundledSQLiteDriver()) // or AndroidSQLiteDriver .setQueryCoroutineContext(Dispatchers.IO) .build() Link: h tt ps://developer.android.com/kotlin/multipla tf orm/room ❓ -
Framework SQLite Android Common Native Android Native sqlite3.h android.database. SQLiteDatabase
Android SQLite Driver Native SQLite Driver room-runtime sqlite-framework -
Common Bundled SQLite Android Common Native Android Native sqlite3.c sqlite_jni.cpp
room-runtime sqlite-bundled Bundled SQLite Driver -
Link: h tt ps://developer.android.com/kotlin/multipla tf orm/room // Kotlin Multiplatform //
Usage - iosMain val dbFileName = "database-name.db" val db = Room.databaseBuilder<AppDatabase>( name = "${fileDirectory()}/$dbFileName", factory = { AppDatabase::class.instantiateImpl() } ) .setDriver(BundledSQLiteDriver()) // or NativeSQLiteDriver .setQueryCoroutineContext(Dispatchers.IO) .build() -
android/kotlin-multiplatform-samples Section 2 DiceRoller Fruitties Native UI + Preferences DataStore
Native UI + Okio DataStore + Room -
android/kotlin-multiplatform-samples Section 2 DiceRoller Fruitties Native UI + Preferences DataStore
Native UI + Okio DataStore + Room -
forked from androidx/androidx Compose Multiplatform Core Section 3 Link: h
tt ps://github.com/JetBrains/compose-multipla tf orm-core commonMain androidMain jbMain ... nativeMain desktopMain … webMain or skikoMain Jetpack Compose Compose Multiplatform -
// Jetpack Compose dependencies { implementation("androidx. compose.runtime :runtime:1.6.7") implementation("androidx. compose.ui
:ui:1.6.7") implementation("androidx. compose.foundation :foundation:1.6.7") implementation("androidx. compose.animation :animation:1.6.7") implementation("androidx. compose.material :material:1.6.7") implementation("androidx. compose.material3 :material3:1.2.1") } Link: h tt ps://developer.android.com/jetpack/androidx/releases/compose#versions -
// Compose Multiplatform commonMain.dependencies { implementation("org.jetbrains. compose.runtime :runtime:1.6.11") implementation("org.jetbrains. compose.ui
:ui:1.6.11") implementation("org.jetbrains. compose.foundation :foundation:1.6.11") implementation("org.jetbrains. compose.animation :animation:1.6.11") implementation("org.jetbrains. compose.material :material:1.6.11") implementation("org.jetbrains. compose.material3 :material3:1.6.11") } Link: h tt ps://www.jetbrains.com/help/kotlin-multipla tf orm-dev/compose-compatibility-and-versioning.html -
// Compose Multiplatform plugins { id("org.jetbrains.compose") version "1.6.11" } commonMain.dependencies
{ implementation( compose.runtime ) implementation( compose.ui ) implementation( compose.foundation ) implementation( compose.animation ) implementation( compose.material ) implementation( compose.material3 ) } -
KMP Wizard Section 3 commonMain androidMain iosMain desktopMain Desktop App
Android App iOS App -
@Composable KMP Wizard Section 3 App() ComposeView ComposeUIViewController ComposeWindow Window
ComponentActivity SwiftUI -
// Android class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState:
Bundle?) { super.onCreate(savedInstanceState) setContent { // ComposeView App() } } } // Desktop fun main() = application { Window(...) { // ComposeWindow App() } } -
// iOS fun MainViewController(): UIViewController { return ComposeUIViewController { App()
} } @main struct iOSApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { var body: some View { ComposeView() } } struct ComposeView: UIViewControllerRepresentable { func makeUIViewController(context: Context) -> UIViewController { MainViewControllerKt.MainViewController() } } Link: h tt ps://www.jetbrains.com/help/kotlin-multipla tf orm-dev/compose-swi ft ui-integration.html -
org.jetbrains.compose.components:components-resources Resources Section 3 Link: h tt ps://www.jetbrains.com/help/kotlin-multipla tf orm-dev/compose-images-resources.html
composeResources/ drawable files font value -fr -en-rUS -hdpi -dark -
// Jetpack Compose android { android.buildFeatures.resValues = true // default
} import com.example.kmp.R Image( painterResource(R.drawable.compose_multiplatform), contentDescription = null, ) -
// Compose Multiplatform commonMain.dependencies { implementation(compose.components.resources) } import kotlinproject.composeapp.generated.resources.Res import
kotlinproject.composeapp.generated.resources.compose_multiplatform Image( painterResource(Res.drawable.compose_multiplatform), contentDescription = null, ) -
Lifecycle Section 3 androidx.lifecycle:lifecycle-common ✅ ✅ ✅ ❌ androidx.lifecycle:lifecycle-runtime ✅
✅ ✅ ❌ androidx.lifecycle:lifecycle-runtime-compose ✅ ❌ ✅ ❌ org.jetbrains.androidx.lifecycle:lifecycle-common ✅ ✅ ✅ ✅ org.jetbrains.androidx.lifecycle:lifecycle-runtime ✅ ✅ ✅ ✅ org.jetbrains.androidx.lifecycle:lifecycle-runtime-compose ✅ ✅ ✅ ✅ Android iOS Desktop Web module Link: h tt ps://www.jetbrains.com/help/kotlin-multipla tf orm-dev/compose-lifecycle.html -
Lifecycle Common Section 3 INITIALIZED DESTROYED CREATED STARTED RESUMED INITIALIZED
DESTROYED CREATED STARTED RESUMED States Events States ON_PAUSE ON_RESUME ON_STOP ON_START ON_DESTROY ON_CREATE -
Lifecycle to other platforms Section 3 Desktop iOS ViewControllerBasedLifecycleOwner.uikit.kt ComposeUIViewController.uikit.kt
✅ ComposeContainer.uikit.kt ComposeContainer.desktop.kt ✅ ComposeWindow.desktop.kt ComposeWindowPanel.desktop.kt LocalLifecycleOwner -
ViewModel Section 3 Link: h tt ps://www.jetbrains.com/help/kotlin-multipla tf orm-dev/compose-viewmodel.html androidx.lifecycle:lifecycle-viewmodel
✅ ✅ ✅ ❌ androidx.lifecycle:lifecycle-viewmodel-compose ✅ ❌ ✅ ❌ org.jetbrains.androidx.lifecycle:lifecycle-viewmodel ✅ ✅ ✅ ✅ org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose ✅ ✅ ✅ ✅ Android iOS Desktop Web module -
// Jetpack Compose dependencies { implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.0") } @Composable fun CupcakeApp(
viewModel: OrderViewModel = viewModel(), ) { ... } class OrderViewModel : ViewModel() { val uiState: StateFlow<OrderUiState> = ... } -
// Compose Multiplatform commonMain.dependencies { implementation("org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose:2.8.0") } @Composable fun CupcakeApp(
viewModel: OrderViewModel = viewModel { OrderViewModel() }, ) { ... } class OrderViewModel : ViewModel() { val uiState: StateFlow<OrderUiState> = ... } -
// Compose Multiplatform (Desktop) class OrderViewModel : ViewModel() { fun
doSomething() { viewModelScope.launch { ... } } } // build.gradle.kts commonMain.dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1") } val desktopMain by getting desktopMain.dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.8.1") } // Dispatchers.Main.immediate -
ViewModelStoreOwner Section 3 @Composable ComposeView ComposeUIViewController ComposeWindow ComponentActivity ✅ ✅
✅ Desktop iOS Android LocalViewModelStoreOwner -
@Composable NavHost NavBackStackEntry @Composable ✅ ViewModelStoreOwner Section 3 ComposeView ComposeUIViewController
ComposeWindow ComponentActivity ✅ ✅ ✅ LocalViewModelStoreOwner NavBackStackEntry ✅ @Composable -
Navigation Section 3 Link: h tt ps://www.jetbrains.com/help/kotlin-multipla tf orm-dev/compose-navigation-routing.html androidx.navigation:navigation-*
✅ ❌ ❌ ❌ org.jetbrains.androidx.navigation:navigation-common ✅ ✅ ✅ ✅ org.jetbrains.androidx.navigation:navigation-runtime ✅ ✅ ✅ ✅ org.jetbrains.androidx.navigation:navigation-compose ✅ ✅ ✅ ✅ Android iOS Desktop Web module -
// Jetpack Compose dependencies { implementation("androidx.navigation:navigation-compose:2.7.7") } // Compose Multiplatform
commonMain.dependencies { implementation("org.jetbrains.androidx.navigation:navigation-compose:2.7.0-alpha07") } -
// Jetpack Compose // Compose Multiplatform val navController: NavHostController =
rememberNavController() NavHost(navController, startDestination = "list") { composable("list") { ListScreen(onItemClick = { id -> navController.navigate("detail/$id") }) } composable( "detail/{id}", arguments = listOf(navArgument("id") { type = NavType.LongType }) ) { backStackEntry -> val id = backStackEntry.arguments?.getLong("id")!! DetailScreen(id = id) } } -
JetBrains Internal ۄ࠳۞ܻ Section 3 androidx.annotation:annotation ✅ ✅ ✅ ❌
androidx.collection:collection ✅ ✅ ✅ ❌ org.jetbrains.compose.annotation-internal:annotation ✅ ✅ ✅ ✅ org.jetbrains.compose.collection-internal:collection ✅ ✅ ✅ ✅ Android iOS Desktop Web module -
UI Compose ViewModel Lifecycle Domain Data To share UI Logic!
Collection* DataStore Room Repository UseCase Compose ViewModel Lifecycle Collection* Navigation Section 3 -
Domain Summary Data Jetpack UI Android iOS Desktop But, in
progress... 😅 ⚠ ⚠