I have Shared Preferences as a dagger singleton component. I need to inject it into background services like FirebaseInstanceService. Here's my attempt:
public class InstanceIDListenerService extends FirebaseInstanceIdService {
#Inject
Preferences preferences;
#Override
public void onTokenRefresh() {
((MyApp) getApplication()).getSingletonComponent().inject(this);
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
preferences.setFcmToken(refreshedToken);
}
}
It is used in this way:
<service android:name="com.fcm.InstanceIDListenerService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
Should I use ((MyApp) getApplication()).getSingletonComponent().inject(this); in the onTokenRefresh listener? Is this the correct listener to inject dependencies?
I know this question is old, but I've just been hitting my head on this over the last few hours, and found the solution.
With the new Dagger2 version, you can now have your application implement the HasServiceInjector interface, which lets you inject stuff into services.
A simple example:
1) Create your services module:
#Module
abstract class ServicesModule {
#ContributesAndroidInjector
abstract SomeService ProvideSomeService();
}
2) Add it to your App component:
#Component(modules = {
AndroidSupportInjectionModule.class,
AppModule.class,
ActivitiesModule.class,
ServicesModule.class
})
public interface AppComponent {
#Component.Builder
interface Builder {
#BindsInstance
Builder application(App application);
AppComponent build();
}
void inject(App app);
}
3) Have your application implement the said interface:
public class App extends Application implements HasActivityInjector, HasServiceInjector {
#Inject
DispatchingAndroidInjector<Activity> activityInjector;
#Inject
DispatchingAndroidInjector<Service> serviceInjector;
#Override
public void onCreate() {
super.onCreate();
AppInjector.init(this);
}
#Override
public AndroidInjector<Activity> activityInjector() {
return activityInjector;
}
#Override
public AndroidInjector<Service> serviceInjector() {
return serviceInjector;
}
}
4) Finally, inject your service:
public class SomeService extends Service {
#Inject
SomeDependency dependency;
#Override
public void onCreate() {
AndroidInjection.inject(this);
super.onCreate();
}
// Do things with your dependencies
}
I'm using a Service in the example, but my actual use case was with FirebaseInstanceIdService as well. And this worked.
Related
SOLVED
I am new with Dagger 2 and I am trying to provide Activity Context to classes but without success. I search a lot but did not find any appropriate answer.
I can provide Application Context. But I also need to provide Activity Context as well and I don't know any good way to implement that.
I need to clarify that I am using Dagger for Android dependencies.
def dagger_version = "2.24"
implementation "com.google.dagger:dagger:$dagger_version"
annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version"
I also have only an AppComponent with the following code:
#Singleton
#Component(
modules = {
AndroidSupportInjectionModule.class,
ActivityBuildersModule.class,
AppModule.class,
ViewModelFactoryModule.class,
}
)
public interface AppComponent extends AndroidInjector<BaseApplication> {
SessionManager sessionManager();
#Component.Builder
interface Builder{
#BindsInstance
Builder application(Application application);
AppComponent build();
}
}
Except that I have a module for each of my activity but I didn't find a way to inject the Activity Context either for AppComponent or from a ActivityModule.
What is the right way to do that?
UPDATE
I finally found the right way to do that.
First I created a module for the class that I want to provide
#Module
public class AlertsModule {
#Provides
static Alerts provideAlerts(Activity activity){
return new Alerts(activity);
}
}
Then I go to the ActivityModules that I want to Inject that custom Class and do a Binding like that
#Module
public abstract class MainActivityModule {
...
#Binds
abstract Activity providesActivity(MainActivity activity);
...
}
And finally I just include the CustomClassModule in my ActivityBuildersModule where I use #ContributesAndroidInjector to provide my Activities.
#MainScope
#ContributesAndroidInjector(
modules = {
AlertsModule.class,
}
)
abstract MainActivity contributeMainActivity();
You can bind an instance of Activity in the same way you're currently binding an instance of Application, either by using an #Component.Builder or an #Component.Factory.
An example implementation would look something like this:
#Subcomponent(...)
interface ActivitySubcomponent {
#Subcomponent.Factory
interface Factory {
ActivitySubcomponent create(#BindsInstance MyActivity activity)
}
}
#Module(subcomponents = [ActivitySubcomponent.class])
class ApplicationModule {
....
}
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
...
((MyApplication) getApplication())
.applicationComponent
.activitySubcomponentFactory()
.create(this)
.inject(this)
}
}
#Module
class MainActivityModule(mActivity: MainActivity) {
var mActivity: MainActivity
init {
this.mActivity= mActivity
}
#Singleton
#Provides
fun getMainActivity(): MainActivity{
return mActivity
}
}
now add this module to your Component class and pass your activity to module constructor while using your component builder hope this may helps you.
#Named annotation helps us to differentiate the Context. We can differentiate the method context() in ActivityModule and ContextModule by adding the #Named annotation as below:
#Module
public class ActivityModule {
private final Context context;
ActivityModule(Activity context){
this.context = context;
}
#Named("activity_context")
#Provides
public Context context(){ return context; }
}
#Module
public class ContextModule {
private final Context context;
ActivityModule(Activity context){
this.context = context;
}
#Named("application_context")
#Provides
public Context context(){ return context.getApplicationContext(); }
Then, we tell Dagger to use the respective Context, as below:
#Module(includes = ContextModule.class)
public class OkHttpClientModule {
....
#Provides
public File file(#Named("application_context") Context context){
File file = new File(context.getCacheDir(), "HttpCache");
file.mkdirs();
return file;
}
....
}
Hope this helps.
I study Dagger 2 and try to understand how I can attach it to an already existing project. Now I faced with the following problem with injectiing dependencies into BroadcastReceivers. I can't use DaggerReceiver because my receivers already extends another class. To use AndroidInjection inside BroadcastReceivers, I should inject DispatchingAndroidInjector to Application class. I don't want to include module with AndroidInjection contributors to main dependency graph so, how I understand, I should place it to dedicated component for Application class, but because of this duplication of Singletons occurs.
Here is my example (GitHub repo)
Classes to provide dependencies to MainActivity and other classes
AppModule.class
#Module
public class AppModule {
Application mApplication;
public AppModule(Application application) {
mApplication = application;
}
#Singleton
#Provides
Application providesApplication() {
return mApplication;
}
}
UtilityModule.class
#Module
public class UtilityModule {
#Singleton
#Provides
FirstUtilityClass providesFirstUtility(Application app) {
return new FirstUtilityClass(app);
}
#Singleton
#Provides
SecondUtilityClass providesSecondUtility(Application app) {
return new SecondUtilityClass(app);
}
}
MainComponent.class
#Singleton
#Component(modules = { AppModule.class, UtilityModule.class })
public interface MainComponent {
void inject(MainActivity activity);
}
Classes to provide DispatchingAndroidInjector to Application class
ReceiversModule.class
#Module
public abstract class ReceiversModule {
#ContributesAndroidInjector
abstract FirstReceiver contributesFirstReceiver();
#ContributesAndroidInjector
abstract SecondReceiver contributesSecondReceiver();
}
ReceiversComponent.class
#Singleton
#Component(modules = { AppModule.class, UtilityModule.class, ReceiversModule.class })
public interface ReceiversComponent {
void inject(MyApplication application);
}
Injecting...
MyApplication.class
public class MyApplication extends Application implements HasBroadcastReceiverInjector {
#Inject DispatchingAndroidInjector<BroadcastReceiver> mInjector;
#Inject FirstUtilityClass mFirstUtility;
#Inject SecondUtilityClass mSecondUtility;
MainComponent mMainComponent;
#Override
public void onCreate() {
AppModule appModule = new AppModule(this);
mMainComponent = DaggerMainComponent.builder().appModule(appModule).build();
// I should build new component only for Application class, because DispatchingAndroidInjector not required for another targets
DaggerReceiversComponent.builder().appModule(appModule).build().inject(this); // Here created new scope
super.onCreate();
// do something
mFirstUtility.doFistThings(getClass().getSimpleName());
mSecondUtility.doSecondThings(getClass().getSimpleName());
}
public MainComponent getMainComponent() {
return mMainComponent;
}
#Override
public AndroidInjector<BroadcastReceiver> broadcastReceiverInjector() {
return mInjector;
}
}
MainActivity.class
public class MainActivity extends AppCompatActivity {
#Inject FirstUtilityClass mFirstUtility;
#Inject SecondUtilityClass mSecondUtility;
#Override
protected void onCreate(Bundle savedInstanceState) {
((MyApplication) getApplication()).getMainComponent().inject(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Do something
mFirstUtility.doFistThings(getClass().getSimpleName());
mSecondUtility.doSecondThings(getClass().getSimpleName());
// Sending broadcasts here
}
}
Logcat output
FirstUtilityClass1: doing first things from MyApplication...
SecondUtilityClass1: doing second things from MyApplication...
FirstUtilityClass2: doing first things from MainActivity...
SecondUtilityClass2: doing second things from MainActivity...
FirstUtilityClass1: doing first things from FirstReceiver...
SecondUtilityClass1: doing second things from SecondReceiver...
How you see in log, Apllication and MainActivity receives different instances of utility classes. I had an idea to make ReceiversComponent as a subcomponent of MainComponent. But ReceiversModule is abstract and I cant add it dinamicaly to existed component instance.
MainComponent.class
#Singleton
#Component(modules = { AppModule.class, UtilityModule.class })
public interface MainComponent {
ReceiversComponent with(ReceiversModule module);
void inject(MainActivity activity);
}
ReceiversComponent.class
#Subcomponent(modules = { ReceiversModule.class })
public interface ReceiversComponent {
void inject(MyApplication application);
}
And then in MyApplication:
// It does not work because I can't create ReceiverModule instance :(
mMainComponent.with(new ReceiversModule()).inject(this);
How can I use the ReceiversComponent as a subcomponent of MainComponent?
It's clear to me that I'm doing something wrong, but I just can't understand where. Maybe there is another way to solve my problem?
I just found the answer!
Since my ReceiversModule class has a default constructor without parameters, I can skip it as parameter when describing the subcomponent in MainComponent interface:
MainComponent.class
#Singleton
#Component(modules = { AppModule.class, UtilityModule.class })
public interface MainComponent {
ReceiversComponent withReceiversModule(); // Here is the reference to the subcomponent with abstract module
void inject(MainActivity activity);
}
MyApplication.class
#Override
public void onCreate() {
AppModule appModule = new AppModule(this);
mMainComponent = DaggerMainComponent.builder().appModule(appModule).build();
mMainComponent.withReceiversModule().inject(this); // It's works!!! :)
super.onCreate();
// do something
mFirstUtility.doFistThings(getClass().getSimpleName());
mSecondUtility.doSecondThings(getClass().getSimpleName());
}
Maybe someone will think it is a trifle, but it took me several days to reach it. I'm very happy, because my code has become a little cleaner. Thanks to all!
I am using dagger for dependency injection, I apologize for not diving into deep and getting my hands on :)
I am using two modules(ApplicationModule, ContactServiceModule), with a component (AppComponent). I have initialized the Dagger in App.java, and I am trying to inject dependencies to a backgroud Service.
Here is the code snippet.
1)ApplicationModule.java
#Module
public class ApplicationModule {
private final App mApp;
private Service contactService;
public ApplicationModule(App app) {
mApp = app;
}
#Provides
#Singleton
public ContactModel contactModel(SQLiteDatabase database) {
return new ContactModel(mApp, database);
}
#Provides
#Singleton
public Context appContext() {
return mApp;
}
#Provides
#Singleton
public ContactController contactController() {
return new ContactController(mApp.getAppComponent());
}
//.....Some more code
}
2)ContactServiceModule.java
#Module
public class ContactServiceModule {
ContactService contactService;
public ContactServiceModule(ContactService contactService){
this.contactService =contactService;
}
#Provides
#Singleton
ContactService provideContactService() {
return this.contactService;
}
}
3)AppComponent.java
#Singleton
#Component(modules = {ApplicationModule.class,ContactServiceModule.class})
public interface AppComponent {
ContactController contactController();
Context appContext();
//...Some code
void inject(ContactController contactController);
//...Some code
void inject(ContactService contactService);
}
4)App.java
public class App extends Application {
private AppComponent mAppComponent;
#Override
public void onCreate() {
super.onCreate();
FlowManager.init(this);
mAppComponent = DaggerAppComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
}
public AppComponent getAppComponent() {
return mAppComponent;
}
}
5) ContactService.java
public class ContactService extends Service {
#Inject
Context appContext;
#Inject
ContactController contactController;
#Override
public void onCreate() {
super.onCreate();
getAppComponent().inject(this);
}
private AppComponent getAppComponent() {
return ((App)getApplication()).getAppComponent();
}
}
It could be an issue of dependencies of UI threads injected into background services. But I do not understand how should I achieve the same dependencies to be injected into services. Or what should be the Ideal way to communicate between application level dependencies and services. I want the service to be running always in the background and listening to various intents and also communicate to other dependencies like eventbus etc.
The error message:
contactServiceModule must be set at DaggerAppComponent$Builder.build(DaggerAppComponent.java)”
means that you have created a module with dependencies in the constructor (ContactServiceModule which has your Service as a parameter in the constructor). It follows that Dagger 2 cannot instantiate that module automatically and it must be instantiated in the builder when you create your component:
mAppComponent = DaggerAppComponent.builder()
.applicationModule(new ApplicationModule(this))
.contactServiceModule(contactService);
.build();
However there is an underlying issue with the architecture you are trying to create. Services, like Activities, are designed to be autonomous and instantiated by the Android OS. Much in the same way you wouldn't directly pass as an Activity as a dependency to another Activity, you shouldn't make a Service a dependency. Instead you need to use Intent, service binding, or EventBus events to communicate with the service. The Android Service documentation covers this and you should read it carefully before proceeding.
I'm trying to follow the same example as in here by creating a Repository pattern with DI.
The problem is that I get the following error:
"Error:(16, 20) error: #mvp.model.di.scope.Local
mvp.model.repository.local.GameLocalDataSource cannot be provided
without an #Provides-annotated method. #mvp.model.di.scope.Local
mvp.model.repository.local.GameLocalDataSource is injected at
mvp.model.repository.GameRepository.(gameLocalDataSource, …)
mvp.model.repository.GameRepository is provided at
mvp.model.di.component.RepositoryComponent.getGameRepository()"
Here's the code related to the app:
public class GameApplication extends Application {
private RepositoryComponent repositoryComponent;
#Override
public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
LeakCanary.install(this);
// Normal app init code...
repositoryComponent = DaggerRepositoryComponent.builder()
.applicationModule(new ApplicationModule((getApplicationContext())))
.build();
}
public RepositoryComponent getRepositoryComponent() {
return repositoryComponent;
}
}
This is my RepositoryComponent:
#Singleton
#Component(modules = {RepositoryModule.class, ApplicationModule.class})
public interface RepositoryComponent {
GameRepository getGameRepository();
}
Here's the RepositoryModule:
#Module
public class RepositoryModule {
#Singleton
#Provides
#Local
GameDataSource provideLocalDataSource(Context context) {
return new GameLocalDataSource(context);
}
#Singleton
#Provides
#Remote
GameDataSource provideRemoteDataSource() {
return new GameRemoteDataSource();
}
}
And finally, the ApplicationModule:
#Module
public final class ApplicationModule {
private Context context;
public ApplicationModule(Context context) {
this.context = context;
}
#Provides
Context providesContext() {
return context;
}
}
Here's most of my GameRepository class:
#Singleton
public class GameRepository implements GameDataSource {
private GameDataSource remoteDataSource;
private GameDataSource localDataSource;
#Inject
public GameRepository(#Local GameLocalDataSource gameLocalDataSource, #Remote GameRemoteDataSource gameRemoteDataSource) {
remoteDataSource = gameRemoteDataSource;
localDataSource = gameLocalDataSource;
}
Also, as in the mentioned example, I created a couple of scopes, #Local and #Remote since my two data sources have the same type and Dagger needs to differentiate them.
#Qualifier
#Documented
#Retention(RetentionPolicy.RUNTIME)
public #interface Local {
}
The rest of the code I have related to dagger, is just the #Inject in the constructors where I want to inject my dependencies.
Also, the DaggerRepositoryComponent is never generated in the GameApplication class.
Thanks a lot for the help in advance!
GameLocalDataSource cannot be provided without an #Provides-annotated method
Somewhere in the code you are trying to #Inject GameLocalDataSource, but you have specified in your module how to provide GameDataSource, not GameLocalDataSource.
...
GameDataSource provideLocalDataSource(Context context) {
return new GameLocalDataSource(context);
}
...
Either ask Dagger to inject GameDataSource, or describe Dagger how to provide GameLocalDataSource.
...
GameLocalDataSource provideLocalDataSource(Context context) {
return new GameLocalDataSource(context);
}
...
I'm following this tutorial to add Dagger 2 to my Android project.
After doing setup and creating the modules and components I can add the dependencies in an Activity like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
ButterKnife.bind(this);
((AppController) getApplication()).getNetComponent().inject(this);
}
I am struggling in how to inject dependencies in a Fragment and IntentService?
public class FragmentBrandList extends ListFragment {
}
In this class which #Override method should I request injection in and what will be the code for this?
In this class which #Override method i should use and what will be the code to add dependency in fragment?
The correct place to call injection inside a Fragment is onAttach(Context context). This is stated in the where to inject section of the Dagger 2 user guide here
#Override
public void onAttach(Context context) {
((AppController) context.getApplicationContext()).getNetComponent().inject(this);
super.onAttach(context);
}
The correct place to call injection inside a Service is onCreate()
#Override
public void onCreate() {
((AppController) getApplication()).getNetComponent().inject(this);
super.onCreate();
}
Note that in both cases the request for injection comes before the call to super.onCreate(). The Dagger user guide explains it like this:
It is crucial to call AndroidInjection.inject() before super.onCreate() in an Activity, since the call to super attaches Fragments from the previous activity instance during configuration change, which in turn injects the Fragments. In order for the Fragment injection to succeed, the Activity must already be injected. For users of ErrorProne, it is a compiler error to call AndroidInjection.inject() after super.onCreate().
In other words:
The Activity super.onCreate() call re-attaches Fragments from a previous instance
This super call in cause Fragments to be re-injected (since Fragments are injected in onAttach)
Fragments should be injected after their Activity is injected, therefore request injection in your Activity before calling super.onCreate().
You can always check where to inject by looking at the relevant source code for the com.google.dagger:dagger-android classes like DaggerFragment and DaggerService. See the GitHub repo here
For your specific example, please make sure you have added the new injection sites to the NetComponent:
void inject(FragmentBrandList frag);
void inject(BrandListService service);
Step 1: Create your ApplicationModule
#Module
public class ApplicationModule {
private final DIApplication application;
public ApplicationModule(DIApplication application) {
this.application = application;
}
#Provides #Singleton
public DIApplication provideApplication() {
return application;
}
#Provides #Singleton
public DogModel provideDogModel() {
return new DogModelImpl("Scooby-doo");
}
}
Step 2: Create your ApplicationComponent:
#Singleton
#Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
void inject(DIApplication application);
void inject(BaseActivity activity);
void inject(BaseFragment fragment);
void inject(DogSyncService service);
}
Step 3: Create a DI Class:
public class DependencyInjector {
private static ApplicationComponent applicationComponent;
public static void initialize(DIApplication diApplication) {
applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(diApplication))
.build();
}
public static ApplicationComponent applicationComponent() {
return applicationComponent;
}
private DependencyInjector(){}
}
Final Step: Inject anywhere using:
DependencyInjector.applicationComponent()
Your question inspired me to create a Demo project that shows Activity, Fragment and Service injection using Dagger2. Here is the git: https://github.com/write2sv/AndroidDIDagger2/tree/master/app/src/main/java/work/shaggy/didemo
I did it using
((AppController) getActivity().getApplication()).getNetComponent().inject(this);
in Fragment.onCreate() method.
((MyApp) context.getApplicationContext()).getApplicationComponent().inject(MyFragment.this);
I added this in onAttach(Context context) method of the fragment.
You really just need to include the injector method for whatever you want to inject.
#Singleton
#Component
public interface AppComponent {
void inject(MainActivity activity);
void inject(FragmentBrandList fragmentBrandList);
}
You can use autodagger2 to avoid having to write all that boilerplate. I use this architecture quite often:
build.gradle
apt 'com.github.lukaspili.autodagger2:autodagger2-compiler:1.1'
compile 'com.github.lukaspili.autodagger2:autodagger2:1.1'
YourApp.java
#AutoComponent( modules = YourApp.YourAppModule.class )
public class YourApp extends Application {
private static YourApp instance;
private YourAppComponent component;
public YourAppComponent getComponent() {
return this.component;
}
#Override
public void onCreate() {
super.onCreate();
setupComponent();
}
private void setupComponent() {
component = DaggerYourAppComponent.builder()
.yourAppModule(new YourAppModule(instance))
.build();
}
#dagger.Module
public static class YourAppModule {
private YourApp app;
YourAppModule(YourAppApp application) {
this.app = application;
}
#Provides #AutoExpose(YourApp.class)
Application provideApplication() {
return app;
}
#Provides #AutoExpose(PoswalaApp.class)
Context provideContext() {
return app;
}
#Provides #AutoExpose(YourApp.class)
Retrofit provideApiAdapter() {
return ApiService.getServiceInstance();
}
}
}
YourActivity.java
#AutoComponent(
dependencies = YourApp.class,
modules = YourActivity.YourActivityModule.class
)
public class YourActivity extends BaseActivity implements YourActivityView {
private YourActivityComponent component;
#Inject MyPresenter presenter
// This is an abstract method from BaseActivity
#Override
protected void setupComponent(YourAppComponent appComponent) {
component = DaggerYourActivityComponent.builder()
.yourAppComponent(((YourApp) getApplication()).getComponent())
.yourActivityModule(new YourctivityModule(this))
.build();
}
#Override
protected MyPresenter getPresenter() {
return presenter;
}
#dagger.Module
public static class YourActivityModule {
private YourActivityView view;
YourActivityModule(YourActivityView view) {
this.view = view;
}
#Provides #AutoExpose(YourActivity.class)
YourActivityView provideView() {
return view;
}
// Your other dependencies
}
}
Quick explanation:
Your app's module will have to a "universal" dependency, but this way you can achieve using several modules for a class. You just need to customize the
#AutoComponent(
dependencies = YourApp.class,
modules = { YourActivity.YourActivityModule.class, YourFragment.YourFragmentModule.class }
)
block. You can add as many modules as you like using that syntax.
Hope this helps you