Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Dagger] integration with workers (For Java People)
public class AndroidWorkerInjection {
public static void inject(Worker worker) {
checkNotNull(worker, "worker");
Object application = worker.getApplicationContext();
if (!(application instanceof HasWorkerInjector)) {
throw new RuntimeException(
String.format(
"%s does not implement %s",
application.getClass().getCanonicalName(),
HasWorkerInjector.class.getCanonicalName()));
}
AndroidInjector<Worker> workerInjector =
((HasWorkerInjector) application).workerInjector();
checkNotNull(workerInjector, "%s.workerInjector() returned null", application.getClass());
workerInjector.inject(worker);
}
}
@Module
public abstract class AndroidWorkerInjectionModule {
@Multibinds
abstract Map<Class<? extends Worker>, AndroidInjector.Factory<? extends Worker>>
workerInjectorFactories();
}
@Singleton
@Component(modules = {
AndroidSupportInjectionModule.class,
AndroidWorkerInjectionModule.class,
WorkerModule.class})
public interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(MyApplication application);
AppComponent build();
}
void inject(MyApplication app);
}
public interface HasWorkerInjector {
AndroidInjector<Worker> workerInjector();
}
public class MyApplication extends MultiDexApplication implements HasWorkerInjector {
@Inject DispatchingAndroidInjector<Worker> workerDispatchingAndroidInjector;
public AppComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
MultiDex.install(this);
appComponent = DaggerAppComponent
.builder()
.application(this)
.build();
appComponent.inject(this);
//Call the Profile Worker
OneTimeWorkRequest refreshProfile =
new OneTimeWorkRequest.Builder(ProfileWorker.class)
.addTag(ConstantsUtils.TAG_OUTPUT)
.build();
WorkManager.getInstance().enqueue(refreshProfile);
}
@Override
public AndroidInjector<Activity> activityInjector() {
return dispatchingAndroidInjector;
}
@Override
public AndroidInjector<Worker> workerInjector() {
return workerDispatchingAndroidInjector;
}
}
public class ProfileWorker extends Worker {
@Inject
SomeClass clazz;
@NonNull
@Override
public WorkerResult doWork() {
AndroidWorkerInjection.inject(this);
// Do work here :
return WorkerResult.SUCCESS;
}
}
@Subcomponent
public interface ProfileWorkerModule extends AndroidInjector<ProfileWorker> {
@Subcomponent.Builder
abstract class Builder extends AndroidInjector.Builder<ProfileWorker>{}
}
@Module(subcomponents = {
ProfileWorkerModule.class,
UploadWorkerModule.class
})
public abstract class WorkerModule {
@Binds
@IntoMap
@WorkerKey(ProfileWorker.class)
abstract AndroidInjector.Factory<? extends Worker> bindProfileWorkerFactory(ProfileWorkerModule.Builder profileWorker);
}
@usmanrana07

This comment has been minimized.

usmanrana07 commented Aug 7, 2018

Hi!
I'm getting exception: java.lang.IllegalArgumentException: No injector factory bound for Class. What's missing?

@ThePredators

This comment has been minimized.

Owner

ThePredators commented Aug 27, 2018

@usmanrana07:
Actually nothing i have filled all what you need to declare !

Can you put the stack trace please ?

@ToniNgethe

This comment has been minimized.

ToniNgethe commented Sep 11, 2018

where is the code for @WorkerKey?

@ThePredators

This comment has been minimized.

Owner

ThePredators commented Sep 17, 2018

@ToniNgethe

It's in WorkerModule.java !

@usmanrana07

This comment has been minimized.

usmanrana07 commented Oct 15, 2018

@usmanrana07:
Actually nothing i have filled all what you need to declare !

Can you put the stack trace please ?

Here is stackTrace:

E/DefaultWorkerFactory: Could not instantiate com.fayvo.workers.LikeActionsSyncManager
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at androidx.work.DefaultWorkerFactory.createWorker(DefaultWorkerFactory.java:58)
at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:180)
at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:117)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at com.fayvo.di.worker.AndroidWorkerInjection.inject(AndroidWorkerInjection.java:17)

@ThePredators

This comment has been minimized.

Owner

ThePredators commented Oct 31, 2018

@usmanrana07:
Actually nothing i have filled all what you need to declare !
Can you put the stack trace please ?

Here is stackTrace:

E/DefaultWorkerFactory: Could not instantiate com.fayvo.workers.LikeActionsSyncManager
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at androidx.work.DefaultWorkerFactory.createWorker(DefaultWorkerFactory.java:58)
at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:180)
at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:117)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at com.fayvo.di.worker.AndroidWorkerInjection.inject(AndroidWorkerInjection.java:17)

Send me please the hole stack trace so i can help you fastly, here is my email : predator-gist-0001@yopmail.com.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment