【问题标题】:Dagger 2 subcomponent across Android modules跨 Android 模块的 Dagger 2 子组件
【发布时间】:2018-12-05 10:54:01
【问题描述】:

最近我开始将我们的应用程序划分为更小的 Android 模块,但我不知道如何构建 dagger 以使其适合我。

我的方法是将“更大”的功能划分为更小的模块。我的大部分功能现在都在 base 模块中。

我现在的工作:

         App
          ^ 
          |
         Base
          ^
          |
      Mvvm core

MyDagger 设置包括:
- 一个@Singleton 作用域AppComponent
- 一个@UserScope 子组件UserComponent

两者都放在Base模块中。

我想要达到的目标:

         App
          ^ 
          |
Feature 1 - Feature 2
          ^
          |
         Base
          ^
          |
      Mvvm core

我的计划是每个功能都将在它们自己的模块中包含一个 @FeatureX 子组件 - 但这是一切都崩溃的地方。

我不知道如何将SubComponent.Build 公开给我的每个功能模块。

这是正确的方法还是我错过了什么?

奖励信息:我还尝试将 AppComponentUserComponent 移动到 App 模块中,但是我无法从我的功能模块中访问我需要注入的组件。

【问题讨论】:

  • 特性包含什么:活动、实用程序类、模型?
  • 是的,该功能需要发挥作用的一切。
  • 所以如果我猜对了,您的功能模块需要访问您的应用程序依赖项,而您的应用程序还需要访问您的功能子组件?
  • 没有。我的功能模块将包含一个子组件,其父组件位于基本模块(AppComponent)中。但我的问题是,要能够创建子组件,您需要父组件
  • 你不需要访问父组件来构建子组件

标签: android module dagger-2


【解决方案1】:

假设您创建了一个名为 cookie 的 android 模块,其中包含一个活动和一个 dagger 模块。

CookieActivity:

class CookieActivity : DaggerAppCompatActivity() {

    @Inject
    lateinit var name: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cookie)

        findViewById<TextView>(R.id.name).text = name
    }
}

Cookie 模块:

@Module
abstract class CookieModule {

    @ContributesAndroidInjector
    abstract fun cookieActivity(): CookieActivity

    @Module
    companion object {

        @Provides
        @JvmStatic
        fun provideName() = "Oreo"
    }
}

现在你想在你的主应用程序中使用这个CookieActivity。首先你需要创建你的AppComponent(注意CookieModule是安装在组件里面的):

@Singleton
@Component(modules = [AndroidSupportInjectionModule::class, CookieModule::class])
interface AppComponent : AndroidInjector<App>

然后让您的Application 类能够注入活动和片段:

class App : Application(), HasActivityInjector, HasSupportFragmentInjector {

    @Inject
    internal lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>

    @Inject
    internal lateinit var dispatchingFragmentInjector: DispatchingAndroidInjector<Fragment>

    override fun onCreate() {
        super.onCreate()
        DaggerAppComponent.builder()
            .build()
            .inject(this)
    }
    override fun activityInjector(): AndroidInjector<Activity>? {
        return dispatchingActivityInjector
    }

    override fun supportFragmentInjector(): AndroidInjector<Fragment>? {
        return dispatchingFragmentInjector
    }
}

您现在可以使用您的CookieActivity,主应用程序将负责注入:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<Button>(R.id.button).setOnClickListener {
            val intent = Intent(this, CookieActivity::class.java)
            startActivity(intent)
        }
    }
}

“奥利奥”将按照CookieModule提供的方式显示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    相关资源
    最近更新 更多