Skip to content

Commit e997459

Browse files
authored
Merge pull request #562 from SimonMarquis/room/convention-plugin
Migrate custom room configuration into a convention plugin
2 parents cb7dece + 3409f93 commit e997459

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

build-logic/convention/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ java {
2828
dependencies {
2929
compileOnly(libs.android.gradlePlugin)
3030
compileOnly(libs.kotlin.gradlePlugin)
31+
compileOnly(libs.ksp.gradlePlugin)
3132
}
3233

3334
gradlePlugin {
@@ -68,6 +69,10 @@ gradlePlugin {
6869
id = "nowinandroid.android.hilt"
6970
implementationClass = "AndroidHiltConventionPlugin"
7071
}
72+
register("androidRoom") {
73+
id = "nowinandroid.android.room"
74+
implementationClass = "AndroidRoomConventionPlugin"
75+
}
7176
register("firebase-perf") {
7277
id = "nowinandroid.firebase-perf"
7378
implementationClass = "FirebasePerfConventionPlugin"
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2022 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import com.google.devtools.ksp.gradle.KspExtension
18+
import org.gradle.api.Plugin
19+
import org.gradle.api.Project
20+
import org.gradle.api.artifacts.VersionCatalogsExtension
21+
import org.gradle.api.tasks.InputDirectory
22+
import org.gradle.api.tasks.PathSensitive
23+
import org.gradle.api.tasks.PathSensitivity
24+
import org.gradle.kotlin.dsl.configure
25+
import org.gradle.kotlin.dsl.dependencies
26+
import org.gradle.kotlin.dsl.getByType
27+
import org.gradle.process.CommandLineArgumentProvider
28+
import java.io.File
29+
30+
class AndroidRoomConventionPlugin : Plugin<Project> {
31+
32+
override fun apply(target: Project) {
33+
with(target) {
34+
pluginManager.apply("com.google.devtools.ksp")
35+
36+
extensions.configure<KspExtension> {
37+
// The schemas directory contains a schema file for each version of the Room database.
38+
// This is required to enable Room auto migrations.
39+
// See https://developer.android.com/reference/kotlin/androidx/room/AutoMigration.
40+
arg(RoomSchemaArgProvider(File(projectDir, "schemas")))
41+
}
42+
43+
val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")
44+
dependencies {
45+
add("implementation", libs.findLibrary("room.runtime").get())
46+
add("implementation", libs.findLibrary("room.ktx").get())
47+
add("ksp", libs.findLibrary("room.compiler").get())
48+
}
49+
}
50+
}
51+
52+
/**
53+
* https://issuetracker.google.com/issues/132245929
54+
* [Export schemas](https://developer.android.com/training/data-storage/room/migrating-db-versions#export-schemas)
55+
*/
56+
class RoomSchemaArgProvider(
57+
@get:InputDirectory
58+
@get:PathSensitive(PathSensitivity.RELATIVE)
59+
val schemaDir: File,
60+
) : CommandLineArgumentProvider {
61+
override fun asArguments() = listOf("room.schemaLocation=${schemaDir.path}")
62+
}
63+
}

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ plugins {
3030
alias(libs.plugins.kotlin.jvm) apply false
3131
alias(libs.plugins.kotlin.serialization) apply false
3232
alias(libs.plugins.hilt) apply false
33+
alias(libs.plugins.ksp) apply false
3334
alias(libs.plugins.secrets) apply false
3435
}

core/database/build.gradle.kts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,11 @@ plugins {
2222
id("nowinandroid.android.library")
2323
id("nowinandroid.android.library.jacoco")
2424
id("nowinandroid.android.hilt")
25-
alias(libs.plugins.ksp)
25+
id("nowinandroid.android.room")
2626
}
2727

2828
android {
2929
defaultConfig {
30-
// The schemas directory contains a schema file for each version of the Room database.
31-
// This is required to enable Room auto migrations.
32-
// See https://developer.android.com/reference/kotlin/androidx/room/AutoMigration.
33-
ksp {
34-
arg("room.schemaLocation", "$projectDir/schemas")
35-
}
36-
3730
testInstrumentationRunner =
3831
"com.google.samples.apps.nowinandroid.core.testing.NiaTestRunner"
3932
}
@@ -57,10 +50,6 @@ android {
5750
dependencies {
5851
implementation(project(":core:model"))
5952

60-
implementation(libs.room.runtime)
61-
implementation(libs.room.ktx)
62-
ksp(libs.room.compiler)
63-
6453
implementation(libs.kotlinx.coroutines.android)
6554
implementation(libs.kotlinx.datetime)
6655

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine
121121
# Dependencies of the included build-logic
122122
android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" }
123123
kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" }
124+
ksp-gradlePlugin = { group = "com.google.devtools.ksp", name = "com.google.devtools.ksp.gradle.plugin", version.ref = "ksp" }
124125

125126
[plugins]
126127
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }

0 commit comments

Comments
 (0)