Search code examples
android-studioandroid-gradle-pluginandroid-jetpack-composeannotation-processing

kotlin-kapt plugin with version catalogs


I have migrated my build to use version catalogs. I have removed the following from my build.gradle

plugins {
id 'kotlin-android'
id 'kotlin-kapt'
}

and

kapt "androidx.room:room-compiler:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

kapt "com.google.dagger:hilt-compiler:$hilt_version"

now I am getting

warning: The following options were not recognized by any processor: 
'[dagger.hilt.internal.useAggregatingRootProcessor, room.schemaLocation, 
dagger.fastInit, dagger.hilt.android.internal.disableAndroidSuperclassValidation, 
dagger.hilt.android.internal.projectType]'

1 warning

and

A failure occurred while executing 
com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
 > 2 files found with path 'META-INF/gradle/incremental.annotation.processors' from 
 inputs:
  - /.gradle/caches/transforms- 
 3/5f588ae3973231906376832b3350f55d/transformed/jetified-hilt-android-compiler- 
 2.45.jar
   - /.gradle/caches/transforms- 
 3/f1c91d9e7fbd546100043f736678c477/transformed/jetified-dagger-compiler-2.45.jar

Now I am wondering how to add kotlin-kapt as a plugin to the version catalogs. Or how do I use the annotationProcessor with version catalogs?


Solution

  • libs.version.toml

    [versions]
    com-android-application = "8.1.0-alpha11"
    org-jetbrains-kotlin-android = "1.8.0"
    navigation-fragment-ktx = "2.5.3"
    navigation-ui-ktx = "2.5.3"
    
    [libraries]
    navigation-fragment-ktx = { group = "androidx.navigation", name = "navigation-fragment-ktx", version.ref = "navigation-fragment-ktx" }
    navigation-ui-ktx = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "navigation-ui-ktx" }
    
    [plugins]
    com-android-application = { id = "com.android.application", version.ref = "com-android-application" }
    org-jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "org-jetbrains-kotlin-android" }
    org-jetbrains-kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "org-jetbrains-kotlin-android" }
    

    build.gradle(:project)

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    plugins {
        alias(libs.plugins.com.android.application) apply false
        alias(libs.plugins.org.jetbrains.kotlin.android) apply false
        alias(libs.plugins.org.jetbrains.kotlin.kapt) apply false
    }
    

    build.gradle(:app)

    plugins {
        alias(libs.plugins.com.android.application)
        alias(libs.plugins.org.jetbrains.kotlin.android)
        alias(libs.plugins.org.jetbrains.kotlin.kapt)
    }
    
    android {
        namespace = "com.example.jetpack"
        compileSdk = 33
    
        defaultConfig {
            applicationId = "com.example.jetpack"
            minSdk = 24
            targetSdk = 33
            versionCode = 1
            versionName = "1.0"
    
            testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                isMinifyEnabled = 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"
        }
        buildFeatures {
            viewBinding = true
            dataBinding =true
        }
    }
    
    dependencies {
        ....
        implementation(libs.navigation.fragment.ktx)
        implementation(libs.navigation.ui.ktx)
       ...
    }
    

    For more detail visit this website:

    https://www.droidcon.com/2022/05/13/gradle-version-catalogs-for-an-awesome-dependency-management/