こちらの記事と同じことを行いました。
AndroidでRoomを使ってデータベース
コードは、次のように取得しました。
git clone https://github.com/cozyk100/AndroidStudioProjectsPublic.git
その中の RoomKotlin というプロジェクトを使いました。
Ksp (Kotlin Symbol Processing API) を使うようにしました。
プロジェクトの作成
プロジェクト名: Room02
環境
build.gradle.kts_Project_Room02
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.android.application") version "8.1.2" apply false
id("org.jetbrains.kotlin.android") version "1.9.0" apply false
id("com.google.devtools.ksp") version "1.9.20-1.0.14" apply false
}
build.gradle.kts_Module_app
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.google.devtools.ksp")
}
(省略)
android {
(省略)
buildFeatures {
viewBinding = true
}
}
dependencies {
val room_version = "2.6.0"
//Room
implementation("androidx.room:room-runtime:$room_version")
ksp("androidx.room:room-compiler:$room_version")
implementation("androidx.room:room-rxjava2:$room_version")
implementation("androidx.room:room-rxjava3:$room_version")
implementation("androidx.room:room-guava:$room_version")
testImplementation("androidx.room:room-testing:$room_version")
implementation("androidx.room:room-paging:$room_version")
// 非同期
implementation("io.reactivex.rxjava2:rxjava:2.2.21")
implementation("io.reactivex.rxjava2:rxkotlin:2.4.0")
implementation ("io.reactivex.rxjava2:rxandroid:2.1.1")
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.10.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
画面
次は、 RoomKotlin のプロジェクトのものをそのままコピーしました。
activity_main.xml
my_test_view.xml
プログラム
RoomKotlin のプロジェクトのものを
com.example.room -> com.example.room02 と書き変えました。
MainActivity.kt
MyAdapter.kt
dao/DeptDao.kt
dao/UserDao.kt
dao/UserViewDao.kt
db/AppDatabase.kt
entity/Dept.kt
entity/User.kt
entity/UserView.kt