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

Windows重装后导入GitHub项目遇Hilt插件未找到错误

问题描述

昨日重装Windows系统后,从GitHub导入之前推送的项目(推送前Gradle编译完全正常),编译时弹出错误:

Plugin com.google.dagger.hilt.android was not found in any of the following sources

试过修改Gradle版本,问题依旧存在。

模块级build.gradle代码

defaultConfig {
    applicationId "com.example.firebasetesting"
    minSdk 24
    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.0'
}
packagingOptions {
    resources {
        excludes += '/META-INF/{AL2.0,LGPL2.1}'
    }
}

def nav_version = "2.5.3"

dependencies {
    implementation "androidx.navigation:navigation-compose:$nav_version"
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
    implementation 'androidx.activity:activity-compose:1.7.0'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.4.0'
    implementation 'androidx.wear.tiles:tiles-material:1.1.0-alpha04'
    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"
    implementation "io.coil-kt:coil-compose:2.2.2"

    //Lifecycle
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1"
    implementation "androidx.lifecycle:lifecycle-runtime-compose:2.6.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1"

    // Hilt
    implementation "com.google.dagger:hilt-android:2.44"
    kapt "com.google.dagger:hilt-compiler:2.44"
    implementation "androidx.hilt:hilt-navigation-compose:1.0.0"

    // Network Dependencies
    implementation "com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.3"
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation "com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.3"
    kapt "com.squareup.retrofit2:retrofit:2.9.0"
    implementation 'com.google.code.gson:gson:2.9.0'

    // firebase
    implementation platform('com.google.firebase:firebase-bom:31.4.0')
    implementation 'com.google.firebase:firebase-auth-ktx'
    implementation 'com.google.firebase:firebase-auth-ktx:21.2.0'
    implementation 'com.google.android.gms:play-services-auth:20.5.0'
}

kapt {
    correctErrorTypes true
}
解决方法

这个问题核心是Hilt插件没在Gradle配置里正确声明,按下面步骤操作就能修复:

1. 配置项目级build.gradle

打开项目根目录的build.gradle,确保添加了Hilt插件的classpath:

plugins {
    id 'com.android.application' version '7.4.2' apply false
    id 'com.android.library' version '7.4.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
    // 版本和你用的Hilt依赖保持一致(这里是2.44)
    id 'com.google.dagger.hilt.android' version '2.44' apply false
}

如果你的项目用的是旧版Gradle结构(带buildscript块),就改成这样:

buildscript {
    dependencies {
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.44"
    }
}

2. 配置模块级build.gradle

在模块的build.gradle顶部,添加Hilt和kapt插件的应用声明:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    // 应用Hilt插件
    id 'com.google.dagger.hilt.android'
    // 确保kapt插件已启用
    id 'kotlin-kapt'
}

3. 同步并重建项目

  • 点击Android Studio右上角的「Sync Project with Gradle Files」按钮同步配置
  • 依次执行Build > Clean Project和Build > Rebuild Project

4. 额外检查

确认settings.gradle里配置了Maven Central仓库,Hilt依赖托管在这里:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 09:58:13