Skip to content

Commit 4d1d730

Browse files
committed
Add KSP plugin support for kotlin_library targets
Extend KSP support to pure Kotlin (JVM) library modules by wiring up the plugin extraction in KotlinProjectDataExtractor and passing plugins through to the generated kotlin_library targets. Changes: - Add plugins field to KotlinProjectData - Extract KSP plugins via collectKspPluginDeps() in extractor - Pass plugins to KotlinLibraryTarget in builder Also add KSP/Moshi testing to sample modules: - sample-kotlin-library: Add KotlinConfig data class with @JsonClass - sample-android-library: Add @JsonClass to User (alongside @parcelize)
1 parent bbcc423 commit 4d1d730

File tree

11 files changed

+88
-15
lines changed

11 files changed

+88
-15
lines changed

grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/kotlin/KotlinProjectData.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ data class KotlinProjectData(
2525
val res: List<String>,
2626
val deps: List<BazelDependency>,
2727
val tags: List<String>,
28-
val lintConfigData: LintConfigData
28+
val lintConfigData: LintConfigData,
29+
val plugins: List<BazelDependency> = emptyList()
2930
)

grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/kotlin/KotlinProjectDataExtractor.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,16 @@ internal class DefaultKotlinProjectDataExtractor
9292
calculateDirectDependencyTags(self = name, deps = deps + transitiveMavenDeps)
9393
} else emptyList()
9494

95+
val plugins = dependenciesDataSource.collectKspPluginDeps(project, variantKey)
96+
9597
return KotlinProjectData(
9698
name = name,
9799
srcs = srcs,
98100
res = resources,
99101
deps = deps.replaceAutoService(),
100102
tags = tags,
101103
lintConfigData = lintConfigs(project),
104+
plugins = plugins
102105
)
103106
}
104107

grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/target/KotlinLibraryTargetBuilder.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ constructor(
7676
res = res,
7777
deps = deps,
7878
tags = tags,
79-
lintConfigData = lintConfigData
79+
lintConfigData = lintConfigData,
80+
plugins = plugins
8081
)
8182
}
8283

keystore/BUILD.bazel

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
filegroup(
2-
name = "debug-keystore",
3-
srcs = [
4-
"debug.keystore",
5-
],
6-
visibility = [
7-
"//visibility:public",
8-
]
2+
name = "debug-keystore",
3+
srcs = [
4+
"debug.keystore",
5+
],
6+
visibility = [
7+
"//visibility:public",
8+
],
99
)
10-

