安卓注解库hilt配置
一、在app目录下build.gradle中引入hilt插件
dependencies {
implementation 'com.google.dagger:hilt-android:2.40.5'
kapt 'com.google.dagger:hilt-compiler:2.40.5'
// For instrumentation tests
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.40.5'
kaptAndroidTest 'com.google.dagger:hilt-compiler:2.40.5'
// For local unit tests
testImplementation 'com.google.dagger:hilt-android-testing:2.40.5'
kaptTest 'com.google.dagger:hilt-compiler:2.40.5'
}
执行同步操作sync——成功
项目编译运行——失败
报错如下:
^
Expected @AndroidEntryPoint to have a value. Did you forget to apply the Gradle Plugin? (dagger.hilt.android.plugin)
See https://dagger.dev/hilt/gradle-setup.html
[Hilt] Processing did not complete. See error above for details.
二、根据报错提示添加plugins ‘dagger.hilt.android.plugin’
可以这样添加
plugins {
id 'dagger.hilt.android.plugin'
}
或者这样添加
apply plugins 'dagger.hilt.android.plugin'
添加上后再次同步sync——失败
报错如下:
Plugin [id: 'dagger.hilt.android.plugin'] was not found in any of the following sources:
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'dagger.hilt.android.plugin'] was not found in any of the following sources:
三、根据报错可知,该plugin的id找不到
查阅资料得知需要在项目build.gradle文件下添加如下内容:
dependencies {
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.37'
}
添加后同步sync——成功
项目编译运行——成功
至此hilt配置完成。