Component Dagger 2 not generated

In my module in my application base class

component = DaggerCompClassComponent.builder() .classModule(new ModuleClass()).build(); 

he cannot find DaggerCompClassComponent.

I have a build.gradle module

 apply plugin: 'com.neenbedankt.android-apt' ......................... apt 'com.google.dagger:dagger-compiler:2.8' compile 'com.google.dagger:dagger:2.8' provided 'javax.annotation:jsr250-api:1.0' 

and in the build.gradle project,

  classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 

I completed the build / rebuild / clean / restart project. I have a Component class where I insert objects and ModuleClass where I provide objects for input.

What could be the reason for not creating the dagger component. class?

EDIT:

This is my ModuleClass added with @Module:

 @Provides @Singleton public Interceptor provideInterceptor() { return new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request.Builder builder = chain.request().newBuilder(); builder.addHeader("AppName-Android", BuildConfig.VERSION_NAME + "-" + BuildConfig.VERSION_CODE) .addHeader("Content-Type", "application/json"); return chain.proceed(builder.build()); } }; } @Provides @Singleton OkHttpClient provideOkHttpClient(Interceptor interceptor) { OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.interceptors().add(interceptor); return builder.build(); } @Provides @Singleton Retrofit provideRetrofit(OkHttpClient client) { return new Retrofit.Builder() .baseUrl(BaseApplication.getRes().getString(R.string.api_base_url)) .addConverterFactory(GsonConverterFactory.create()) .client(client) .build(); } @Provides @Singleton WebServiceCall provideWebService(Retrofit retrofit) { return retrofit.create(WebServiceCall.class); } 

And this is my component class:

 @Component(modules = ModuleClass.class) @Singleton public interface ComponentClass { void inject(Interceptor o); void inject(OkHttpClient o); void inject(Retrofit o); void inject(WebServiceCall o); } 
+23
android dagger
source share
6 answers

When developing on Kotlin, you should add the following lines next to their annotationProcessor counterparts:

 kapt 'com.google.dagger:dagger-android-processor:2.15' kapt 'com.google.dagger:dagger-compiler:2.15' 

and add apply plugin: 'kotlin-kapt' at the beginning of the same file.

This section looks like this for me:

 apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' // <- Add this line apply plugin: 'io.fabric' 
+41
source share

Here is the solution for the fresh Dagger project.

These two lines are responsible for creating the compile-time structure in Dagger 2.

compile 'com.google.dagger:dagger:2.14.1' // generates a framework at compile time annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1' // generates a framework at compile time based on the annotations you provided

Complete Dagger Setup

  //dagger 2 compile 'com.google.dagger:dagger:2.14.1' annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1' //to enable DaggerActivity, DaggerBroadcastReceiver, DaggerFragment etc classes compile 'com.google.dagger:dagger-android:2.14.1' annotationProcessor 'com.google.dagger:dagger-android-processor:2.14.1' //support libraries with dagger 2 compile 'com.google.dagger:dagger-android-support:2.14.1' 

Note You must configure the annotation process as shown in the screenshot below. You can do this File>Other Settings>Default Settings> search for "Annotation processor" enter image description here

+21
source share

Did you forget to annotate the ClassClass module with @Module?

+6
source share

There are some minor misunderstandings / errors in the above code, here's a working implementation:

Application.java:

 component = DaggerComponentClass.builder().classModule(new ModuleClass()).build(); 

The generated class will be called DaggerComponentClass , not DaggerCompClassComponent . If you cannot start the application in Android Studio to create it, try to execute the Build-> Clean project and Build-> Rebuild project in the menu. If everything is in order, the dagger will be compiled by the DaggerComponentClass , which will be located in the same package as the ComponentClass .

ComponentClass.java:

 @Component(modules = ModuleClass.class) public interface ComponentClass { void inject(AClassThatShouldGetInstancesInjected instance); } 

A component in Dagger2 has methods called inject , which get an instance for inject instances into it, and not vice versa. In the above code, the AClassThatShouldGetInstancesInjected class usually calls componentClass.inject(this); to get instances introduced into itself.

ModuleClass.java:

 @Module public class ModuleClass { @Provides @Singleton public Interceptor provideInterceptor() {/*code*/} //Your Providers... } 

The module is correct in your code, make sure its annotated.

+3
source share

If you have multiple modules in AndroidStudio (modules in terms of Android Studio, not Dagger), another possible reason for the failure is that you forgot to put annotation processors in the build.gradle all modules.

We divided our application into several modules, updated the implementation dependencies to use api but forgot to process annotation processors accordingly.

Thus, you can only have these lines in the root module:

 api 'com.google.dagger:dagger-android:2.16' // if you use the support libraries api 'com.google.dagger:dagger-android-support:2.16' 

But these must be specified in all module dependencies:

 annotationProcessor 'com.google.dagger:dagger-compiler:2.16' // if you use injections to Android classes annotationProcessor 'com.google.dagger:dagger-android-processor:2.16' 
+1
source share

If Dagger2 cannot generate its components, it means that your code has some errors using Scopes / Modules. Check out our @ Provides / Inject methods.

UPD:

You should embed your components when you need class instances provided by the module.

like

 inject(MainActivity main); 
0
source share

All Articles