gpt4 book ai didi

带有 Dagger Hilt 的 Android 动态功能模块

转载 作者:行者123 更新时间:2023-12-04 09:26:26 34 4
gpt4 key购买 nike

我已经构建了一个基于格子应用程序的 fragment 、子组件和依赖组件的动态功能模块示例,如果您想查看 here是链接。现在,我正在尝试使用 official android document 将其转换为 Dagger Hilt .

核心模块是库模块,app模块和动态特性模块依赖

@Singleton
@Component(modules = [CoreModule::class])
interface CoreComponent {

/*
Provision methods to provide dependencies below to components that depends on
CoreComponent
*/
fun coreDependency(): CoreDependency

fun coreCameraDependency(): CoreCameraDependency

fun corePhotoDependency(): CorePhotoDependency

fun coreActivityDependency(): CoreActivityDependency

@Component.Factory
interface Factory {
fun create(@BindsInstance application: Application): CoreComponent
}

}

它是模块

@Module(includes = [CoreProvideModule::class])
abstract class CoreModule {
@Binds
abstract fun bindContext(application: Application): Context
}

@Module
object CoreProvideModule {

@Singleton
@Provides
fun provideCoreDependency(application: Application) = CoreDependency(application)

@ActivityScope
@Provides
fun provideCoreActivityDependency(context: Context) = CoreActivityDependency(context)

@Provides
fun provideCoreCameraDependency(): CoreCameraDependency = CoreCameraDependency()

@Provides
fun provideCorePhotoDependency(): CorePhotoDependency = CorePhotoDependency()

}

CoreComponent 是如何迁移的?提供方法是否仍然存在,我只是改变

@Singleton
@DefineComponent

@Singleton
@DefineComponent(parent = ApplicationComponent.class)

对于 CoreModule 我想我只改变

@EntryPoint
@InstallIn(CoreComponent::class)

或者这是为了在 CoreComponent 中添加配置方法?

如何在应用模块中创建子组件?

如果有人有带有动态功能 fragment 和刀柄的示例,或要构建的教程,我们将非常欢迎。我现在正在研究它,如果我想通了,我会发布一个答案

最佳答案

我终于明白了。

对于应用结构

FeatureCamera  FeaturePhotos  (Dynamic Feature Modules)  
| | |
| ----App
| |
core(android-library)

相机动态特征模块依赖于核心模块,照片动态特征模块依赖于应用。

首先在库模块中创建一个CoreModule

@InstallIn(ApplicationComponent::class)
@Module
class CoreModule {

@Singleton
@Provides
fun provideCoreDependency(application: Application) = CoreDependency(application)

@Provides
fun provideCoreActivityDependency(context: Application) = CoreActivityDependency(context)

@Provides
fun provideCoreCameraDependency(): CoreCameraDependency = CoreCameraDependency()

@Provides
fun provideCorePhotoDependency(): CorePhotoDependency = CorePhotoDependency()
}

需要一个带有@EntryPoint 的接口(interface)来提供在此接口(interface)中定义的方法,如果您没有为该依赖项定义一个方法,即使有一个@,您也无法注入(inject)它为其提供方法。这些是将应用程序或上下文作为唯一参数的模拟依赖项。

@EntryPoint
@InstallIn(ApplicationComponent::class)
interface CoreComponent {

/*
Provision methods to provide dependencies to components that depend on this component
*/
fun coreDependency(): CoreDependency

fun coreActivityDependency(): CoreActivityDependency

fun coreCameraDependency(): CoreCameraDependency

fun corePhotoDependency(): CorePhotoDependency

}

在camera dynamic feature module中,基于这个dynamic feature module内部的依赖创建另一个module。

@InstallIn(FragmentComponent::class)
@Module(includes = [CameraBindModule::class])
class CameraModule {

@Provides
fun provideCameraObject(context: Context) = CameraObject(context)
}

@InstallIn(FragmentComponent::class)
@Module
abstract class CameraBindModule {
@Binds
abstract fun bindContext(application: Application): Context
}

component 将依赖项注入(inject)此 DFM 中的 FragmentsActivities

@Component(
dependencies = [CoreComponent::class],
modules = [CameraModule::class]
)
interface CameraComponent {

fun inject(cameraFragment1: CameraFragment1)
fun inject(cameraFragment2: CameraFragment2)


fun inject(cameraActivity: CameraActivity)

@Component.Factory
interface Factory {
fun create(coreComponent: CoreComponent, @BindsInstance application: Application): CameraComponent
}

}

If injected to Activity call in `onCreate()`


DaggerCameraComponent.factory().create(
EntryPointAccessors.fromApplication(
applicationContext,
CoreComponent::class.java
),
application
)
.inject(this)

For injecting to Fragment call in `onCreate()`

DaggerCameraComponent.factory().create(
EntryPointAccessors.fromApplication(
requireActivity().applicationContext,
CoreComponent::class.java
),
requireActivity().application
)
.inject(this)

The trick is here to get dependency interface annotated with `@EntryPoint`
using `EntryPointAccessors.fromApplication()`

Also created Activity based dependencies in app module

**MainActivityModule.kt**

@InstallIn(ActivityComponent::class)
@Module(includes = [MainActivityBindModule::class])
class MainActivityModule {

@Provides
fun provideToastMaker(application: Application) = ToastMaker(application)

@ActivityScoped
@Provides
fun provideMainActivityObject(context: Context) = MainActivityObject(context)

}

@InstallIn(ActivityComponent::class)
@Module
abstract class MainActivityBindModule {

@Binds
abstract fun bindContext(application: Application): Context

}

And only intend to inject these dependencies to Photos dynamic feature module so named it as `PhotoDependencies`

@EntryPoint
@InstallIn(ActivityComponent::class)
interface PhotoModuleDependencies {

fun toastMaker(): ToastMaker

fun mainActivityObject(): MainActivityObject
}


In Photos dynamic feature module create dagger module named `PhotoModule`

@InstallIn(FragmentComponent::class)
@Module(includes = [PhotoBindModule::class])
class PhotoModule {

@Provides
fun providePhotoObject(application: Application): PhotoObject = PhotoObject(application)

}

@InstallIn(FragmentComponent::class)
@Module
abstract class PhotoBindModule {
@Binds
abstract fun bindContext(application: Application): Context
}

And component

@Component(
dependencies = [PhotoModuleDependencies::class],
modules = [PhotoModule::class]
)
interface PhotoComponent {

fun inject(photosFragment1: PhotoFragment1)
fun inject(photosFragment2: PhotoFragment2)

@Component.Factory
interface Factory {
fun create(photoModuleDependencies: PhotoModuleDependencies,
@BindsInstance application: Application): PhotoComponent
}
}


And inject to fragments with


DaggerPhotoComponent.factory().create(
EntryPointAccessors.fromActivity(
requireActivity(),
PhotoModuleDependencies::class.java
),
requireActivity().application
)
.inject(this)

The trick here is to get `EntryPointAccessors.fromActivity` instead of fromApplication.

You can check out [this link][1] if you wish to experiment yourself.

If you wish to add `ViewModel` to dynamic feature modules with hilt you can check out my answer [here][2].


[1]: https://github.com/SmartToolFactory/Dagger2-Tutorials/tree/master/Tutorial10-1DFM-DaggerHilt
[2]: https://stackoverflow.com/questions/63671489/how-to-create-viewmodel-in-dynamic-feature-module-with-dagger-hilt

关于带有 Dagger Hilt 的 Android 动态功能模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62992903/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com