使用Dagger中的应用程序组件构建Android Instant App [英] Building an Android Instant App with Application Component from Dagger

查看:61
本文介绍了使用Dagger中的应用程序组件构建Android Instant App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试InstantApps,并且想在我的项目中加入匕首。

I'm currently experimenting with InstantApps and would like to include dagger into my project.

我在设置应用程序AppComponent时遇到了问题。我的应用程序组件包括我应用程序的所有功能匕首模块。

I'm facing an issue setting up an application AppComponent. My application component includes all the feature dagger modules of my app.

我基本上有:


  • 一个基本应用程序模块,其中包含我的应用程序类

  • 多个功能,每个活动都有一个匕首模块,所有这些都带有Base

  • 一个应用模块和即时模块都导入所有功能和基本应用模块。

  • One Base app module containing my Application class
  • Multiple features with each a dagger Module per Activity, all with Base as a dependency.
  • One app module and instant module both importing all the features and the base app module.

我试图在添加Instant App模块之前弄清楚设置。

I'm trying to figure out the setup before adding the Instant App module.

来自InstantApps文档和项目示例。看来Application类需要在Base中。从Dagger文档中,设置Dagger:

From InstantApps documentation and project examples. It seems like the Application class needs to be in Base. From the Dagger documentation, to setup dagger:

 DaggerYourAppComponent.create().inject(this);

应包含在您的应用程序类中。但是,这似乎是不可能的,因为AppComponent需要引用所有功能匕首模块。

Should be included in your application class. However, this seems to be impossible as the AppComponent needs to reference all the feature dagger modules.

我的问题是:


  • 我应该在哪里添加AppComponent匕首模块?

  • 我应该将应用程序保留在app模块中而不是在Base中吗

  • 使用Instant Apps在Dagger周围的任何GitHub存储库或文档?

谢谢 p>

Thank you

推荐答案


  • Instant应用程序非常支持Dagger2。您可以为每个功能模块创建组件类,并创建一个相应的Dagger提供程序类以公开每个功能模块的组件类实例。

  • 每个模块组件类都可以为仅包含在该功能模块中的类声明注入方法。

  • 此外,您还可以拥有一个Application组件类

  • 可以在基本模块中包含的
    Application类中实例化应用程序组件类,并将其暴露给其他
    功能模块通过应用程序类中的静态方法来实现。

    • Dagger2 is very much supported with Instant apps. You can create Component classes for each feature module and a corresponding Dagger provider class to expose the component class instance for each feature module.
    • Each module component class can declare inject methods for the classes contained only in that feature module.
    • In addition you can also have a Application component class in the base module for application wide injection.
    • The application component class can be instantiated in the Application class included in the base module and exposed to other feature modules via static method in the application class.
    • 此处是使用Instant应用程序进行Dagger2注入的示例代码,使事情更清晰。
      https://github.com/willowtreeapps/android-instant-apps-demo

      Here is a sample code of Dagger2 injection with Instant apps to make things clearer. https://github.com/willowtreeapps/android-instant-apps-demo

      这篇关于使用Dagger中的应用程序组件构建Android Instant App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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