My first Kotlin Symbol Processing Tool for Android

Background of requirements

1. KSP module config

implementation("com.google.devtools.ksp:symbol-processing-api:1.4.30-1.0.0-alpha02")

2. SymbolProcessor

fun init(
options: Map<String, String>,
kotlinVersion: KotlinVersion,
codeGenerator: CodeGenerator,
logger: KSPLogger
)
fun process(resolver: Resolver): List<KSAnnotated>fun finish()fun onError() {}
  1. in init() : Read Gradle script property. Set codeGenerator to member field
  2. in process() : Collect @JsonClass(generateAdapter = true)annotated class
  3. in finish() : Generate Grouped JsonAdapterFactory

3. Init()

4. process()

5. CustomKSVisitor.kt

class KSVisitor<D, R> {
fun visitFile(file: KSFile, data: D): R
fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: D): R
fun visitFunctionDeclaration(function: KSFunctionDeclaration, data: D): R
// and so on.. argument, property getter/setter
}

6. finish()

val packageName = "com.example"
val outputStream: OutputStream = codeGenerator.createNewFile(
dependencies = Dependencies(
true,
*targets.map { it.containingFile!! }
.toTypedArray()
),
packageName = packageName,
fileName = "GroupJsonAdapterFactory"
)
outputStream.write("""
|package $packageName
|
|import com.squareup.moshi.JsonAdapter
|import com.squareup.moshi.Moshi
|import com.squareup.moshi.Types
|// import target classes & target classes json adapter
|class GroupJsonAdapterFactory: JsonAdapter.Factory {
| override fun create(): JsonAdapter<*> {
| // declare logics
| }
|}
""".trimMargin().toByteArray())
outputStream.close()

7. Resources/META-INF.services

com.example.CustomSymbolProcessor

7. Consumer module

8. Wth??? My IDEA isn’t aware of KSP generated file

Tip

// consumer build.gradle.kts
ksp {
arg("projectName", project.name)
}
// SymbolProcessor.kt
private lateinit var projectName: String
fun init(options: Map<String, String>) {
projectName = options["projectName"]
}
fun finish() { outputStream.write("""
|class ${projectName}JsonAdapterFactory
""")
}

Conclusion

Reference

Update::

--

--

--

Android Developer, Google Developers Experts

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Insolar’s Pulsar Protocol

In the aftermath of the French Revolution and the Reign of

One Easy Trick to Understanding Object Relations in Ruby

Why I took a break from coding(and you need to too!)

How to Reset Alcatel Pixi 3 (5.5)

Hard reset Samsung galaxy

Authoring Azure DevOps Task — Overview (1)

How to get RCE on AEM instance without Java knowledge

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
SeongUg Steve Jung

SeongUg Steve Jung

Android Developer, Google Developers Experts

More from Medium

A scalable Deep Link approach to navigation in Android

ACTIVITY LIFECYCLE IN KOTLIN

Novice Note: HMS Core Integration to Android Studio in Ubuntu 21.10

Android AES Cipher Encryption/Decryption - compatible with Node.js and Angular