DEV Community

Cover image for KSP Gradle setup & Processor's first log: Part 2
Aniket Bhoite
Aniket Bhoite

Posted on • Updated on

KSP Gradle setup & Processor's first log: Part 2

In first part we covered what KSP is and what problem we want solve using KSP. This part will cover setting up KSP in Android project with Groovy Gradle or new Kotlin DSL Gradle o Running KSP processor with first log

Checkout other parts in this series:

  1. Android KSP guide for dummies by a Dummy: Part 1 (link)
  2. KSP Gradle setup & Processor's first log: Part 2
  3. Generate the code using KSP : Part 3 (link)
  4. Using KSP output in app: Part 4 (link)

Steps

1) Adding JVM plugin in Project

In project build gradle

Note: Different Kotlin DSL syntax is added in code block wherever needed

//project/build.gradle

...
plugins {
    ...
    id "org.jetbrains.kotlin.jvm" version "1.6.10" apply false
}


//In kts.
/*
plugins {
    ...
    kotlin("jvm") version "1.6.10" apply false
}
*/

...
Enter fullscreen mode Exit fullscreen mode

If project doesn’t have gradlePluginPortal() in plugin repositories then add it as below:

repositories {
    gradlePluginPortal()
    ...
}
Enter fullscreen mode Exit fullscreen mode

2) Creating Processor Module

Create New module as java library named processor to host symbol processor. Apply java-library, org.jetbrains.kotlin.jvm & kotlin plugins in gradle with KSP's symbol processing api dependency. In processor's build.gradle

//processor/build.gradle

plugins {
    id 'java-library'
    id 'org.jetbrains.kotlin.jvm'
    id 'kotlin'
}

//in Kts
/*
plugins {
    id("java-library")
    id("org.jetbrains.kotlin.jvm")
    id("kotlin")
}
*/

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

dependencies {
    implementation(project(":annotations"))
    implementation("com.google.devtools.ksp:symbol-processing-api:1.6.10-1.0.2")
}
Enter fullscreen mode Exit fullscreen mode

3) Creating Annotations Module

Create New module as java library named annotations to define our custom annotations.

annotations module's build.gradle

//annotations/build.gradle

plugins {
    id 'java-library'
    id 'org.jetbrains.kotlin.jvm'
    id 'kotlin'
}

//in KTS
/*
plugins {
    id("java-library")
    id("org.jetbrains.kotlin.jvm")
    id("kotlin")
}
*/

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10")
}
Enter fullscreen mode Exit fullscreen mode

4) Creating Processor Classes

Create MyEventProcessor class

package com.aniket.myevent.processor

...

class MyEventProcessor(
    private val codeGenerator: CodeGenerator,
    private val logger: KSPLogger,
    private val options: Map<String, String>
): SymbolProcessor {
    override fun process(resolver: Resolver): List<KSAnnotated> {

        logger.warn("Async Task API was very good API")
        return emptyList()
    }
}
Enter fullscreen mode Exit fullscreen mode

Create MyEventProcessorProvider class

package com.aniket.myevent.processor

...

class MyEventProcessorProvider: SymbolProcessorProvider {
    override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
        return MyEventProcessor(
            environment.codeGenerator,
            environment.logger,
            environment.options
        )
    }
}

Enter fullscreen mode Exit fullscreen mode

Register your processor in processor module
processor/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider

as follows

com.aniket.myevent.processor.MyEventProcessorProvider
Enter fullscreen mode Exit fullscreen mode

Image description


5) Applying KSP modules to app module

Lets use annotation & processor module for KSP

Add KSP plugin in the Project
project's build.gradle

//project/build.gradle

buildscript {
    dependencies {
        ...
        classpath("com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:1.6.10-1.0.2")
    }
}
Enter fullscreen mode Exit fullscreen mode

Apply KSP plugin, annotation module and processor module as KSP in app module

//app/build.gradle


plugins {
    ...
    id 'com.google.devtools.ksp'
}

//in KTS
/*
plugins {
    ...
    id("com.google.devtools.ksp")
}
*/

dependencies {
    ...
    implementation(project(":annotations"))
    ksp(project(":processor"))
}

Enter fullscreen mode Exit fullscreen mode

Note: For now we are not writing any code in Annotation module, we will
do that in future parts

If you want to run KSP in any other modules then same steps apply to that modules too

Let's add Event Interface early so we can use that in future parts. You can add this interface in any package but make sure you use same the package name for Event interface in the code generation in future parts.

package com.aniket.myevent

import android.os.Bundle

interface Event {
    fun getHashMapOfParamsForCustomAnalytics(): HashMap<*, *>?

    fun getBundleOfParamsForFirebase(): Bundle
}
Enter fullscreen mode Exit fullscreen mode

Now Run app or build project and we can see our KSP log in build output

Build Output log window

Hooray!!!! You just added & ran your first KSP processor. Good job. Take a deep breath and go to sleep, KSP is not that important. GN

In Next part, We will understand how KSP processor works and how to generate Code. Generate the code using KSP : Part 3

Links
GitHub repo part-2 branch: https://github.com/aniketbhoite/ksp-my-event/tree/part-2

Top comments (1)

Collapse
 
laurentlr profile image
laurent russier

Nice introduction, a few suggestions:

  • The Event interface is confusing in this part, since it's not used yet.
  • On the screen shot, the META-INF/services looks like META-INF.services, it could be updated.