sample-android-library/BUILD.bazel

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ android_library(
1313
"config": "//:lint.xml",
1414
},
1515
manifest = "src/debug/AndroidManifest.xml",
16+
plugins = [
17+
"//:moshi-kotlin-codegen-ksp",
18+
],
1619
resource_sets = {
1720
"debug": {
1821
"manifest": "src/debug/AndroidManifest.xml",
@@ -26,6 +29,7 @@ android_library(
2629
],
2730
deps = [
2831
"//:parcelize",
32+
"@maven//:com_squareup_moshi_moshi",
2933
],
3034
)
3135

@@ -42,6 +46,9 @@ android_library(
4246
"config": "//:lint.xml",
4347
},
4448
manifest = "src/debug/AndroidManifest.xml",
49+
plugins = [
50+
"//:moshi-kotlin-codegen-ksp",
51+
],
4552
resource_sets = {
4653
"debug": {
4754
"manifest": "src/debug/AndroidManifest.xml",
@@ -55,6 +62,7 @@ android_library(
5562
],
5663
deps = [
5764
"//:parcelize",
65+
"@maven//:com_squareup_moshi_moshi",
5866
],
5967
)
6068

@@ -71,6 +79,9 @@ android_library(
7179
"config": "//:lint.xml",
7280
},
7381
manifest = "src/debug/AndroidManifest.xml",
82+
plugins = [
83+
"//:moshi-kotlin-codegen-ksp",
84+
],
7485
resource_sets = {
7586
"debug": {
7687
"manifest": "src/debug/AndroidManifest.xml",
@@ -84,6 +95,7 @@ android_library(
8495
],
8596
deps = [
8697
"//:parcelize",
98+
"@maven//:com_squareup_moshi_moshi",
8799
],
88100
)
89101

@@ -100,6 +112,9 @@ android_library(
100112
"config": "//:lint.xml",
101113
},
102114
manifest = "src/debug/AndroidManifest.xml",
115+
plugins = [
116+
"//:moshi-kotlin-codegen-ksp",
117+
],
103118
resource_sets = {
104119
"debug": {
105120
"manifest": "src/debug/AndroidManifest.xml",
@@ -113,6 +128,7 @@ android_library(
113128
],
114129
deps = [
115130
"//:parcelize",
131+
"@maven//:com_squareup_moshi_moshi",
116132
],
117133
)
118134

@@ -135,6 +151,7 @@ android_unit_test(
135151
deps = [
136152
"//:parcelize",
137153
"//sample-android-library:sample-android-library-demo-free-debug",
154+
"@maven//:com_squareup_moshi_moshi",
138155
"@maven//:junit_junit",
139156
],
140157
)
@@ -158,6 +175,7 @@ android_unit_test(
158175
deps = [
159176
"//:parcelize",
160177
"//sample-android-library:sample-android-library-demo-paid-debug",
178+
"@maven//:com_squareup_moshi_moshi",
161179
"@maven//:junit_junit",
162180
],
163181
)
@@ -181,6 +199,7 @@ android_unit_test(
181199
deps = [
182200
"//:parcelize",
183201
"//sample-android-library:sample-android-library-full-free-debug",
202+
"@maven//:com_squareup_moshi_moshi",
184203
"@maven//:junit_junit",
185204
],
186205
)
@@ -204,6 +223,7 @@ android_unit_test(
204223
deps = [
205224
"//:parcelize",
206225
"//sample-android-library:sample-android-library-full-paid-debug",
226+
"@maven//:com_squareup_moshi_moshi",
207227
"@maven//:junit_junit",
208228
],
209229
)

sample-android-library/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ plugins {
1818
alias(libs.plugins.android.library)
1919
alias(libs.plugins.kotlin.android)
2020
alias(libs.plugins.kotlin.parcelize)
21+
alias(libs.plugins.ksp)
2122
}
2223

2324
android {
@@ -29,5 +30,7 @@ android {
2930
}
3031

3132
dependencies {
33+
implementation libs.moshi
34+
ksp libs.moshi.codegen
3235
testImplementation libs.junit
3336
}

sample-android-library/src/main/java/com/grab/grazel/android/sample/SampleViewModel.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,27 @@ package com.grab.grazel.android.sample
1818

1919
import android.os.Parcelable
2020
import android.util.Log
21+
import com.squareup.moshi.JsonClass
22+
import com.squareup.moshi.Moshi
2123
import kotlinx.parcelize.Parcelize
2224

25+
/**
26+
* Data class to test both Parcelize and KSP code generation with Moshi.
27+
*/
2328
@Parcelize
24-
class User(val firstName: String, val lastName: String) : Parcelable
29+
@JsonClass(generateAdapter = true)
30+
data class User(val firstName: String, val lastName: String) : Parcelable
2531

2632
class SampleViewModel {
2733
val user = User(firstName = "tom", lastName = "hanks")
34+
35+
/**
36+
* Verify KSP code generation by using the generated Moshi adapter.
37+
* This will fail to compile if KSP doesn't generate UserJsonAdapter.
38+
*/
39+
fun verifyKspCodeGeneration(): String {
40+
val moshi = Moshi.Builder().build()
41+
val adapter = moshi.adapter(User::class.java)
42+
return adapter.toJson(user)
43+
}
2844
}

sample-android/src/main/java/com/grab/grazel/android/sample/MainActivity.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,5 @@ class MainActivity : ComponentActivity() {
9393
val moshi = Moshi.Builder().build()
9494
val adapter = moshi.adapter(User::class.java)
9595
val json = adapter.toJson(User("Test", 25))
96-
Log.d("KSP", "Moshi KSP generated: $json")
9796
}
98-
}
97+
}

sample-kotlin-library/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ kotlin_library(
1010
"baseline": "lint-baseline-kotlin.xml",
1111
"lint_checks": ["@lint_maven//:com_slack_lint_slack_lint_checks"],
1212
},
13+
plugins = [
14+
"//:moshi-kotlin-codegen-ksp",
15+
],
1316
visibility = [
1417
"//visibility:public",
1518
],
19+
deps = [
20+
"@maven//:com_squareup_moshi_moshi",
21+
],
1622
)
1723

1824
kotlin_test(
@@ -28,6 +34,7 @@ kotlin_test(
2834
"//visibility:public",
2935
],
3036
deps = [
37+
"@maven//:com_squareup_moshi_moshi",
3138
"@maven//:junit_junit",
3239
],
3340
)

sample-kotlin-library/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
plugins {
1717
id("kotlin")
1818
id("com.android.lint")
19+
alias(libs.plugins.ksp)
1920
}
2021

2122
lint {
@@ -24,6 +25,8 @@ lint {
2425

2526
dependencies {
2627
implementation fileTree(dir: "libs", include: ["*.jar"])
28+
implementation libs.moshi
29+
ksp libs.moshi.codegen
2730
lintChecks libs.slack.lint.checks
2831
testImplementation libs.junit
2932
}

0 commit comments

Comments
 (0)