在以下任何来源中都找不到插件 [id: 'dagger.hilt.android.plugin']

Plugin [id: 'dagger.hilt.android.plugin'] was not found in any of the following sources

当我想使用 @AndroidEntryPoint 时,我收到以下警告,它是我项目中的 属性 刀柄。

 Expected @AndroidEntryPoint to have a value. Did you forget to apply the Gradle Plugin? (dagger.hilt.android.plugin)

当我尝试将 id 'dagger.hilt.android.plugin' 添加到我的项目的模块级别 build.gradle 文件时,出现以下错误。

org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'dagger.hilt.android.plugin'] was not found in any of the following sources:

我尝试将其添加到项目模块级别的 build.gradle 文件中,如下所示。都报错

我试图将它作为类路径添加到项目级别 build.gradle 文件中,在这种情况下我仍然遇到错误。

我在创建默认项目时,创建了一个settings.gradle结构,如下。这是我第一次使用这个构建。我的 Android Studio Android Studio - Bumblebee 版本 | 2021.1.1 金丝雀 13

用于将 dagger hilt 添加到您的项目。按照这些步骤

hilt 依赖项添加到模块的 build.gradle。我假设您使用的是 Kotlin,否则您必须使用 annotationProcessor 而不是 kapt 插件。

dependencies {
  //..
  implementation 'com.google.dagger:hilt-android:2.39.1'
  kapt 'com.google.dagger:hilt-compiler:2.39.1'
  //..
   }

hilt gradle plugin 添加到项目的 build.gradle

dependencies {
    //..
    classpath 'com.google.dagger:hilt-android-gradle-plugin:2.39.1'
  }

kotlin-kapthilt 插件应用于模块 build.gradle

plugins {
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

经过长期的斗争,我解决了如下问题;

我在 settings.gradle 中添加了 resolutionStrategy,如下所示。

 pluginManagement {
        repositories {...}
        plugins { ...}
     resolutionStrategy {
            eachPlugin {
                if( requested.id.id == 'dagger.hilt.android.plugin') {
                    useModule("com.google.dagger:hilt-android-gradle-plugin:2.39.1")
                }
            }
        }
    }

然后,当我将如下所示的 hilt 插件添加到模块级别 build.gradle 文件时,它已正确更新。

plugins{
...
id 'dagger.hilt.android.plugin'
}

只需将其添加到项目的根 build.gradle 文件中即可。

buildscript {
  repositories {
    // other repositories...
    mavenCentral()
  }
  dependencies {
    // other plugins...
    classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40'
  }
}
build.gradle 中的

plugin{} 块用于定义可应用于根 build.gradle 和所有(或部分) [=] 的 Gradle 插件34=] 个子项目。

使用插件块的一个 警告 是它只解析存在于 Gradle 插件门户 中的插件(参见 doc) or custom Maven and Ivy plugin repositories must contain plugin marker artefacts in addition to the artefacts which actually implement the plugin(see doc)。 对于 Android Gradle 插件和 Hilt 插件,他们还没有将这些插件发布到 Gradle 插件门户 ,他们也没有发布他们的 Plugin Marker Artifacts

由于上面缺少 Plugin Marker Artifacts 您需要通过添加以下代码在 settings.gradle 使用 Plugin Resolution Rules 手动解析插件(这是特定于 Hilt Gradle 插件,您必须根据 requested.id.id)

检查不同之处
pluginManagement {
    repositories {
        // Your repo from where Gradle will search for Gradle plugins
    }
    plugins {
        // ...
    }
    resolutionStrategy {
        eachPlugin {
            if(requested.id.id == 'dagger.hilt.android.plugin') {
                    useModule("com.google.dagger:hilt-android-gradle-plugin:${requested.version}")
            }
        }
    }
}

我迟到了。 由于用于在项目级别添加依赖项的新 Gradle 语法,我在 Android Studio Bumblebee 中也遇到了同样的问题。

要在 project-level 中添加匕首柄,您可以使用以下语法:

id 'com.google.dagger.hilt.android' version '2.41' apply false

撰写本文时,最新版本为 2.41。它在 mavenCentral repository.

首先,将 hilt-android-gradle-plugin 插件添加到项目的根目录 build.gradle 文件:

buildscript {
    ...
    dependencies {
        def hilt_version = "2.39.1"
        classpath 'com.android.tools.build:gradle:7.0.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
    }
}

然后,应用 Gradle 插件并将这些依赖项添加到您的 app/build.gradle 文件中:

plugins {
  id 'kotlin-kapt' // for annotation processing
  id 'dagger.hilt.android.plugin'
}

android {
    ...
}

dependencies {
    def hilt_version = "2.39.1"
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-compiler:$hilt_version"
}

 
kapt {
    correctErrorTypes true
}

在您的项目中启用 Java 8,将以下内容添加到 app/build。gradle

android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8.toSttring()
}

我只需要添加这个 class 路径:

classpath 'com.google.dagger:hilt-android-gradle-plugin:2.39.1'

在gradle root

中使用此配置
         plugins {
         id 'com.android.application' version '7.2.0' apply false
         id 'com.android.library' version '7.2.0' apply false
        id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
        id 'com.google.dagger.hilt.android' version '2.42' apply false
         }

       task clean(type: Delete) {
        delete rootProject.buildDir
         }

在第 2 个 gradle 文件中

   plugins {    id 'com.android.application'    id 
   'org.jetbrains.kotlin.android'    id 'kotlin-kapt'    id 
   'com.google.dagger.hilt.android' }
   android {    compileSdk 32
   defaultConfig {
   applicationId "com.example.hilttest"
   minSdk 21
   targetSdk 32
   versionCode 1
   versionName "1.0"

   testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"    }
   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'    } }
   dependencies {
   implementation 'androidx.core:core-ktx:1.7.0'    implementation 
   'androidx.appcompat:appcompat:1.4.1'    implementation 
     `enter code here`'com.google.android.material:material:1.6.0'    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'    testImplementation 'junit:junit:4.13.2'    androidTestImplementation 'androidx.test.ext:junit:1.1.3'    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'    implementation 'com.google.dagger:hilt-android:2.42'    kapt 'com.google.dagger:hilt-compiler:2.42'
   // For instrumentation tests    androidTestImplementation  'com.google.dagger:hilt-android-testing:2.42'    kaptAndroidTest 'com.google.dagger:hilt-compiler:2.42'
   // For local unit tests    testImplementation 'com.google.dagger:hilt-android-testing:2.42'    kaptTest 'com.google.dagger:hilt-compiler:2.42' } kapt {    correctErrorTypes = true }