Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于组件化dagger android使用相关问题 #121

Open
canhuah opened this issue Oct 23, 2018 · 2 comments
Open

关于组件化dagger android使用相关问题 #121

canhuah opened this issue Oct 23, 2018 · 2 comments

Comments

@canhuah
Copy link

canhuah commented Oct 23, 2018

你好 目前在考虑使用组件化改造项目。所以进来学习一波

由于dagger android的初始化需要写在application当中
假如各个Moudle单独作为App运行的话需要些在各个moudle的的Application当中,
那moudle作为lib的时候,该如何初始化各个lib里 的dagger android呢。
希望能提供一些思路。

@liulinjie1990823
Copy link

我是使用了cc的组件化,DDComponentForAndroid也类似
LoginModule工程中:

class LoginModule : IComponent {
    private lateinit var mLoginComponent: LoginComponent

    override fun getName(): String {
        return "LoginModule"
    }

    override fun onCall(cc: CC?): Boolean {
        if (cc == null) {
            return false
        }
        if ("init" == cc.actionName) {
            val componentApplication = cc.context as ComponentApplication
            mLoginComponent = DaggerLoginComponent.builder()
                    .component(componentApplication.mComponent)
                    .build()
        } else if ("injectActivity" == cc.actionName) {
            val activity = cc.context as Activity
            mLoginComponent.activityInjector().inject(activity)
        } else if ("injectFragment" == cc.actionName) {
            val activity = cc.context as FragmentActivity
            val tag = cc.getParamItem<String>("fragment")

            if (tag != null) {
                val findFragmentByTag = activity.supportFragmentManager.findFragmentByTag(tag)
                if (findFragmentByTag != null) {
                    mLoginComponent.supportFragmentInjector().inject(findFragmentByTag)
                }
            }
        }
        return false
    }
}
@Singleton
@Component(
        dependencies = [
            com.llj.component.service.Component::class //依赖库中的Component,可以获得一些字段
        ],
        modules = [
            AndroidInjectionModule::class,
            AndroidSupportInjectionModule::class,
            LoginComponentBuilder::class, //将所有的Activity,Fragment注册进来
            LoginComponentModule::class
        ])
interface LoginComponent {

    //    @Component.Builder
    //    interface Builder {
    //
    //        //提供给AppModule中方法中的Application入参用
    //        @BindsInstance
    //        fun application(application: Application): Builder
    //
    //        fun build(): LoginComponent
    //    }
    //调用该方法才会注入BaseApplication中的@Inject标记的对象
    //    override fun inject(application: BaseApplication)

    fun activityInjector(): DispatchingAndroidInjector<Activity>
    fun supportFragmentInjector(): DispatchingAndroidInjector<Fragment>
}

app工程:

class AppApplication : ComponentApplication() {
    private lateinit var mAppComponent: AppComponent

    override fun injectApp() {
        mAppComponent = DaggerAppComponent.builder()
                .component(mComponent)
                .build()

        //调用LoginComponent中的dagger组件
        CC.obtainBuilder("LoginModule")
                .setActionName("init")
                .build()
                .call()
}
}
class AppApplication : ComponentApplication() {
...
override fun activityInjector(): AndroidInjector<Activity>? {
        return AndroidInjector { activity ->
            val mvpBaseActivity = activity as MvpBaseActivity<*>

            if ("app" == mvpBaseActivity.moduleName()) {
                //主工程
                mAppComponent.activityInjector().inject(activity)
            } else if ("login" == mvpBaseActivity.moduleName()) {
               //LoginModule
                CC.obtainBuilder("LoginModule")
                        .setContext(activity)
                        .setActionName("injectActivity")
                        .build()
                        .call()
            }
        }
    }

    override fun supportFragmentInjector(): AndroidInjector<Fragment>? {
        return AndroidInjector { fragment ->
            val mvpBaseFragment = fragment as MvpBaseFragment<*>

            if ("app" == mvpBaseFragment.moduleName()) {
                //主工程
                mAppComponent.supportFragmentInjector().inject(fragment)
            } else if ("login" == mvpBaseFragment.moduleName()) {
            //LoginModule
                CC.obtainBuilder("LoginModule")
                        .setContext(fragment.context)
                        .addParam("fragment", fragment.tag)
                        .setActionName("injectFragment")
                        .build()
                        .call()
            }
        }
    }
...
}

@xiaojinzi123
Copy link

你好 目前在考虑使用组件化改造项目。所以进来学习一波

由于dagger android的初始化需要写在application当中
假如各个Moudle单独作为App运行的话需要些在各个moudle的的Application当中,
那moudle作为lib的时候,该如何初始化各个lib里 的dagger android呢。
希望能提供一些思路。

其实没必要单独运行的,原因如下:

  • 现在无论是 ddcomponentgradle 插件还是自己手动改变量,前一种可能会产生未知的问题,后一种可能比较麻烦,我个人是建议哪个模块需要独立运行,就单独新建一个 module 去包含要独立运行的模块,这样子不仅符合 module 的常规用法,而且不可能出问题

另外其实 ddcomponent 组件化我从一年前就开始看,内部还是很多可以优化或者西区其他组件化的优点的,所以我原理采纳自 ddcomponent,自行写了一个更加完善的,你不妨可以看看,就当学些也好,链接如下
Component

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants