Can I just inject super class when use dagger2 for dependency injection?

I encountered the same situation. One way to ease a bit the injection from a common component in all Activities is the following: 1) Extend the Application class to be able to create the common component and keep a reference to it. public class ApplicationDagger extends Application { private ApplicationComponent component; @Override public void onCreate(){ … Read more

Android lifecycle library ViewModel using dagger 2

You need to implement your own ViewModelProvider.Factory. There is an example app created by Google demonstrating how to connect Dagger 2 with ViewModels. LINK. You need those 5 things: In ViewModel: @Inject public UserViewModel(UserRepository userRepository, RepoRepository repoRepository) { Define annotation: @Documented @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @MapKey @interface ViewModelKey { Class<? extends ViewModel> value(); } In ViewModelModule: @Module … Read more

Problems with singletons when using component dependencies

You should put @Singletonto CComponent class declaration. @Singleton @Component(modules = CCModule.class) public interface CComponent { XXX getXXX(); } Explanation is in error message: CComponent is unscoped, @Singleton is a scope. Dagger 2 does not allow unscoped components to use modules with scoped bindings. However, now you will get the following error: AComponent (unscoped) cannot depend … Read more

Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?

I just hit this problem this morning. Do you have anything in your build.gradle that adds arguments to the annotationProcessOptions? For example: android { … defaultConfig { … javaCompileOptions { annotationProcessorOptions { arguments = [“room.schemaLocation”: “$projectDir/schemas”.toString()] } } } } If so, try changing from “arguments =” to “arguments +=”, as just using equals overwrites … Read more

Dagger 2 injecting Android Application Context

@Module public class MainActivityModule { private final Context context; public MainActivityModule (Context context) { this.context = context; } @Provides //scope is not necessary for parameters stored within the module public Context context() { return context; } } @Component(modules={MainActivityModule.class}) @Singleton public interface MainActivityComponent { Context context(); void inject(MainActivity mainActivity); } And then MainActivityComponent mainActivityComponent = DaggerMainActivityComponent.builder() … Read more

Dagger 2 – what is the purpose of a @Singleton annotation class

@Singleton (and any other scope annotation) makes your class a single instance in your dependencies graph (it means that this instance will be “singleton” as long as Component object exists). In short – everytime you’re injecting @Singleton annotated class (with @Inject annotation) it will be the same instance as long as you inject it from … Read more