Injecting a dependency with Dagger 2 only into one of the product flavors

邮差的信 提交于 2019-12-13 02:06:08

问题


My application consists of two product flavors: free and pro. I have an ApplicationModule that provides the object for ads, but this object is only required by the free version, as the pro version does not have the com.google.android.gms:play-services-ads:8.3.0 dependency. How do I structure my project such that only if the free product flavor is in use the ApplicationModule provides the ads object?


回答1:


Since you have two product flavors, i am assuming the free version classes are in the proper version structure, outside of the 'main' resource set. and im assuming you have another resource set for paid version.

So, initialize the dagger component inside main source set, but have ApplicationModule reside in each flavors source set. The module is the provider, so if you want to change the dependencies you'll be able to by defining different providers even though both source sets (paid ,nonpaid) will have the same ApplicationModule in each.



来源:https://stackoverflow.com/questions/34033452/injecting-a-dependency-with-dagger-2-only-into-one-of-the-product-flavors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!