androidkotlinandroid-roomdagger-hiltkapt

How do I make kapt gradle task showing errors in Kotlin code instead of generated Java code?


I'm using kapt for hilt and room. Whenever there is an error related to those libraries, kapt show me all errors in the generated Java code. How do I make it to show errors in Kotlin code instead? This is what it looks like in Android studio.

Just to clarify. It works fine for a valid code. But when there is a mistake (like a missing @Entity annotation on an actual entity class) I want it to point the error in Kotlin code, not in generated Java code.

Here is my project's build.gradle:

plugins {
    id 'com.android.application' version '7.4.1' apply false
    id 'com.android.library' version '7.4.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
    id 'com.google.dagger.hilt.android' version '2.44' apply false
    id 'org.jetbrains.kotlin.kapt' version "1.6.10" apply false
}

And the app's build.gradle:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'org.jetbrains.kotlin.kapt'
    id 'com.google.dagger.hilt.android'
}

android {
    namespace 'org.acanthite.upc'
    compileSdk 33

    defaultConfig {
        applicationId "org.acanthite.upc"
        minSdk 22
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
        coreLibraryDesugaringEnabled true
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.1.1'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}
kapt {
    correctErrorTypes = true
}
ext {
    compose_ui_version = '1.3.3'
    room_version = '2.5.0'
    hilt_version = '2.45'
}
dependencies {
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
    implementation 'androidx.activity:activity-compose:1.6.1'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.3.1'
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-paging:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

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

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"

    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"

    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.2'
}

I've tried clearing Android Studio caches, rebuild and sync it a ton of times, lovered rooms version, but still no luck. It's basicaly just a project generated from Compose template. Plus I added room and hilt dependencies that I've added on top of it.


Solution

  • Looks like it's just the way KAPT works. Migrating to KSP helped, but I digged tons of google pages to find how to do it properly. The official doc (https://kotlinlang.org/docs/ksp-overview.html) only explains how to use it if you're a library developer. There is nothing there for mere mortals who just want to use KSP plugin for gradle. At least I could not find it there.

    Anyway, here is project's build.gradle after migration to KSP (and to Kotlin 1.8.10):

    plugins {
        id 'com.android.application' version '7.4.1' apply false
        id 'com.android.library' version '7.4.1' apply false
        id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
        id 'com.google.devtools.ksp' version '1.8.10-1.0.9' apply false
        id 'com.google.dagger.hilt.android' version '2.45' apply false
    }
    

    App's build.gradle after migration:

    plugins {
        id 'com.android.application'
        id 'org.jetbrains.kotlin.android'
        id 'com.google.devtools.ksp'
        id 'kotlin-kapt'
        id 'com.google.dagger.hilt.android'
    }
    
    android {
        namespace 'org.acanthite.upc'
        compileSdk 33
    
        defaultConfig {
            applicationId "org.acanthite.upc"
            minSdk 22
            targetSdk 33
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            vectorDrawables {
                useSupportLibrary true
            }
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
            coreLibraryDesugaringEnabled true
        }
        kotlinOptions {
            jvmTarget = '1.8'
        }
        buildFeatures {
            compose true
        }
        composeOptions {
            kotlinCompilerExtensionVersion '1.4.2'
        }
        packagingOptions {
            resources {
                excludes += '/META-INF/{AL2.0,LGPL2.1}'
            }
        }
    }
    kapt {
        correctErrorTypes = true
    }
    ext {
        compose_ui_version = '1.3.3'
        room_version = '2.5.0'
        hilt_version = '2.45'
    }
    dependencies {
        implementation 'androidx.core:core-ktx:1.9.0'
        implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
        implementation 'androidx.activity:activity-compose:1.6.1'
        implementation "androidx.compose.ui:ui:$compose_ui_version"
        implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
        implementation 'androidx.compose.material:material:1.3.1'
        implementation "androidx.room:room-runtime:$room_version"
        implementation "androidx.room:room-paging:$room_version"
        implementation "androidx.room:room-ktx:$room_version"
        ksp "androidx.room:room-compiler:$room_version"
    
        implementation "com.google.dagger:hilt-android:$hilt_version"
        kapt "com.google.dagger:hilt-compiler:$hilt_version"
    
        testImplementation 'junit:junit:4.13.2'
        androidTestImplementation 'androidx.test.ext:junit:1.1.5'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
        androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    
        debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
        debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
    
        coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.2'
    }
    

    Pay attention to these line in project's build.gradle:

    id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
    id 'com.google.devtools.ksp' version '1.8.10-1.0.9' apply false
    

    You must find matching ksp version here: https://github.com/google/ksp/releases

    For example, if your kotlin-android plugin is 1.8.10, then you need to find ksp release that has first part matching 1.8.10 and the second part as high, as possible. In my case it's 1.8.10-1.0.9.

    If you're using compose

    You also need to pay attention to this one (app's build.gradle):

    composeOptions {
        kotlinCompilerExtensionVersion '1.4.2'
    }
    

    Go to here https://developer.android.com/jetpack/androidx/releases/compose-kotlin#pre-release_kotlin_compatibility and find the version that matches your kotlin version. In my case 1.4.2 is for kotlin 1.8.10.

    That's how you do it, except...

    This does not work for Dagger/Hilt becase they don't have support for KSP yet. Here is the issue: https://github.com/google/dagger/issues/2349

    So you're basically stuck with Dagger/Hilt untill they add support for KSP.

    Have fun.