KMP介绍
www.jetbrains.com.cn/en-us/help/…
jetbrain 官网自行查看
Samples 里面有 star 比较多的项目
创建项目下载
www.jetbrains.com.cn/en-us/help/…
项目结构 Android+IOS
library
- 多平台库生态
依赖库分析 libs.versions.toml
推荐使用 kotlin 做 DSL
- 最外层 build.gradle.kts 管理插件
- 最外层 settings.gradle.kts 管理库和项目
- gradle/libs.versions.toml 管理所有相关库项目,依赖 值的配置信息,添加一个 module 之后要添加到这个文件夹下面
- composeApp/build.gradle.kts 管理 commonMain 和 AndroidMain 、 iosMain 项目的配置,
- iosApp 单独一个 Xcode 的项目文件
[versions]
# agp
agp = "8.2.0"
android-compileSdk = "34"
android-minSdk = "24"
android-targetSdk = "34"
androidx-activityCompose = "1.9.0"
androidx-appcompat = "1.6.1"
androidx-constraintlayout = "2.1.4"
androidx-core-ktx = "1.13.1"
androidx-espresso-core = "3.5.1"
androidx-material = "1.12.0"
androidx-test-junit = "1.1.5"
compose-plugin = "1.6.10"
junit = "4.13.2"
kotlin = "2.0.0"
material3Android = "1.2.1"
appcompat = "1.7.0"
mvikotlinVersion = "1.0"
settings = "1.1.1"
lifecycle-viewmodel-compose = "2.8.0"
kotlinx-coroutines = "1.8.1"
ktor = "2.3.11"
kotlinx-serialization = "1.6.3"
kmp-nativecoroutines = "1.0.0-ALPHA-31"
koin = "3.6.0-Beta4"
koinCompose = "3.6.0-Beta4"
koinComposeMultiplatform = "1.2.0-Beta4"
lifecycleViewmodelAndroid = "2.8.1"
kotlinVersion = "1.9.0"
[libraries]
#arkivanov-mvikotlin = { module = "com.arkivanov.mvikotlin:mvikotlin", version.ref = "mvikotlinVersion" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "androidx-core-ktx" }
androidx-test-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-junit" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidx-espresso-core" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" }
androidx-material = { group = "com.google.android.material", name = "material", version.ref = "androidx-material" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "androidx-constraintlayout" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
kotlinx-coroutines = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
# 公共库 lifecycle 和 viewmodel
lifecycle-viewmodel-compose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycle-viewmodel-compose" }
# 第三方 kv 库 Android 使用 sharedperfence iOS 使用 UserDefaults
settings = { module = "com.russhwolf:multiplatform-settings", version.ref = "settings" }
# 网络框架 ktor
ktor-client-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" }
ktor-client-json = { group = "io.ktor", name = "ktor-client-json", version.ref = "ktor" }
ktor-client-logging = { group = "io.ktor", name = "ktor-client-logging", version.ref = "ktor" }
ktor-client-serialization = { group = "io.ktor", name = "ktor-client-serialization", version.ref = "ktor" }
ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktor" }
ktor-client-android = { group = "io.ktor", name = "ktor-client-android", version.ref = "ktor" }
ktor-client-darwin = { group = "io.ktor", name = "ktor-client-darwin", version.ref = "ktor" }
ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio", version.ref = "ktor" }
ktor-network-tls = { group = "io.ktor", name = "ktor-network-tls", version.ref = "ktor" }
ktor-client-java = { group = "io.ktor", name = "ktor-client-java", version.ref = "ktor" }
ktor-client-js = { group = "io.ktor", name = "ktor-client-js", version.ref = "ktor" }
ktor-serialization-kotlinx-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktor" }
//DI 框架 koin
koin-android = { module = "io.insert-koin:koin-android", version.ref = "koin" }
koin-androidx-compose = { module = "io.insert-koin:koin-androidx-compose", version.ref = "koinCompose" }
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
koin-test = { module = "io.insert-koin:koin-test", version.ref = "koin" }
koin-test-junit4 = { module = "io.insert-koin:koin-test-junit4", version.ref = "koin" }
#ktor 公共库全套
[bundles]
ktor-common = ["ktor-network-tls","ktor-client-cio", "ktor-client-core", "ktor-client-json", "ktor-client-logging", "ktor-client-serialization", "ktor-client-content-negotiation", "ktor-serialization-kotlinx-json"]
[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
# 使用了 compose multiplatform 版本 1.6.10
jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" }
# compose compiler 和 cmp 版本合并成一个
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
# 序列化
kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
kmpNativeCoroutines = { id = "com.rickclephas.kmp.nativecoroutines", version.ref = "kmp-nativecoroutines" }
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlinVersion" }
打包文件
- composeApp/build/output/ Android 打包文件目录
- composeApp/build/bin/ ios 端目录打包之后 的 framework
- composeApp/build/kotlin-multiplatform-resources ios 作为第三方库需要资源文件
WASM 问题
-
1、ktor 打包报错
-
运行脚本
:composeApp:wasmJsBrowserDevelopmentRun
IOS .framework 的问题
1、IOS 使用库文件时 , 资源文件找不到的问题
Uncaught Kotlin exception: org.jetbrains.compose.resources.MissingResourceException:
Missing resource with path: /private/var/containers/Bundle/Application/C209C38F-4683-4D82-BD0B-B76C7D6CF359/Feedback.app/compose-resources/composeResources/ckck.composeapp.generated.resources/
提供 .framework 当第三方依赖时, 需要把对应的资源文件一并提供,
.framework 在 shared/bin/iosArm64 对应的版本是 M1 芯片 .framework 在 shared/bin/iosX64 对应的版本是 Intel 芯片
processedResources 在 shared/processedResources 目录对应
2、ktor 500 的问题
Desktop
- compose 需要导入不同平台的依赖
implementation(compose.desktop.windows_x64)
- 运行脚本
desktopRun -DmainClass=MainKt --quiet