question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to apply Dagger injection into services?

See original GitHub issue

Fragment & Activity injection is done through registerActivityLifecycleCallbacks and registerFragmentLifecycleCallbacks, but it’s unclear how to turn on injection for Services. Any ideas?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

180reactions
Naucecommented, Dec 16, 2017

Here’s my solution:

Make Application implements HasServiceInjector and inject a DispatchingAndroidInjector for services.

public class App extends Application implements HasActivityInjector, HasServiceInjector {

    @Inject
    DispatchingAndroidInjector<Activity> dispatchingActivityInjector;
    @Inject
    DispatchingAndroidInjector<Service> dispatchingServiceInjector;

    @Override
    public void onCreate() {
        super.onCreate();
        AppInjector.init(this);
    }

    @Override
    public AndroidInjector<Activity> activityInjector() {
        return dispatchingActivityInjector;
    }

    @Override
    public AndroidInjector<Service> serviceInjector() {
        return dispatchingServiceInjector;
    }

}

Create a new module to perform injection over your services.

@Module
abstract class ServiceBuilderModule {

    @ContributesAndroidInjector
    abstract MyService contributeMyService();

}

Register your new module in your application’s component.

@Component(modules = {
        AndroidSupportInjectionModule.class,
        AppModule.class,
        ActivityBuilderModule.class,
        ServiceBuilderModule.class
})
@Singleton
public interface AppComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(App application);

        AppComponent build();
    }

    void inject(App app);

}

And finally, override method onCreate of the service adding AndroidInjection.inject(this).

public class MyService extends Service {

    @Override
    public void onCreate() {
        AndroidInjection.inject(this);
        super.onCreate();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
}
6reactions
pzmudzinskicommented, Dec 16, 2017

Ok, but what about integration tests? It will crash since Github example app is using different Application class which disables Dagger initialization and you are responsible for manually providing mock-objects into properties.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Dagger 2 to inject into service - android - Stack Overflow
2 Answers 2 · Declare a component, · add the inject method to that component, · add a module providing your service ·...
Read more >
Using Dagger in Android apps
To inject an object in the activity, you'd use the appComponent defined in your Application class and call the inject() method, passing in...
Read more >
Using Dagger 2 for dependency injection in Android - Tutorial
Dagger 2 is dependency injection framework. It is based on the Java Specification Request (JSR) 330. It uses code generation and is based...
Read more >
Dependency Injection with Dagger 2 - CodePath Cliffnotes
Instantiating the component ... Make sure to rebuild the project (in Android Studio, select Build > Rebuild Project) if you cannot reference the...
Read more >
Dependency injection with Dagger 2: @Inject and @Provides
The @Inject annotation is concise and easy to use. However, there are cases where this annotation cannot be used: ... In this case,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found