While trying to integrate latest Dagger 2 version, I am facing problem of Dagger auto generation. Dagger is not auto generating DaggerAppComponent in spite of several Rebuilds a
Adding the below dependency fixed the issue for me for Java project
annotationProcessor 'com.google.dagger:dagger-compiler:2.27'
and for Kotlin project (after adding kotlin-kapt
):
kapt "com.google.dagger:dagger-compiler:2.27"
I had the same problem...What solved my problem was actually adding The view model to the ViewmodelModulle then add the annotation @Inject to the constructor of my view model. It could be a different issue for you but in my situation, this really helped. My code compiled without any problem
@Inject <----- This was Missing in the constructor.
public MessageViewModel(Application application) {
super(application);
mApp = application;
You need these two lines
implementation 'com.google.dagger:dagger:2.16'
kapt 'com.google.dagger:dagger-compiler:2.16'
Use kapt instead of annotationProcessor when using kotlin. Generated class like Dagger+AppComponentClass, eg:DaggerAppComponent
1.Clean the project 2.Rebuild 3.File-->Invalidate and Restart
Chenge code to this,
private void initAppComponent()
{
/* DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();*/
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
appComponent .inject(this)
}
Other things are
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent
{
void inject(BaseApplication application);
}
Why you need to inject same class where a component is built you can easily get context and application on Application class. Dagger can help you to find a dependent class.
Try, go to File and
Invalidate and Restart