Skip to content

Commit 8394f52

Browse files
authored
kmp-impl: Room basic setup (#2284)
1 parent 18ad115 commit 8394f52

File tree

10 files changed

+217
-10
lines changed

10 files changed

+217
-10
lines changed

Diff for: build-logic/convention/src/main/kotlin/AndroidRoomConventionPlugin.kt

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import androidx.room.gradle.RoomExtension
2+
import com.google.devtools.ksp.gradle.KspExtension
23
import org.gradle.api.Plugin
34
import org.gradle.api.Project
45
import org.gradle.kotlin.dsl.configure
@@ -12,17 +13,21 @@ class AndroidRoomConventionPlugin : Plugin<Project> {
1213
pluginManager.apply("androidx.room")
1314
pluginManager.apply("com.google.devtools.ksp")
1415

16+
extensions.configure<KspExtension> {
17+
arg("room.generateKotlin", "true")
18+
}
1519
extensions.configure<RoomExtension> {
1620
// The schemas directory contains a schema file for each version of the Room database.
1721
// This is required to enable Room auto migrations.
1822
// See https://developer.android.com/reference/kotlin/androidx/room/AutoMigration.
1923
schemaDirectory("$projectDir/schemas")
2024
}
2125

26+
2227
dependencies {
23-
add("implementation", libs.findLibrary("room.runtime").get())
24-
add("implementation", libs.findLibrary("room.ktx").get())
25-
add("ksp", libs.findLibrary("room.compiler").get())
28+
"implementation"(libs.findLibrary("androidx.room.runtime").get())
29+
"implementation"(libs.findLibrary("androidx.room.ktx").get())
30+
"ksp"(libs.findLibrary("androidx.room.compiler").get())
2631
}
2732
}
2833
}

