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

使用Jetpack Compose+Hilt时出现处理器无法识别选项的错误

Jetpack Compose集成Hilt编译错误解决

我在搭建Jetpack Compose项目并集成Hilt时,遇到编译报错,相关配置和错误信息如下:

模块级build.gradle

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

//    id 'dagger.hilt.android.plugin'
}
apply plugin: 'kotlin-kapt'

android {
    namespace 'com.mawgif.composeapp3'
    compileSdk 33

    defaultConfig {
        applicationId "com.mawgif.composeapp3"
        minSdk 26
        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
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.4.2'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

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'
    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"

    // Kotlin components
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"

    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"

    // networking
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'
    implementation("com.squareup.okhttp3:okhttp:5.0.0-alpha.1")
    implementation("com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.1")
    implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
    kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"

    // coil
    implementation("io.coil-kt:coil-compose:1.4.0")

    // dagger -hilt
    implementation 'com.google.dagger:hilt-android:2.45'
    kapt 'com.google.dagger:hilt-compiler:2.45'
    kapt "androidx.hilt:hilt-compiler:1.0.0"
}

项目级build.gradle

buildscript {
    ext {
        compose_ui_version = '1.3.3'
        kotlinVersion = '1.8.10'
        coroutines = '1.5.0'
        lifecycleVersion = '2.5.1'
        moshi_version = '1.12.0'
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
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.dagger.hilt.android' version '2.45' apply false
}

编译错误

以下选项未被任何处理器识别:'[dagger.fastInit, dagger.hilt.android.internal.disableAndroidSuperclassValidation, dagger.hilt.android.internal.projectType, dagger.hilt.internal.useAggregatingRootProcessor, kapt.kotlin.generated]'


解决方法

  1. 调整kapt插件声明方式
    在模块级build.gradle中,将apply plugin: 'kotlin-kapt'替换为plugins块内声明:

    plugins {
        id 'com.android.application'
        id 'org.jetbrains.kotlin.android'
        id 'com.google.dagger.hilt.android'
        id 'kotlin-kapt' // 直接添加此行
    }
    // 移除原来的 apply plugin: 'kotlin-kapt'
    
  2. 统一依赖版本(可选)
    将androidx.hilt:hilt-compiler版本升级到1.1.0,确保和Hilt 2.45版本兼容:

    kapt "androidx.hilt:hilt-compiler:1.1.0"
    
  3. 确认Hilt初始化配置
    检查自定义Application类是否添加了@HiltAndroidApp注解(Hilt必须的初始化步骤):

    @HiltAndroidApp
    class MyApplication : Application() {
        // 应用初始化逻辑
    }
    
  4. 清理并重建项目
    执行命令:./gradlew clean build,或者在Android Studio中点击Build -> Clean Project后再Rebuild Project。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 15:04:58