为什么不安装AndroidInjectionModule仍然可以使用? [英] Why it still works without installing AndroidInjectionModule?

查看:177
本文介绍了为什么不安装AndroidInjectionModule仍然可以使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据有关注入活动对象的Dagger文档,它表示在应用程序组件中安装 AndroidInjectionModule 。但是,没有它,一切都会很好。

According to Dagger documentation about injecting activity objects, it says that installing AndroidInjectionModule in your application component. However, everything is fine without it.

这是否意味着我不需要声明它?

Does it means that I don't need to declare it? Under what circumstances will it be wrong?

例如:

注入实例

For example:
Injected instance

data class Food(val name: String)

模块

@Module
class FoodModule{
    @Provides
    fun provideFood(): Food {
        return Food("cholocate")
    }
}

BindingModule

BindingModule

@Module
abstract class MainActivityModule {
    @ContributesAndroidInjector(modules = [FoodModule::class])
    abstract fun FoodShop(): MainActivity
}

AppComponent(无需安装AndroidInjectionModule)

AppComponent (Without installing AndroidInjectionModule)

@Component(modules = [MainActivityModule::class])
interface AppComponent{
    fun inject(app: App)
}

App

class App : Application(), HasActivityInjector {
    @Inject
    lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>

    override fun onCreate() {
        super.onCreate()
        DaggerAppComponent.create().inject(this)
    }

    override fun activityInjector(): AndroidInjector<Activity> {
        return dispatchingActivityInjector
    }
}

MainActivity

MainActivity

class MainActivity : AppCompatActivity() {

    @Inject
    lateinit var food: Food

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d("test", "Get ${food.name}")
    }
}

它在MainActivity中成功获取巧克力。

It get chocolate successfully in MainActivity.

推荐答案


这是否意味着我不需要声明它?在什么情况下会出错?

Does it means that I don't need to declare it? Under what circumstances will it be wrong?

实际上似乎不需要声明它,但它可能导致编译

It actually seems like you don't need to declare it, but it might lead to compile errors if you don't.

如果您查看 AndroidInjectionModule 您可以看到它只列出了一堆 @Multibinds 框架类型的方法。

If you have a look at AndroidInjectionModule you can see that it just lists a bunch of @Multibinds methods for framework types.

@Multibinds
abstract Map<Class<? extends Activity>, AndroidInjector.Factory<? extends Activity>>
    activityInjectorFactories();

现在,如果您查找声明 @Multibinds 可以看到

Now if you look up Declaring @Multibinds you can read that


对于至少有一个 @IntoSet的集合或映射,不必使用 @Multibinds @ElementsIntoSet @IntoMap 绑定,但是如果需要,则必须声明它们它们可能为空。

You do not have to use @Multibinds for sets or maps that have at least one @IntoSet, @ElementsIntoSet, or @IntoMap binding, but you do have to declare them if they may be empty.

声明它们是否为空正是 AndroidInjectionModule 模块正在为您做。如果Android注入部件需要未定义的注入器工厂映射,则可能会收到编译时错误,指出无法提供。

And to declare them if they may be empty is exactly what the AndroidInjectionModule module is doing for you. If the Android Injection parts would require an undefined Map of injector factories you would probably get a compile time error stating that it cannot be provided.

不需要此模块的原因是因为您使用的是 @ContributesAndroidInjector ,生成的代码将包含 @Binds @IntoMap等。方法,该方法声明绑定映射。上面已经说明了-因为它不再为空了-您将不需要 AndroidInjectionModule 提供的其他 @Multibinds 声明。非空多重绑定。

The reason that you don't need the module is because you're using @ContributesAndroidInjector, of which the generated code will contain a @Binds @IntoMap etc. method, that declares the bindings map. Stated above—as it is not empty anymore—you would not need the additional @Multibinds declaration that AndroidInjectionModule provides for the non-empty multibinding.

您可能不需要 模块,但是它将声明所有如果框架注入器可能为空,则可以为您提供工厂,以防止发生一两个编译错误。毕竟Javadoc只是声明 应该 已安装,而不是必须

You might not need the module, but it will declare all the framework injector factories for you in case that they might be empty, possibly preventing one or two compile errors. After all the javadoc simply states that it should be installed, not that it must.


此模块应安装在用于注入应用程序的组件中类。

This module should be installed in the component that is used to inject the Application class.

这篇关于为什么不安装AndroidInjectionModule仍然可以使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