In my module, in my base Application class
component = DaggerCompClassComponent.builder()
.classModule(new ModuleClass()).build();
In build.gradle's app, you need to add necessary dependencies for Dagger to generate corresponding classes, as below:
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21' // if you use the support libraries
annotationProcessor 'com.google.dagger:dagger-android-processor:2.21'
annotationProcessor 'com.google.dagger:dagger-compiler:2.21'
you should change dagger version which you are using.
refer full dagger document in this
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'
In my case it wasn't being created only in Instrumentation Testing, because I was missing
kaptAndroidTest "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
Of course I'm using Kotlin and $rootProject.daggerVersion
is a property on my base build file. You can replace for whatever version or gradle dependency you desire and are missing.
I solved it by editing my dagger dependencies to this:
implementation "com.google.dagger:dagger:${versions.dagger}"
kapt "com.google.dagger:dagger-compiler:${versions.dagger}"
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"
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();
}
If Dagger2 can not generate its components it means that your code have some errors with Scopes/Modules. Check our your @Provides/Inject methods.
UPD:
You should inject your components into cases where you need instances of classes provided by module.
like
inject(MainActivity main);