0
1

More than 3 years have passed since last update.

ViewModelを使ったAndroidプロジェクトセットアップのチートシート

Last updated at Posted at 2020-10-31

いつもドキュメントを探してググりまくっているので、ここにまとめる。

ViewModel

  • ViewModelを使いたい
  • val viewModel: MyViewmodel by viewModels()を使いたい
dependencies {
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
    implementation 'androidx.activity:activity-ktx:1.2.3'
    implementation 'androidx.fragment:fragment-ktx:1.3.3'
}

Data binding & View binding

どちらか、使いたい方だけ選ぶ。

android {
    buildFeatures {
        viewBinding true
        dataBinding true
    }
}

Data bindingを使う時は、先頭部分にkotlin-kaptの指定も追加する必要があります。

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt' // これを追加
}

Coroutine, Flow

最近はLiveDataよりもCoroutine, Flowを使いたいので、以下も入れるのが良いでしょう。

dependencies {
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0' // Flow.asLiveData()とか
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1