Diff for: build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ plugins {
88
alias(libs.plugins.hilt) apply false
99
alias(libs.plugins.kotlin.serialization) apply false
1010
alias(libs.plugins.androidx.navigation) apply false
11+
alias(libs.plugins.room) apply false
1112
alias(libs.plugins.ksp) apply false
1213
alias(libs.plugins.secrets) apply false
1314
alias(libs.plugins.dependencyGuard) apply false

Diff for: core/database/build.gradle.kts

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
plugins {
1111
alias(libs.plugins.mifos.android.library)
1212
alias(libs.plugins.mifos.android.hilt)
13+
alias(libs.plugins.mifos.android.room)
14+
alias(libs.plugins.kotlin.serialization)
1315
alias(libs.plugins.mifos.android.library.jacoco)
1416
id(libs.plugins.kotlin.parcelize.get().pluginId)
1517
}
@@ -29,6 +31,7 @@ dependencies {
2931

3032
implementation(libs.converter.gson)
3133

34+
implementation(libs.kotlinx.serialization.json)
3235
//rxjava dependencies
3336
implementation(libs.rxandroid)
3437
implementation(libs.rxjava)
@@ -38,6 +41,12 @@ dependencies {
3841
implementation(libs.dbflow)
3942
kapt(libs.github.dbflow.processor)
4043

44+
//room dependencies
45+
// implementation(libs.room.runtime)
46+
// implementation(libs.room.ktx)
47+
// ksp(libs.room.compiler)
48+
// kspTest(libs.room.compiler)
49+
4150
// Hilt dependency
4251
implementation(libs.hilt.android)
4352
kapt(libs.hilt.compiler)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2025 Mifos Initiative
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*
8+
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
9+
*/
10+
package com.mifos.room.dao
11+
12+
import androidx.room.Dao
13+
import androidx.room.Delete
14+
import androidx.room.Insert
15+
import androidx.room.OnConflictStrategy
16+
import androidx.room.Query
17+
import androidx.room.Update
18+
import com.mifos.room.entities.noncore.ColumnValue
19+
20+
@Dao
21+
interface ColumnValueDao {
22+
@Insert(onConflict = OnConflictStrategy.REPLACE)
23+
suspend fun insert(columnValue: ColumnValue)
24+
25+
@Update
26+
suspend fun update(columnValue: ColumnValue)
27+
28+
@Delete
29+
suspend fun delete(columnValue: ColumnValue)
30+
31+
@Query("SELECT * FROM ColumnValue WHERE id = :id")
32+
suspend fun getColumnValue(id: Int): ColumnValue?
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2025 Mifos Initiative
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*
8+
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
9+
*/
10+
package com.mifos.room.db
11+
12+
import androidx.room.Database
13+
import androidx.room.RoomDatabase
14+
import androidx.room.TypeConverters
15+
import com.mifos.room.dao.ColumnValueDao
16+
import com.mifos.room.entities.noncore.ColumnValue
17+
import com.mifos.room.utils.typeconverters.ListTypeConverters
18+
19+
@Database(
20+
// [TODO -> add other entities ]
21+
entities = [ColumnValue::class],
22+
version = MifosDatabase.VERSION,
23+
exportSchema = true,
24+
autoMigrations = [],
25+
)
26+
@TypeConverters(ListTypeConverters::class)
27+
// ( TODO -> add type converters here )
28+
29+
abstract class MifosDatabase : RoomDatabase() {
30+
abstract fun columnValueDao(): ColumnValueDao
31+
32+
companion object {
33+
const val VERSION = 1
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2025 Mifos Initiative
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*
8+
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
9+
*/
10+
package com.mifos.room.di
11+
12+
import com.mifos.room.dao.ColumnValueDao
13+
import com.mifos.room.db.MifosDatabase
14+
import dagger.Module
15+
import dagger.Provides
16+
import dagger.hilt.InstallIn
17+
import dagger.hilt.components.SingletonComponent
18+
19+
@Module
20+
@InstallIn(SingletonComponent::class)
21+
object DaoModule {
22+
@Provides
23+
fun providesColumnValueDao(database: MifosDatabase): ColumnValueDao {
24+
return database.columnValueDao()
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2025 Mifos Initiative
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*
8+
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
9+
*/
10+
package com.mifos.room.di
11+
12+
import android.content.Context
13+
import androidx.room.Room
14+
import com.mifos.room.db.MifosDatabase
15+
import dagger.Module
16+
import dagger.Provides
17+
import dagger.hilt.InstallIn
18+
import dagger.hilt.android.qualifiers.ApplicationContext
19+
import dagger.hilt.components.SingletonComponent
20+
import javax.inject.Singleton
21+
22+
@Module
23+
@InstallIn(SingletonComponent::class)
24+
object DatabaseModule {
25+
26+
@Provides
27+
@Singleton
28+
fun providesDatabase(@ApplicationContext context: Context): MifosDatabase {
29+
return Room.databaseBuilder(
30+
context = context,
31+
klass = MifosDatabase::class.java,
32+
name = "mifos-database",
33+
).build()
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2025 Mifos Initiative
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*
8+
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
9+
*/
10+
package com.mifos.room.entities.noncore
11+
12+
import android.os.Parcelable
13+
import androidx.room.ColumnInfo
14+
import androidx.room.Entity
15+
import androidx.room.PrimaryKey
16+
import kotlinx.parcelize.Parcelize
17+
18+
@Parcelize
19+
@Entity(tableName = "ColumnValue")
20+
data class ColumnValue(
21+
@PrimaryKey
22+
@ColumnInfo(name = "id")
23+
var id: Int? = null,
24+
25+
@ColumnInfo(name = "value")
26+
var value: String? = null,
27+
28+
@ColumnInfo(name = "score")
29+
var score: Int? = null,
30+
31+
@ColumnInfo(name = "registeredTableName")
32+
var registeredTableName: String? = null,
33+
) : Parcelable
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2025 Mifos Initiative
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*
8+
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
9+
*/
10+
package com.mifos.room.utils.typeconverters
11+
12+
import androidx.room.TypeConverter
13+
import kotlinx.serialization.encodeToString
14+
import kotlinx.serialization.json.Json
15+
16+
class ListTypeConverters {
17+
18+
@TypeConverter
19+
fun fromIntList(value: String): ArrayList<Int?> {
20+
return Json.decodeFromString(value)
21+
}
22+
23+
@TypeConverter
24+
fun toIntList(list: ArrayList<Int?>): String {
25+
return Json.encodeToString(list)
26+
}
27+
}

Diff for: gradle/libs.versions.toml

+10-7
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ playScanner = "16.1.0"
102102
playService = "18.5.0"
103103
realmVersion = "1.13.0"
104104
recyclerview = "1.3.2"
105-
room = "2.6.1"
105+
room = "2.7.0-alpha12"
106+
roomKtxVersion = "2.7.0-alpha12"
107+
roomRuntimeVersion = "2.7.0-alpha12"
108+
roomTestingVersion = "2.7.0-alpha12"
106109
retrofit = "2.11.0"
107110
rxandroidVersion = "1.1.0"
108111
rxjava = "1.3.8"
@@ -271,7 +274,13 @@ androidx-navigation-ui-ktx = { module = "androidx.navigation:navigation-ui-ktx",
271274
androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "androidxNavigation" }
272275
androidx-navigation-testing = { group = "androidx.navigation", name = "navigation-testing", version.ref = "androidxNavigation" }
273276

277+
#room
278+
androidx-room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }
279+
androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "roomKtxVersion" }
280+
androidx-room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "roomRuntimeVersion" }
281+
androidx-room-testing = { group = "androidx.room", name = "room-testing", version.ref = "roomTestingVersion" }
274282
androidx-core-testing = { module = "androidx.arch.core:core-testing", version.ref = "coreTesting" }
283+
room-paging = { group = "androidx.room", name = "room-paging", version.ref = "room" }
275284

276285
androidx-profileinstaller = { group = "androidx.profileinstaller", name = "profileinstaller", version.ref = "androidxProfileinstaller" }
277286
androidx-test-core = { group = "androidx.test", name = "core", version.ref = "androidxTestCore" }
@@ -387,12 +396,6 @@ play-services-places = { module = "com.google.android.gms:play-services-places",
387396
protobuf-kotlin-lite = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protobuf" }
388397
protobuf-protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protobuf" }
389398

390-
# Room Database
391-
room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }
392-
room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" }
393-
room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" }
394-
room-paging = { group = "androidx.room", name = "room-paging", version.ref = "room" }
395-
396399
# Realm
397400
realm-library-base = { group = "io.realm.kotlin", name = "library-base", version.ref = "realmVersion" }
398401

0 commit comments

Comments
 (0)