如何使 kapt gradle 任务显示 Kotlin 代码中的错误而不是生成的 Java 代码?

aca*_*ite 2 android kotlin kapt android-room dagger-hilt

我使用 kapt 作为刀柄和空间。每当出现与这些库相关的错误时,kapt 都会向我显示生成的 Java 代码中的所有错误。如何让它显示 Kotlin 代码中的错误? 就是 Android Studio 中的样子。

只是为了澄清。对于有效的代码来说它工作得很好。但是,当出现错误时(例如@Entity实际实体类上缺少注释),我希望它指出 Kotlin 代码中的错误,而不是生成的 Java 代码中的错误。

这是我的项目的 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
}
Run Code Online (Sandbox Code Playgroud)

以及应用程序的 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'
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试清除 Android Studio 缓存、重建和同步它很多次,喜欢房间版本,但仍然没有运气。它基本上只是一个从 Compose 模板生成的项目。另外,我添加了在其之上添加的空间和刀柄依赖项。

aca*_*ite 5

看起来这就是 KAPT 的工作方式。迁移到 KSP 很有帮助,但我挖掘了大量的谷歌页面来找到如何正确地做到这一点。官方文档(https://kotlinlang.org/docs/ksp-overview.html)仅解释了如果您是库开发人员如何使用它。对于那些只想使用 gradle 的 KSP 插件的普通人来说,这里没有任何东西。至少我在那里找不到它。

无论如何,这是迁移到 KSP(以及 Kotlin 1.8.10)后项目的 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.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
}
Run Code Online (Sandbox Code Playgroud)

迁移后应用程序的build.gradle:

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'
}
Run Code Online (Sandbox Code Playgroud)

注意项目的 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
Run Code Online (Sandbox Code Playgroud)

您必须在这里找到匹配的 ksp 版本:https://github.com/google/ksp/releases

例如,如果您的 kotlin-android 插件是1.8.10,那么您需要找到第一部分匹配1.8.10且第二部分尽可能高的 ksp 版本。就我而言,它是1.8.10-1.0.9.

如果您使用的是撰写

您还需要注意这一点(应用程序的build.gradle):

composeOptions {
    kotlinCompilerExtensionVersion '1.4.2'
}
Run Code Online (Sandbox Code Playgroud)

转到此处https://developer.android.com/jetpack/androidx/releases/compose-kotlin#pre-release_kotlin_compatibility 并找到与您的 kotlin 版本匹配的版本。就我而言,1.4.2是针对 kotlin 的1.8.10

你就是这样做的,除了...

这不适用于 Dagger/Hilt,因为它们还不支持 KSP。这是问题: https: //github.com/google/dagger/issues/2349

所以你基本上只能使用 Dagger/Hilt,直到他们添加对 KSP 的支持。

玩得开心。