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


    Sr. Software Development Engineer, Last Mile Driver Assistance Technology

    Amazon
    Berlin
    • Full Time
    apply now

    Kotlin Multiplatform Mobile Developer

    Touchlab
    Remote
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

, ,

Painless, Typesafe Jetpack Compose Navigation with Voyager

Jetpack Compose Navigation by Google has so many drawbacks like no typesafety, specifying the whole NavGraph at startup and fuzzing around with ids. It could all be so simple: Why not just define screens by…
Watch Video

Painless, Typesafe Jetpack Compose Navigation with Voyager

Alexander Steenbergen
Android Dev Lead
IBM

Painless, Typesafe Jetpack Compose Navigation with Voyager

Alexander Steenber ...
Android Dev Lead
IBM

Painless, Typesafe Jetpack Compose Navigation with Voyager

Alexander Steenb ...
Android Dev Lead
IBM

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
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog

How to animate BottomSheet content using Jetpack Compose

Early this year I started a new pet project for listening to random radio…
READ MORE
blog
I recently found a bug that would cause a crash in all the apps…
READ MORE

Leave a Reply

Your email address will not be published.

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

Menu