Blog Infos
Author
Published
Topics
,
Published

This is the third part of the series.

  • For Part 1, please read here.

 

This article serves as a guide on how to set up the project to include the Compose annotation processor as a library. I have exported the necessary JAR files in the GitHub repo. 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.

You have to download these jar files and place them in the libs folder of your project. The project structure will look as follows-

Project
  app
    libs
      annotation.aar
      compose-annotation-processor.jar
    src
      main
        java
        res
      build.gradle

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 dependencyblock.

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 JAR files that we copy-pasted in the libsfolder.

implementation(files('libs/annotation-release.aar'))
ksp(files('libs/compose-annotation-processor.jar'))

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).

kotlin.sourceSets.main {
    kotlin.srcDirs(
        file("$buildDir/generated/ksp/"),
    )
}
ksp {
    arg("ignoreGenericArgs", "false")
}

Job Offers

Job Offers


    Mobile Developer Android (m/w/d) Firebase

    REWE digital
    Köln / Cologne
    • Full Time
    apply now

    Android Engineer

    American Express
    Phoenix, USA
    • Full Time
    apply now

    API Engineer

    American Express
    London
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

Jobs

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:

class UserPageDestination {
data class UserPageArgs (
val id: kotlin.Int,
val names: kotlin.collections.ArrayList<kotlin.String>,
)
companion object {
fun parseArguments(backStackEntry: NavBackStackEntry): UserPageArgs {
return UserPageArgs(
id = backStackEntry.arguments?.getInt("id") ?: 0,
names = backStackEntry.arguments?.getSerializable("names") as? kotlin.collections.ArrayList<kotlin.String> ?: throw NullPointerException("parcel value not found"),
)
}
val argumentList: MutableList<NamedNavArgument>
get() = mutableListOf(
navArgument("id") {
type = NavType.IntType
},
navArgument("names") {
type = UserPage_NamesNavType
},
)
fun getDestination(id: kotlin.Int, names: kotlin.collections.ArrayList<kotlin.String>, ): String {
return "UserPage?" +
"id=$id," +
"names=${Uri.encode(gson.toJson(names))}" +
""
}
val route = "UserPage?id={id},names={names}"
}
}
view raw demo.kt hosted with ❤ by GitHub

Thanks.

Thanks to Andy Dyer.

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
I recently found a bug that would cause a crash in all the apps…
READ MORE
blog

Edge-to-edge support for your Android app with Insetter

READ MORE
blog
Jetpack Compose is here to stay. There are tons of incentives and benefits in…
READ MORE
blog
In this blog post, we will learn how to implement navigation in Wear OS.…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu