问题
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