Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin Multiplatform でのリソース共有について

Kotlin Multiplatform でのリソース共有について

Ryosuke Shimizu (RIO)

July 05, 2024

Transcript

17 slides
  1. Android iOS バックエンド 仕事 自己紹介 清水 亮介 X: @ LINEヤフー株式会社 ヤフーショッピングの

    Androidアプリ開発 + 副業でAndroid / iOSアプリ開発 会社 ゲーム 料理 ギター 自転車 ディズニー 趣味 rioX432
  2. 画像ファイル  Android:PNG, JPG, WEBP→Non Scalable、SVG→Scalable  iOS:PNG, JPG, HEIF/HEIC, GIF→Non Scalable、PDF→Scalable

    文字列  Android:strings.xml  iOS:Localizable.strings 他にも、色・フォントなどのリソースも。
  3. libs.versions.toml [versions] agp = "8.2.0" kotlin = "2.0.0" compose-plugin =

    "1.6.11" [plugins] androidApplication = { id = "com.android.application", version.ref = "agp" } androidLibrary = { id = "com.android.library", version.ref = "agp" } jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" } compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
  4. build.gradle.kts (composeApp) sourceSets { androidMain.dependencies { implementation(compose.preview) implementation(libs.androidx.activity.compose) } commonMain.dependencies

    { implementation(compose.runtime) implementation(compose.foundation) implementation(compose.material) implementation(compose.ui) implementation(compose.components.resources) implementation(compose.components.uiToolingPreview) } }
  5. 画像 Image(  painterResource(   Res.drawable.compose_multiplatform  ),  null ) 文字列 Text(stringResource(Res.string.click_me)) 色

    colors.xmlが使えない。colorResource(Res.color.orange500) Color.ktで定義。 internal val Orange500 = Color(0xFFF9AA33) internal val Orange700 = Color(0xFFC78522) フォント Typography.ktで定義。 @Composable fun getFontFamily(): FontFamily { val rubikFamily = FontFamily( listOf( Font(Res.font.rubik_light), Font(Res.font.rubik_medium), Font(Res.font.rubik_regular), Font(Res.font.rubik_bold) ) ) return rubikFamily }
  6. Res.~でリソースを参照しようとすると、 @Previewが使用できない。 commonMain.dependencies { : implementation(compose.ui) implementation(compose.uiTooling) <-- Jetpack ComposeでのPreview implementation(compose.components.resources)

    implementation(compose.components.uiToolingPreview) <-- KMPでのPreview                                  (いつものようにPreviewで表示確認不可) } No matching variant of org.jetbrains.compose.ui:ui-tooling:1.6.11 was found. The consumer was configured to find a library for use during 'kotlin-metadata', preferably optimized for non-jvm, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'ios_arm64', attribute 'org.jetbrains.kotlin.platform.type' with value 'native' but:- Variant 'debugApiElements-published' declares a library for use during compile-time:

More Decks by Ryosuke Shimizu (RIO)

Other Decks in Programming