带匕首柄的Android动态功能模块

带匕首柄的Android动态功能模块,android,dagger-2,dynamic-feature-module,dagger-hilt,Android,Dagger 2,Dynamic Feature Module,Dagger Hilt,我已经构建了一个基于plaid应用程序的动态功能模块示例,其中包含片段、子组件和依赖组件,如果你想查看链接的话。现在,我正试图把它转换成匕首柄使用 核心模块是库模块、应用程序模块和动态功能模块 @Singleton @Component(modules = [CoreModule::class]) interface CoreComponent { /* Provision methods to provide dependencies below to compone

我已经构建了一个基于plaid应用程序的动态功能模块示例,其中包含片段、子组件和依赖组件,如果你想查看链接的话。现在,我正试图把它转换成匕首柄使用

核心模块是库模块、应用程序模块和动态功能模块

@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
@EntryPoint
@InstallIn(CoreComponent::class)

对于CoreModule,我想我只会改变

@Singleton
@DefineComponent
@EntryPoint
@InstallIn(CoreComponent::class)
或者这是为了在
CoreComponent
中添加供应方法

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


如果有人有一个动态功能片段和刀柄的示例,或者要构建的教程,那将非常受欢迎。我现在正在研究它,如果我弄明白了,我会发布一个答案,我终于弄明白了

对于应用程序结构

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
的接口才能使用此接口中定义的提供方法,如果您没有为该依赖项定义方法,则即使有
@提供的
方法,也无法将其注入。这些是以应用程序或上下文作为唯一参数的模拟依赖项

@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
    
}
在“摄影机动态功能模块”中,基于此动态功能模块内部的依赖关系创建另一个模块

@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
}
组件
向此DFM中的
片段
活动
注入依赖项

@组成部分( 依赖项=[CoreComponent::class], 模块=[CameraModule::class] ) 接口摄像机组件{

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


fun inject(cameraActivity: CameraActivity)

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

如果在
onCreate()中注入活动调用

用于注入到
onCreate()中的片段调用

这里的技巧是使用
@EntryPoint
使用
EntryPointAccessors.fromApplication()

还在应用程序模块中创建了基于活动的依赖项

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

}
并且只打算将这些依赖项注入照片动态功能模块,称之为
photodependences

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

    fun toastMaker(): ToastMaker

    fun mainActivityObject(): MainActivityObject
}
在照片中,动态功能模块创建名为
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
}
和组件

@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
    }
}
并将其注入碎片中

DaggerPhotoComponent.factory().create(
        EntryPointAccessors.fromActivity(
                requireActivity(),
                PhotoModuleDependencies::class.java
        ),
        requireActivity().application
)
        .inject(this)
这里的技巧是获取
EntryPointAccessors.fromActivity
而不是fromApplication

如果你想自己做实验,你可以去看看


如果你想用刀柄将
ViewModel
添加到动态功能模块中,你可以查看我的答案。

你知道如何使用刀柄在DFM中注入ViewModel吗?@YouQi,你可以查看这个同样经过编辑的答案
DaggerPhotoComponent.factory().create(
        EntryPointAccessors.fromActivity(
                requireActivity(),
                PhotoModuleDependencies::class.java
        ),
        requireActivity().application
)
        .inject(this)