Blog Infos
Author
Published
Topics
,
Published

This is the third part of the series.

  • For Part 1, please read here.
  • For Part 2, 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 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 JAR files that we copy-pasted in the libs folder.

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")
}

That’s it, now we can use Compose annotation processor, and the corresponding generated files.

Job Offers

Job Offers


    Senior Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

    Mobile Engineer

    OLX Group
    Remote, Portugal, Spain, Romania, Poland
    • Full Time
    apply now

    Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

,

Breaking the Rules: Dynamic Navigation in Modularized Apps

Properly handled navigation is critical for modularized apps, which often implement navigation logic at runtime. This presents certain challenges when working with the Navigation Components library:
Watch Video

Breaking the Rules: Dynamic Navigation in Modularized Apps

Sumayyah Ahmed
Senior Android Engineer

Breaking the Rules: Dynamic Navigation in Modularized Apps

Sumayyah Ahmed
Senior Android Engin ...

Breaking the Rules: Dynamic Navigation in Modularized Apps

Sumayyah Ahmed
Senior Android Engineer

Jobs

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

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. Required fields are marked *

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

Menu