Safe Compose arguments: An improved way to navigate in Jetpack Compose — Part 3
This is the third part of the series.

This article serves as a guide on how to set up the project to include the Compose annotation processor as a library. This article is written for kotlin build scripts (.gradle.kts). You can check the migration guide to find a groovy alternative for any of the apis used in this article. Before doing any step, make sure that your project is set up with Compose. If creating a new project, this can be done by following
File -> New -> New Project -> Empty Compose Activity
in Android Studio. Or you can follow the guide to include Compose in your existing project.
Now we have to include the official ksp
library for processing the respective annotations. In the app/build.gradle
, include the KSP plugin as follows-
plugins {
id 'com.android.application'
id 'kotlin-android'
id("com.google.devtools.ksp") version "1.5.30-1.0.0"
}
I am using Kotlin version 1.5.30
, hence the KSP plugin version will be 1.5.30-1.0.0
. Please make sure that your Kotlin version matches the KSP version before the hyphen. The latest version can be found here. On similar lines, please make sure that your Kotlin version is the same everywhere. Below are some examples where the Kotlin version is used.
app/build.gradle
composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerVersion '1.5.30'
}build.gradle (project level)
ext {
compose_version = '1.0.3'
}classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
Compose requires version 1.0.3
for compatibility with Kotlin version 1.5.30
. Next include the latest version of Gson
and androidx.navigation
. The latest versions can be found at the previous links . Also include the KSP dependency. This has to go in app/build.gradle
inside the dependency
block.
implementation("com.google.code.gson:gson:2.8.8")
implementation("androidx.navigation:navigation-compose:2.4.0-beta02")
implementation("com.google.devtools.ksp:symbol-processing-api:1.5.30-1.0.0")
Next, we have to include the dependencies to generate the actual helper methods. The latest version can be found on the GitHub repo.
implementation "io.github.dilrajsingh1997:compose-annotation:1.0.1"
ksp("io.github.dilrajsingh1997:compose-annotation-processor:1.0.3")
To make our generated file available to the app
module, we have to instruct the IDE to treat some files under build
folder visible. The following snippet needs to be written in app/build.gradle
in the root of the file (not inside any block).
androidComponents.onVariants { variant ->
kotlin.sourceSets.findByName(variant.name)?.kotlin?.srcDirs(
file("$buildDir/generated/ksp/${variant.name}/kotlin")
)
}
ksp {
arg("ignoreGenericArgs", "false")
}
That’s it, now we can use Compose annotation processor, and the corresponding generated files.
@ComposeDestination
abstract class UserPage {
abstract val id: Int
abstract val names: ArrayList<String>
}
And use the following:
Thanks.