How do I add KSP plugin to android project gradle?

10.3k views Asked by At

My aim is to add the KSP plugin to gradle as stated in the question heading.

just go through the line below each //from quickstart comments and you'll see all the error origins.
To add the dependencies I referred the Kotlin KSP quickstart documentation.
Now, the code might not be an exact replica of the one from the docs because that didn't work and so, I attempted some failed changes to try and make it work.


build.gradle(app/module) -

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    //from quickstart
    id 'org.jetbrains.kotlin.jvm'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.cryptile"
        minSdk 26
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'

    implementation 'com.google.android.material:material:1.5.0'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    def room_version = "2.4.2"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

    //what I want to implement
    ksp "androidx.room:room-compiler:$room_version"

    //from quickstart
    implementation 'com.google.devtools.ksp:symbol-processing-api:1.6.10-1.0.2'
}

build.gradle(project) -

//from quickstart
buildscript {
    dependencies {
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21'
    }
}
plugins {
    id 'com.android.application' version '7.1.3' apply false
    id 'com.android.library' version '7.1.3' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
    //from quickstart
    id 'org.jetbrains.kotlin.jvm' version '1.6.21' apply false
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

settings.gradle -

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "CRYPTILE"
include ':app'

Any help is appriciated.

2

There are 2 answers

0
Nikola Despotoski On BEST ANSWER

KSP guides failed to mention where to reference your processor. Use META-INF resource directory to reference your processor processor in your processormodule:

src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider

In this file you reference your ProcessorProvider:

com.my.package.processor.MySymbolProcessorProvider

This is important step in order your processor provider be recognized and later instantiate the actual processor.

If your dependencies setup are not in place please read further here https://medium.com/@nikoladespotoski/hands-on-google-ksp-c0508d623863

1
Ngaox Thao On

Here is a way to add ksp in android (version: giraffe) gradle library:

  • First, click in build.gradle(project) and add [ id("com.google.devtools.ksp") version "1.8.10-1.0.9" apply false ] in plugin {}

  • Next, click in build.gradle(module) to add this line [ id("com.google.devtools.ksp") ] in plugin{}. Then, implement ksp in dependencies block:

      //To use Kotlin Symbol Processing (KSP)
      ksp("androidx.room:room-compiler:2.5.2")
    

The version of ksp belongs the room that you use. Thanks for reading!.