Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Get started with Kotlin Multiplatform Mobile
Search
Hiroyuki Kusu
March 05, 2021
Programming
0
480
Get started with Kotlin Multiplatform Mobile
Yumemi.apk #3 (
https://yumemi.connpass.com/event/202135/
) の資料
Hiroyuki Kusu
March 05, 2021
Tweet
Share
More Decks by Hiroyuki Kusu
See All by Hiroyuki Kusu
モノレポのプルリクエストに最近、導入したもの
hkusu
2
420
GitHub composite actions
hkusu
2
290
Android の静的解析における SARIF ファイルの活用
hkusu
0
4.6k
CI_でライブラリのバージョンの変化をレポートする.pdf
hkusu
0
300
Maestro を GitHub Actions で動かす 〜Android編〜
hkusu
0
1.3k
Android の CI(GitHub Actions)の改善で、最近やったこと
hkusu
0
550
Tauri Mobile で生成される Android のコードを見てみる
hkusu
0
1.3k
Custom GitHub Actions を作って Organization 内で共有する
hkusu
1
480
GitHub Actions でユニットテストの結果をレポートする
hkusu
0
3.3k
Other Decks in Programming
See All in Programming
fs2-io を試してたらバグを見つけて直した話
chencmd
0
260
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
130
Запуск 1С:УХ в крупном энтерпрайзе: мечта и реальность ПМа
lamodatech
0
770
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
7
1.6k
為你自己學 Python
eddie
0
360
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
240
Findy Team+ Awardを受賞したかった!ベストプラクティス応募内容をふりかえり、開発生産性向上もふりかえる / Findy Team Plus Award BestPractice and DPE Retrospective 2024
honyanya
0
120
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
190
CloudflareStack でRAGに入門
asahiiwm
0
130
週次リリースを実現するための グローバルアプリ開発
tera_ny
1
110
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
980
React 19でお手軽にCSS-in-JSを自作する
yukukotani
5
450
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
133
9k
Git: the NoSQL Database
bkeepers
PRO
427
64k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
BBQ
matthewcrist
85
9.4k
Adopting Sorbet at Scale
ufuk
74
9.1k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
50k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
190
What's in a price? How to price your products and services
michaelherold
244
12k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Transcript
Get started with Kotlin Multiplatform Mobile 2021.03.05 YUMEMI.apk #3 Hiroyuki
Kusu ( @hkusu_ )
About me
https://blog.jetbrains.com/ja/kotlin/2020/09/kotlin-multiplatform-mobile-goes-alpha-ja/
https://kotlinlang.org/docs/mobile/home.html KMM ͷυΩϡϝϯτ
Android Studio ༻ͷϓϥάΠϯ
৽ن KMM ϓϩδΣΫτ࡞༻ͷςϯϓϨʔτ
KMMϓϩδΣΫτͷܗ͕࡞͞ΕΔ
https://github.com/hkusu/KmmSampleApp ؆୯ͳαϯϓϧΛ࡞ͬͯΈͨ
6TF$BTF 3FQPTJUPSZ "QJ %BUB$MBTT "DUJWJUZ 7JFX.PEFM BOESPJE"QQ JPT"QQ ※ ࠓճݕূͰ͖͍ͯ·ͤΜ
(JU)VC"1* TIBSFE
• ಈ࡞֬ೝͨ͠ϩʔΧϧڥ • Android Studio 4.1.2 • Android Studio Plugin
• Kotlin: 1.4.31-release-Studio4.1-1 • Kotlin Multiplatform Mobile: 0.2.0-release-65-Studio4.1 • ϏϧυπʔϧϥΠϒϥϦͷόʔδϣϯ GitHub ͷίʔυͷํΛΈ͍ͯͩ͘͞
plugins { // ... id("kotlinx-serialization") } kotlin { // ...
sourceSets { val commonMain by getting { dependencies { // ... implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0") implementation("io.ktor:ktor-client-core:1.5.2") implementation("io.ktor:ktor-client-serialization:1.5.2") } } // ... val androidMain by getting { dependencies { // ... implementation("io.ktor:ktor-client-android:1.5.2") } } // ... val iosMain by getting { dependencies { implementation("io.ktor:ktor-client-ios:1.5.2") } } // ... } } build.gradle.kts shared Coroutines + serialization + Ktor ͷಋೖ ※ ਖ਼֬ͳઃఆ GitHub ͷίʔυͷํΛݟ͍ͯͩ͘͞
plugins { // ... id("kotlin-kapt") } kotlin { // ...
sourceSets { // ... val androidMain by getting { dependencies { // ... implementation("com.google.dagger:hilt-android:2.31.2-alpha") // javax.annotation.Generated ͕ݟ͔ͭΒͳ͍Τϥʔ͕ग़ΔͷͰ.. compileOnly("javax.annotation:javax.annotation-api:1.3.2") } } // ... } } // ref: https://www.reddit.com/r/Kotlin/comments/ack2r6/problem_using_kapt_in_a_multiplatform_project/ dependencies { "kapt"("com.google.dagger:hilt-android-compiler:2.31.2-alpha") } build.gradle.kts shared ※ ਖ਼֬ͳઃఆ GitHub ͷίʔυͷํΛݟ͍ͯͩ͘͞ Dagger Hilt ͷಋೖ(Androidͷํʹ͚ͩ)
buildscript { repositories { gradlePluginPortal() jcenter() google() mavenCentral() } dependencies
{ classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30") classpath("com.android.tools.build:gradle:4.1.2") classpath("org.jetbrains.kotlin:kotlin-serialization:1.4.30") classpath("com.google.dagger:hilt-android-gradle-plugin:2.31.2-alpha") } } allprojects { repositories { google() jcenter() mavenCentral() } } ϓϩδΣΫτϧʔτͷ build.gradle.kts
shared/commonMain Ktor ෦Ҏ֎ී௨ͷ Kotlin + Coroutines ͷίʔυͳͷͰ Ktor ෦͚ͩҎ߱Ͱઆ໌ ڞ௨ιʔε෦
internal object Libs { internal val httpClient: HttpClient by lazy
{ HttpClient { install(JsonFeature) { serializer = KotlinxSerializer(json = Json { ignoreUnknownKeys = true }) } } } } shared/commonMain Ktor ͷ HTTP ΫϥΠΞϯτͷੜͱ Kotlin serialization ͷઃఆ Android/iOS Ͱڞ௨ͷઃఆͰOK
internal class GitHubApi(private val httpClient: HttpClient) { suspend fun getUserList():
List<GitHubUserResponse> { return httpClient.get("${BASE_URL}/users") } companion object { private const val BASE_URL = "https://api.github.com" } } shared/commonMain suspending function ʹରԠ APIͷఆٛ
@Module @InstallIn(SingletonComponent::class) internal object Module { @Provides fun provideGitHubApi(): GitHubApi
{ return GitHubApi(Libs.httpClient) } @Provides @Singleton fun provideUserRepository(gitHubApi: GitHubApi): UserRepository { return UserRepositoryImpl(gitHubApi) } @Provides fun provideGetUserUseCase(userRepository: UserRepository): GetUserUseCase { return GetUserUseCase(userRepository) } } shared/androidMain Dagger Hilt ͷϞδϡʔϧ ※ commonMain ʹ Dagger ແ͘ίϯετϥΫλΠϯδΣΫγϣϯͰ͖ͳ͍ͷͰ ͜͜ͰΠϯελϯεੜํ๏Λఆٛ (SharedͷதͷOSผ࣮)
plugins { id("com.android.application") kotlin("android") id("kotlin-kapt") id("dagger.hilt.android.plugin") } dependencies { implementation(project(":shared"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2") implementation("androidx.appcompat:appcompat:1.2.0") implementation("androidx.core:core-ktx:1.3.2") implementation("androidx.activity:activity-ktx:1.2.0") implementation("androidx.fragment:fragment-ktx:1.3.0") implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0") implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.3.0") implementation("androidx.constraintlayout:constraintlayout:2.0.4") implementation("com.google.android.material:material:1.3.0") implementation("com.google.dagger:hilt-android:2.31.2-alpha") kapt("com.google.dagger:hilt-android-compiler:2.31.2-alpha") } android { compileSdkVersion(29) // ... build.gradle.kts androidApp ͓ͳ͡ΈͷϥΠϒϥϦୡ
plugins { id("com.android.application") kotlin("android") id("kotlin-kapt") id("dagger.hilt.android.plugin") } dependencies { implementation(project(":shared"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2") implementation("androidx.appcompat:appcompat:1.2.0") implementation("androidx.core:core-ktx:1.3.2") implementation("androidx.activity:activity-ktx:1.2.0") implementation("androidx.fragment:fragment-ktx:1.3.0") implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0") implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.3.0") implementation("androidx.constraintlayout:constraintlayout:2.0.4") implementation("com.google.android.material:material:1.3.0") implementation("com.google.dagger:hilt-android:2.31.2-alpha") kapt("com.google.dagger:hilt-android-compiler:2.31.2-alpha") } android { compileSdkVersion(29) // ... build.gradle.kts androidApp Dagger Hilt
@HiltViewModel class MainViewModel @Inject constructor(getUser: GetUserUseCase) : ViewModel() { private
val _userList: MutableLiveData<List<User>> = MutableLiveData() val userList = _userList as LiveData<List<User>> init { viewModelScope.launch { _userList.value = getUser() } } } ViewModel androidApp Dagger Hilt
@HiltViewModel class MainViewModel @Inject constructor(getUser: GetUserUseCase) : ViewModel() { private
val _userList: MutableLiveData<List<User>> = MutableLiveData() val userList = _userList as LiveData<List<User>> init { viewModelScope.launch { _userList.value = getUser() } } } ViewModel androidApp shared ʹஔ͍ͨ UseCase Λ inject
• Android ෦ʹؔͯ͠ී௨ʹ࡞Εͦ͏ • Android ͔ΒݟΕී௨ͷϚϧνϞδϡʔϧߏ • ڞ௨ιʔε෦ʹ͍ͭͯ.. • ϚϧνϓϥοτϑΥʔϜରԠͷϥΠϒϥϦΛར༻͢Δඞཁ͕͋Δ
• ͜͜Λ iOS ͚ʹͲ͏ఏڙ͍͔͕ͯ͘͠ϙΠϯτʹͳΓͦ͏ • iOS ͚ʹ DI ͢ΔͳΒ KOIN Kodein Λར༻ͨ͠ํ͕Αͦ͞͏
Thank you ! @hkusu_