I have been testing out Dagger 2, and everything had been working, until I did a bit of refactoring. Now gradle is throwing an IllegalArgumentException
, and I canno
I ran into this issue while bringing Firebase into the project. It was the first background service being added to the project so I decided to do some sleuthing with a service that did nothing.
This built:
public class HopefullyBuildsService extends IntentService {
public HopefullyBuildsService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
}
}
..............
@ApplicationScoped
@Component(modules = {ApplicationModule.class, RestModule.class})
public interface ApplicationComponent {
...
void inject(HopefullyBuildsService service);
...
}
But this caused the build to fail:
public class HopefullyBuildsService extends FirebaseMessagingService {
}
..............
@ApplicationScoped
@Component(modules = {ApplicationModule.class, RestModule.class})
public interface ApplicationComponent {
...
void inject(HopefullyBuildsService service);
...
}
For whatever reason trying to inject directly into a Firebase derived service causes the build to fail in the way you described. However indirectly injecting into another class and then instantiating it the old-fashioned way inside the service allowed it to build again.
public class FirebaseDaggerInjectHelper {
@Inject
PersistManager persistManager;
@Inject
RestClient restClient;
@Inject
OtherClasses stuffEtc;
public FirebaseDaggerInjectHelper(MyApplication application){
application.getApplicationComponent().inject(this);
}
//getters and setters n stuff
}
........
@ApplicationScoped
@Component(modules = {ApplicationModule.class, RestModule.class})
public interface ApplicationComponent {
...
void inject(FirebaseDaggerInjectHelper helper);
...
}
........
public class HopefullyBuildsService extends FirebaseMessagingService {
private FirebaseDaggerInjectHelper injectHelper;
@Override
public void onCreate() {
super.onCreate();
injectHelper = new FirebaseDaggerInjectHelper((MyApplication) getApplicationContext());
}
And then it built fine. Admittedly, having this middleman class is annoying and the firebase derived service has to interact with the injected components in an indirect fashion. But its not clear to me why I cannot inject into a Firebase derived service, Or what is special about Firebase that made Dagger2 unhappy.