如何将Dagger-Android转换为Hilt

The new Hilt library is helping us to apply the Dependency inversion principle in the code, and structure it in a layered way. It defines a standard to do Dependency Injection by providing containers for every Android class in the project.

新的Hilt库正在帮助我们在代码中应用依赖倒置原则 ,并以分层的方式进行结构化。 它通过为项目中的每个Android类提供容器来定义进行依赖注入的标准。

Hilt is created on top of the Android DI library Dagger, so it enjoys the scalability, runtime performance, and all the stuff that Dagger provides. It is also including managing lifecycles in the application automatically. Hilt library integrates Jetpack and Android framework classes. It removes most of the boilerplate code because the developer has bigger fish to fry - like defining and injecting bindings, but without managing the Dagger setup. Keep in mind that Hilt is still in alpha!

Hilt是在Android DI库Dagger的顶部创建的,因此它具有可伸缩性,运行时性能以及Dagger提供的所有功能。 它还包括自动管理应用程序中的生命周期。 Hilt库集成了Jetpack和Android框架类。 它删除了大部分样板代码,因为开发人员需要炸很多鱼,例如定义和注入绑定,但无需管理Dagger设置。 请记住,Hilt仍处于alpha状态!

Note: Here I’ll focus on converting the dagger-android library to Hilt.

注意: 在这里,我将重点介绍将dagger-android库转换为Hilt。

Our starting point will be build.gradle(:app) file - so, go back to the drawing board:

我们的起点将是build.gradle(:app)文件-因此,返回到绘图板:

//Hilt
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
// Hilt ViewModel extension
def hilt_jetpack_version = "1.0.0-alpha01"
implementation "androidx.hilt:hilt-lifecycle- viewmodel:$hilt_jetpack_version"
kapt "androidx.hilt:hilt-compiler:$hilt_jetpack_version"
// Hilt testing dependencies
androidTestImplementation "com.google.dagger:hilt-android-testing:$hilt_version"
kaptAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version"
kaptAndroidTest "androidx.hilt:hilt-compiler:$hilt_jetpack_version"

👉 In Application.kt file, we need to remove HasAndroidInjector extension, and add @HiltAndroidApp above the class name:

Application在Application.kt文件中,我们需要删除 HasAndroidInjector扩展,并在类名称上方添加@HiltAndroidApp

@HiltAndroidAppclass SIOTv2Application : Application() {...}

Apply @HiltAndroidApp annotation to Application class. Now Hilt generates Application Component - this is the central point of our global dependency injection setup.

@HiltAndroidApp批注应用于Application类。 现在,Hilt生成了应用程序组件-这是我们全局依赖项注入设置的中心点。

👉 Remove Application/Presentation Component (with all the listed modules), which contain stuff like this:

👉 删除包含以下所有内容的应用程序/演示组件(带有所有列出的模块):

@Singleton
@Component(
modules = [FirstModule::class,
SecondModule::class,
ActivityBuilder::class,
ViewModelFactoryModule::class,
ViewModelModule::class]
)

Hilt automatically generates:

触发自动生成:

  • Components for integrating Android framework classes

    集成Android框架类的组件
  • Pre-defined bindings and qualifiers

    预定义的绑定和限定词
  • Scope annotations

    范围注释

👉 Add the Application Module class instead of Component(s), for example:

👉 添加 Application Module类而不是Component ,例如:

@InstallIn(ApplicationComponent::class)
@Module
object ApplicationModule {
@Singleton
@Provides
fun provideRetrofitService(): APInterface =
Builder()
.baseUrl(TestAPInterface.API_URL)
.addConverterFactory(
create(
Moshi.Builder()
.add(NullToEmptyStringAdapter)
.add(KotlinJsonAdapterFactory())
.build()
)
)
.build()
.create(TestAPInterface::class.java)

... @Singleton
@Provides
fun providesTokenInterceptor(): TokenInterceptor {
return TokenInterceptor()
} ...

The Component also contains a reference to the Activity, Fragment, View, and Service components. When constructing the Application Component class, an ApplicationContext module is instantiated and bound to the component, giving global access to an application-level context if there is the @ApplicationContext annotation.

组件还包含对Activity,Fragment,View和Service组件的引用。 构造应用程序组件类时,将实例化ApplicationContext模块并将其绑定到组件,如果存在@ApplicationContext批注,则可以全局访问应用程序级上下文。

The Component will handle the injection of the Application class. For this, it uses a single generated member injector class for all the required members of the Application class. This is triggered when an external call to the inject function of the component is made.

Component将处理Application类的注入。 为此,它将单个生成的成员注入器类用于 Application类的所有必需成员。 当从外部调用该组件的inject函数时,将触发此事件。

  • The component contains the required access points for dependencies.

    该组件包含依赖项所需的访问点。
  • For dependencies that do not require construction declarations, these will be instantiated on-the-fly.

    对于不需要构造声明的依赖项,这些将在运行时实例化。

👉 Each Activity/Fragment must have @AndroidEntryPoint annotation on top.

Activity每个活动/片段的顶部都必须带有@AndroidEntryPoint批注。

👉 In each ViewModel replace @Inject annotation with @ViewModelInject.

each在每个ViewModel中,将@Inject注释替换为@ViewModelInject。

So that’s it, after these changes your application will use the Hilt library instead of Dagger! But keep in mind that Dagger and Hilt can coexist together if your application has specific requirements/use cases. Hope you found this story interesting. Feel free to share your feedback and comments.

就这样,完成这些更改后,您的应用程序将使用Hilt库而不是Dagger! 但是请记住,如果您的应用程序有特定的需求/用例,那么Dagger和Hilt可以并存 。 希望您发现这个故事有趣。 随时分享您的反馈和意见。

Thanks for reading & happy coding! 🙌

感谢您阅读和编码愉快! 🙌

翻译自: https://medium.com/swlh/how-to-convert-dagger-android-to-hilt-fed4bbfec577

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值