Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 component not generated

Tags:

android

dagger

In my module, in my base Application class

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

it can not find DaggerCompClassComponent.

I have on module build.gradle

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 Project build.gradle,

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

I have done build / rebuild / clean / restart project. I have a Component class where I inject objects and a ModuleClass where I provide objects to inject.

What can be the cause for not generating Dagger Component . class ?

EDIT:

This is my ModuleClass, adnotated 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);  } 
like image 776
ghita Avatar asked May 03 '17 09:05

ghita


2 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 start of the same file.

That 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' 
like image 63
Gonzalo Avatar answered Sep 23 '22 00:09

Gonzalo


Update (March 29, 2020)

Inside your app-level build.gradle inside dependencies block, add these lines:

     //dagger2      api 'com.google.dagger:dagger:2.24'      api 'com.google.dagger:dagger-android:2.24'      api 'com.google.dagger:dagger-android-support:2.24'       annotationProcessor 'com.google.dagger:dagger-compiler:2.24'      kapt 'com.google.dagger:dagger-compiler:2.24'       annotationProcessor 'com.google.dagger:dagger-android-processor:2.24'      kapt 'com.google.dagger:dagger-android-processor:2.24'       compileOnly 'javax.annotation:jsr250-api:1.0'      implementation 'javax.inject:javax.inject:1' 

Inside android block of app-level build.gradle,

kapt {         generateStubs = true     } 

At the top of the app-level build.gradle, Do this in exactly below order.

apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android-extensions' 

Finally, You need to configure Annotation Process as provided in the screenshot below. You can do this File>Other Settings>Settings for New Projects>search"Annotation processor" enter image description here

After this, do from Menu Build > Rebuild. You are done!

Test:

@Component public interface ApplicationComponent {  } 

Now, you can use DaggerApplicationComponent that was generated at compile-time for your ApplicationComponent interface.

public class MyApplication extends Application {      ApplicationComponent applicationComponent = DaggerApplicationComponent.create();   } 
like image 21
Uddhav P. Gautam Avatar answered Sep 19 '22 00:09

Uddhav P. Gautam