Dagger 2 component not generated

前端 未结 12 1560
走了就别回头了
走了就别回头了 2021-01-31 07:21

In my module, in my base Application class

component = DaggerCompClassComponent.builder()
                .classModule(new ModuleClass()).build();
12条回答
  •  迷失自我
    2021-01-31 08:22

    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"

    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();
    
    
    }
    

提交回复
热议问题