android 这些gradle deps和dagger/hilt编译器有什么区别

dl5txlt9  于 5个月前  发布在  Android
关注(0)|答案(2)|浏览(63)

我对何时何地使用以下android匕首柄gradle依赖项感到困惑

"com.google.dagger:dagger-compiler"
"com.google.dagger:hilt-compiler"
"com.google.dagger:hilt-android-compiler"
"androidx.hilt:hilt-compiler"

字符串
我需要在我的Android项目中使用它们吗?每个人都做什么?

csbfibhn

csbfibhn1#

首先,这取决于你的项目中的场景。
“com.google.dagger:dagger-compiler”- * 如果您在项目中使用dagger作为依赖注入器,则可以使用此选项。*
“com.google.dagger:hilt-compiler”或“com.google.dagger:hilt-android-compiler”- * 这两种方式你都可以使用,同时在你的项目中使用Hilt作为依赖注入器。在一些文档中你会找到第一个,在一些文档中你会找到第二个。*
“androidx.hilt:hilt-compiler”- * 这是一个额外的依赖项,当你想启用androidx库中某些类的依赖项注入时会添加它。它会添加到hilt或dagger的依赖项中,无论你在项目中使用什么 *。

你必须记住,希尔特是匕首的继任者,它的喷气背包推荐。

进一步检查的链接-Using Hilt With Jetpack LibraryGoogle Dagger Dependency List

k0pti3hp

k0pti3hp2#

  • Dagger和Hilt编译器“com.google.dagger:dagger-compiler”,“com.google.dagger:hilt-compiler””都是Android和Java的快速依赖注入器(https://mvnrepository.com/artifact/com.google.dagger/dagger-compiler),https://mvnrepository.com/artifact/com.google.dagger/hilt-compiler,这意味着它们用于创建类的示例,这些示例将被注入到需要这些依赖的其他类中;但是这两个框架以不同的方式创建依赖。
  • Dagger创建了一个图,以查找在项目中需要时在何处获取依赖项。为了让Dagger做到这一点,我们需要添加@Component annotation;尽管我们不需要在Hilt中创建此组件。当我们添加@HiltAndroidApp annotation时,Hilt已经为我们做了这一点。此外,应用程序的示例可以由Hilt @Injected到其他模块中
  • 在Dagger中,我们创建了ActivityScope、FragmentScope等作用域注解来指定生命周期,但hilt为我们提供了Application、Activity、Fragment、Service、View等核心组件。在Dagger中,@ContributesAndroidInjector用于自动生成子组件的代码,并删除常用代码。这导致我们在大型项目中编写过多的@Scope注解和@Inject注解;但是,我们不需要在Hilt中编写这些annotation。相反,Hilt提供了@AndroidEntryPoint annotation。通过这种方式,示例被Hilt @Injected到必填字段中。
  • 在Jetpack ViewModel结构中,我们需要将ViewModel示例@Inject到android组件中。我们需要为每个ViewModel提供ViewModelFactory和ViewModelModul。在Hilt,我们不需要将它们写下来。相反,它为Hilt SavedStateHandle提供了@ViewModelInject和@Assisted annotation。

查看此中型文章以了解更多详细信息:https://medium.com/huawei-developers/differences-between-dagger-and-hilt-3e347d24fb86

相关问题