无法应用插件“dagger.hilt.android.plugin”
Posted
技术标签:
【中文标题】无法应用插件“dagger.hilt.android.plugin”【英文标题】:Failed to apply plugin 'dagger.hilt.android.plugin' 【发布时间】:2021-11-19 03:03:23 【问题描述】:当我在我的项目中使用 hilt 时,我遇到了这个问题:
A problem occurred evaluating project ':app'.
> Failed to apply plugin 'dagger.hilt.android.plugin'.
> Could not create plugin of type 'HiltGradlePlugin'.
> Could not generate a decorated class for type HiltGradlePlugin.
> com/android/build/gradle/BaseExtension
Unable to load class 'com.android.build.gradle.BaseExtension'.
This is an unexpected error. Please file a bug containing the idea.log file.
主要问题是:无法应用插件'dagger.hilt.android.plugin'。
这是我的 build.gradle(模块)文件:
plugins
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
android
compileSdk 30
defaultConfig
applicationId "net.holosen.hiltdaggerapp"
minSdk 21
targetSdk 30
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.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
// Dagger Core
implementation "com.google.dagger:dagger:2.38.1"
kapt "com.google.dagger:dagger-compiler:2.38.1"
// Dagger Android
api 'com.google.dagger:dagger-android:2.38.1'
api 'com.google.dagger:dagger-android-support:2.37'
kapt 'com.google.dagger:dagger-android-processor:2.38.1'
// Dagger - Hilt
implementation 'com.google.dagger:hilt-android:2.38.1'
kapt 'com.google.dagger:hilt-compiler:2.38.1'
// For instrumentation tests
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
kaptAndroidTest 'com.google.dagger:hilt-compiler:2.38.1'
// For local unit tests
testImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
kaptTest 'com.google.dagger:hilt-compiler:2.38.1'
kapt
correctErrorTypes true
还有我的 build.gradle(项目)文件:
buildscript
repositories
mavenCentral()
dependencies
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
task clean(type: Delete)
delete rootProject.buildDir
这是 settings.gradle 文件:
pluginManagement
repositories
gradlePluginPortal()
google()
mavenCentral()
plugins
id 'com.android.application' version '7.1.0-alpha12'
id 'com.android.library' version '7.1.0-alpha12'
id 'org.jetbrains.kotlin.android' version '1.5.31'
id 'dagger.hilt.android.plugin' version '2.38.1'
dependencyResolutionManagement
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories
google()
mavenCentral()
rootProject.name = "HiltDaggerApp"
include ':app'
我的参考是 'https://dagger.dev/hilt/gradle-setup.html' 中的 Gradle Build Setup。
gradle-wrapper.properties 文件:
#Sun Sep 26 12:17:22 IRST 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
【问题讨论】:
【参考方案1】:这是我遇到同样问题时找到的解决方案,你可以试试吗?
我在 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'
【讨论】:
这对我有用!但我不明白为什么我们需要这样做......以上是关于无法应用插件“dagger.hilt.android.plugin”的主要内容,如果未能解决你的问题,请参考以下文章
PluginApplicationException:无法应用插件类“FlutterPlugin”