You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加kapt与Dagger Hilt时Gradle出现内部编译错误求助

Android项目添加Kapt与Dagger Hilt后Gradle编译失败

错误信息

Execution failed for task ':app:kaptGenerateStubsDebugKotlin'.

> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
> Internal compiler error. See log for more details

完整错误堆栈

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:kaptGenerateStubsDebugKotlin'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.lambda$executeIfValid$1(ExecuteActionsTaskExecuter.java:130)
at org.gradle.internal.Try$Failure.ifSuccessfulOrElse(Try.java:293)

项目配置

根目录build.gradle

plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.kotlin.android) apply false
    id("com.google.gms.google-services") version "4.4.1" apply false
    alias(libs.plugins.dagger.hilt) apply false
}

App模块build.gradle(修正语法错误后)

plugins { 
    alias(libs.plugins.android.application) 
    alias(libs.plugins.kotlin.android) 
    id("com.google.gms.google-services") 
    kotlin("kapt") 
}
    
android { 
    namespace = "com.example.chatapp" 
    compileSdk = 34 
    defaultConfig { 
        applicationId = "com.example.chatapp" 
        minSdk = 26 
        targetSdk = 34 
        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.1"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
    
    implementation(libs.dagger.hilt.android)
    implementation(libs.firebase.crashlytics.buildtools)
    kapt(libs.dagger.hilt.compiler)
    implementation(libs.dagger.hilt.compose)
    implementation(platform("com.google.firebase:firebase-bom:32.8.0"))
    implementation("com.google.firebase:firebase-analytics")
    implementation("com.google.firebase:firebase-auth")
    implementation("com.google.firebase:firebase-database")
    implementation("com.google.firebase:firebase-storage")
}

已尝试的方案

  • 更换kapt依赖版本
  • 移除Dagger相关配置

解决步骤

1. 修正Gradle语法错误

你原App模块的build.gradle中,所有依赖声明被错误地放在了android块内部,这是无效的Gradle语法,必须将依赖移到独立的dependencies块中(如上面修正后的配置所示)。

2. 应用Hilt插件到App模块

根目录仅声明了Hilt插件但未在App模块应用,需要在App模块的plugins块中添加:

alias(libs.plugins.dagger.hilt)

3. 确保Hilt插件与依赖版本一致

打开libs.versions.toml(版本目录文件),确认Hilt的插件版本和依赖版本完全匹配,示例配置:

[versions]
daggerHilt = "2.48"

[libraries]
dagger-hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "daggerHilt" }
dagger-hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "daggerHilt" }
dagger-hilt-compose = { module = "com.google.dagger:hilt-navigation-compose", version.ref = "daggerHilt" }

[plugins]
dagger-hilt = { id = "com.google.dagger.hilt.android", version.ref = "daggerHilt" }

4. 匹配Kotlin与Compose编译器版本

Compose Compiler版本1.5.1需要搭配Kotlin1.9.20版本,修改libs.versions.toml中的Kotlin版本:

[versions]
kotlin = "1.9.20"

5. 配置Hilt Application类

创建带有@HiltAndroidApp注解的Application类:

@HiltAndroidApp
class ChatAppApplication : Application()

并在AndroidManifest.xml中指定该类:

<application
    android:name=".ChatAppApplication"
    ...>
</application>

6. 清理缓存并重新构建

  • 执行Android Studio菜单:File > Invalidate Caches... > Invalidate and Restart
  • 或在终端运行:
./gradlew clean build --refresh-dependencies

7. 获取详细错误日志

在终端运行以下命令,查看完整错误堆栈以定位具体问题:

./gradlew kaptGenerateStubsDebugKotlin --stacktrace

内容的提问来源于stack exchange,提问作者Mateusz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 14:40:06