Dagger 2 component not generated

倾然丶 夕夏残阳落幕 提交于 2019-12-02 17:56:34

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'

Here is the solution for the fresh Dagger project.

These two lines are responsible to generate the compile-time framework in Dagger 2.

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

Full setup Dagger

        //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 need to configure Annotation Process as provided in the screenshot below. You can do this File>Other Settings>Default Settings>search"Annotation processor"

Maybe you forgot to annotate ModuleClass with @Module ?

There are some minor misconceptions/faults in your code above, here's a working implementation:

Application.java:

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

The generated class will be named DaggerComponentClass, not DaggerCompClassComponent. If you can't run your app in Android Studio to get it built, try Build->Clean project and Build->Rebuild project in the menu. If everything is OK Dagger will have compiled DaggerComponentClass which will be located in the same package as ComponentClass.

ComponentClass.java:

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

A Component in Dagger2 has methods named inject that receive the instance to get instances injected into it, not the other way around. In the code above the class AClassThatShouldGetInstancesInjected will typically call componentClass.inject(this); to get instances injected 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.

If you have several modules in your AndroidStudio (modules in terms of Android Studio, not Dagger), another possible reason of fail is that you've forgot to put annotation processors into the all modules' build.gradle.

We've divided our app into several modules, updated dependencies from using implementation to using api but forgot to handle annotation processors accordingly.

So, you can have this lines only in a 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 this ones should be specified in all modules 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'

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

If you are using Kotlin, make sure to add kapt (Kotlin annotation processor) Gradle plugin to your build script and use kapt Gradle dependency type instead of annotationProcessor for Dagger Compiler.

apply plugin: 'kotlin-kapt

kapt deps.daggercompiler
implementation deps.dagger

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);
//add all the dependencies otherwise component class will not be generated.
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.21'
implementation 'com.google.dagger:dagger:2.21'
annotationProcessor 'com.google.dagger:dagger-compiler:2.21'
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!