Android Kotlin 项目中 Dagger Hilt 和 KSP 的问题:无法解析符号“Inject”

Ala*_*lla 5 android kotlin google-developer-tools dagger-hilt supabase

我\xe2\x80\x99m 使用 Kotlin 处理 Android 项目,并使用 Dagger Hilt 进行依赖注入。最近,我\xe2\x80\x99ve尝试从kapt迁移到KSP(Kotlin符号处理)来生成代码,因为kapt现在处于\xe2\x80\x9c维护模式\xe2\x80\x9d。但是,我\xe2\x80\x99ve遇到了一个问题:KSP未检测到Dagger Hilt注释,例如@Inject,@Binds和@Module,并且我\xe2\x80\x99m得到了\xe2\x80\ x9c无法解析符号\xe2\x80\x9d错误。

\n

我正在使用 \xe2\x80\x9c Framework Quickstarts: Use Supabase with Android Kotlin \xe2\x80\x9d 文档测试与 Supabase 的连接,您可以在其中找到代码。我的 build.gradle.kts(模块):

\n
plugins {\n    id("com.android.application")\n    id("org.jetbrains.kotlin.android") version "1.9.20"\n    id ("com.google.relay") version "0.3.10"\n    kotlin("plugin.serialization") version "1.9.0"\n    id ("com.google.devtools.ksp") version "1.9.20-1.0.14"\n}\n\nandroid {\n    namespace = "xxxxxxxxxxx"\n    compileSdk = 34\n\n    defaultConfig {\n        applicationId = "xxxxxxxxxxxx"\n        minSdk = 24\n        targetSdk = 33\n        versionCode = 1\n        versionName = "1.0"\n\n        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"\n    }\n\n    buildTypes {\n        release {\n            isMinifyEnabled = false\n            proguardFiles(\n                getDefaultProguardFile("proguard-android-optimize.txt"),\n                "proguard-rules.pro"\n            )\n        }\n    }\n    compileOptions {\n        sourceCompatibility = JavaVersion.VERSION_1_8\n        targetCompatibility = JavaVersion.VERSION_1_8\n    }\n    kotlinOptions {\n        jvmTarget = "1.8"\n    }\n}\n\ndependencies {\n\n    implementation("androidx.core:core-ktx:1.12.0")\n    implementation("androidx.appcompat:appcompat:1.6.1")\n    implementation("com.google.android.material:material:1.10.0")\n    implementation(platform("io.github.jan-tennert.supabase:bom:1.4.6-dev-1"))\n    implementation("io.github.jan-tennert.supabase:postgrest-kt")\n    implementation("io.github.jan-tennert.supabase:realtime-kt")\n    implementation("io.github.jan-tennert.supabase:storage-kt")\n    implementation("io.github.jan-tennert.supabase:functions-kt")\n    implementation("io.github.jan-tennert.supabase:gotrue-kt")\n    implementation("io.github.jan-tennert.supabase:compose-auth")\n    implementation("io.github.jan-tennert.supabase:compose-auth-ui")\n    implementation("io.ktor:ktor-client-cio:2.3.5")\n    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")\n    ksp("com.google.dagger:dagger-compiler:2.48.1") // Dagger compiler\n    ksp("com.google.dagger:hilt-compiler:2.48.1")   // Hilt compiler\n    ksp("androidx.room:room-compiler:2.6.0")\n    ksp("androidx.hilt:hilt-compiler:1.1.0")\n    testImplementation("junit:junit:4.13.2")\n    androidTestImplementation("androidx.test.ext:junit:1.1.5")\n    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")\n}\n\n\n
Run Code Online (Sandbox Code Playgroud)\n

我的 build.gradle.kts(项目):

\n
plugins {\n    id("com.android.application") version "8.1.2" apply false\n    id("org.jetbrains.kotlin.android") version "1.9.20" apply false\n    id("com.google.devtools.ksp") version "1.9.20-1.0.14" apply false\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我\xe2\x80\x99已经尝试清理和重建我的项目,验证我的依赖版本,并检查我的 Dagger Hilt 和 KSP 设置。我还查看了官方 KSP 文档和 Google 文档,但我还没有解决该问题。我预计迁移到 KSP 后,Dagger Hilt 注释将被正确检测到,并且代码将按预期工作。

\n

Ala*_*lla 4

终于解决了,我的错误是没有写

implementation 'com.google.dagger:hilt-android:2.48.1'
Run Code Online (Sandbox Code Playgroud)

来自 build.gradle.kts (模块)的依赖项。我还在 build.gradle 文件中做了一些更改

现在它完全可以工作了。

我留下我的build.gradle.kts(模块):

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.google.dagger.hilt.android")
id ("com.google.devtools.ksp")
id ("com.google.relay") version "0.3.10"
kotlin("plugin.serialization") version "1.9.0"

}

android {
namespace = "com.xxxxxxx.xxxx"
compileSdk = 34

defaultConfig {
    applicationId = "com.xxxxx.xxxxx"
    minSdk = 24
    targetSdk = 33
    versionCode = 1
    versionName = "1.0"

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

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 {
    compose = true
}
composeOptions {
    kotlinCompilerExtensionVersion = "1.5.0"
}
packaging {
    resources {
        excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {

implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
implementation("androidx.activity:activity-compose:1.8.0")
implementation(platform("androidx.compose:compose-bom:2023.10.01"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("com.google.dagger:hilt-android:2.48.1")
ksp("androidx.room:room-compiler:2.6.0")
ksp("com.google.dagger:dagger-compiler:2.48.1")
ksp("com.google.dagger:hilt-android-compiler:2.48.1")
implementation(platform("io.github.jan-tennert.supabase:bom:1.4.6-dev-1"))
implementation("io.github.jan-tennert.supabase:postgrest-kt")
implementation("io.github.jan-tennert.supabase:realtime-kt")
implementation("io.github.jan-tennert.supabase:storage-kt")
implementation("io.github.jan-tennert.supabase:functions-kt")
implementation("io.github.jan-tennert.supabase:gotrue-kt")
implementation("io.github.jan-tennert.supabase:compose-auth")
implementation("io.github.jan-tennert.supabase:compose-auth-ui")
implementation("io.ktor:ktor-client-cio:2.3.5")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose- 
bom:2023.10.01"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}
Run Code Online (Sandbox Code Playgroud)

和我的 build.gradle.kts (项目)

// Top-level build file where you can add configuration options common to 
all sub-projects/modules.
plugins {
id("com.android.application") version "8.1.2" apply false
id("org.jetbrains.kotlin.android") version "1.9.20" apply false
id("com.google.dagger.hilt.android") version "2.48.1" apply false
id("com.google.devtools.ksp") version "1.9.20-1.0.14" apply false
}
Run Code Online (Sandbox Code Playgroud)