DaggerAppComponent not created

33,383

Solution 1

With the dependencies listed below everything works:

If you are using Kotlin

apply plugin: 'kotlin-kapt'

dependencies {
    def daggerVer = 2.27 // or latest version

    implementation "com.google.dagger:dagger:$daggerVer"
    implementation "com.google.dagger:dagger-android-support:$daggerVer"
    kapt "com.google.dagger:dagger-android-processor:$daggerVer"
    kapt "com.google.dagger:dagger-compiler:$daggerVer"
}

If you are using Java:

dependencies {
    def daggerVer = 2.27 // or latest version

    implementation "com.google.dagger:dagger:$daggerVer"
    implementation "com.google.dagger:dagger-android-support:$daggerVer"
    annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVer"
    annotationProcessor "com.google.dagger:dagger-compiler:$daggerVer"
}

See google tutorial

You can find the latest release number here.

Solution 2

In Kotlin, we have to add kapt compiler plugin to use Dagger 2.

In your app gradle, add this plugin

apply plugin: 'kotlin-kapt'

And add dependencies as below

dependencies
{
    implementation "com.google.dagger:dagger:$latest_version"
    kapt  "com.google.dagger:dagger-compiler:$latest_version"
    implementation "com.google.dagger:dagger-android:$latest_version"
    kapt  "com.google.dagger:dagger-android-processor:$latest_version"
    implementation "com.google.dagger:dagger-android-support:$latest_version"
    kapt  "com.google.dagger:dagger-android-support:2.12"
}

See Kotlin Documentation

Solution 3

in my case (currently using kotlin)

i use this build gradle

  implementation 'com.google.dagger:dagger:2.24'
  annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
  annotationProcessor 'com.google.dagger:dagger-android-processor:2.24'
  implementation 'com.google.dagger:dagger-android-support:2.24'

after sync gradle,

  1. I quit Android studio (Command + Q).

  2. Delete folder "build" inside 'app' folder

  3. reopen Android Studio

DaggerAppComponent created.

Share:
33,383
kingston
Author by

kingston

I'm not an Autobiographer

Updated on May 18, 2021

Comments

  • kingston
    kingston about 3 years

    With dagger 2.10 I used to be able to create the app component by doing

        sAppComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .sessionModule(new SessionModule())
                .netModule(new NetModule())
                .dataModule(new DataModule())
                .build();
    

    I was already using the AndroidInjector for Activities and everything was fine. Now I switched to 2.11 and I can't find the way to create the app component. In the google tutorial I see:

    DaggerYourApplicationComponent.create()
        .inject(this);
    

    to be added in the onCreate of the Application. In my case DaggerYourApplicationComponent = DaggerAppComponent. The problem is that DaggerAppComponent class isn't created anymore.

    I have:

    public class App extends android.support.multidex.MultiDexApplication implements HasActivityInjector {
        @Inject DispatchingAndroidInjector<Activity> mDispatchingActivityInjector;
        @Override
        public void onCreate() {
            super.onCreate();
    
            sAppComponent = DaggerAppComponent.create().inject(this); //here the error
    

    and:

    @Singleton
    @Component(modules = {
            AppModule.class,
            MainActivityModule.class,
            ...
    })
    public interface AppComponent {
            void inject(App app);
            ...
    }
    

    in the build.gradle file I have:

    def daggerVer = 2.11
    compile "com.google.dagger:dagger:$daggerVer"
    compile "com.google.dagger:dagger-android-support:$daggerVer"
    annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVer"
    
    • OneCricketeer
      OneCricketeer about 7 years
      In the manifest, did you register the application class?
    • kingston
      kingston about 7 years
      Yes and the app is working fine with version 2.10
    • azizbekian
      azizbekian about 7 years
      Instead try DaggerAppComponent.builder().create(this).inject(this).
    • kingston
      kingston about 7 years
      @azizbekian DaggerAppComponent does not exist at all
    • Jeff Bowman
      Jeff Bowman about 7 years
      @kingston This tends to indicate that Dagger failed to generate the file, and has printed the reason for the failure into your compiler output. Can you find that and paste that in here?
    • kingston
      kingston about 7 years
      @JeffBowman I see only the class not found error and if I remove the line sAppComponent = DaggerAppComponent.create().inject(this); the build succeeds with no warnings but of course the AppComponent instance does not exists.
    • kingston
      kingston about 7 years
      I'll try to create a small project to be shared
  • JPM
    JPM over 6 years
    Did this and refuses to see it. clean, cleanBuildCache, nothing is working.
  • kingston
    kingston over 6 years
    @JPM are you using kotlin?
  • JPM
    JPM over 6 years
    yes using dagger. Turns out it was due to Inject on fields still showing fields as private. I added JvmField annotation and that went away then.
  • Jack Guo
    Jack Guo about 6 years
    Solved my problem. Use kapt instead of annotationProcessor as suggested on Dagger 2 GitHub page.
  • Denny
    Denny almost 6 years
    Had this thing disturbing me for 24 hours. I'm developing my app in both Kotlin and Java. When I added 'kapt' next to the annotationProcssors counterparts and it worked.
  • kingston
    kingston almost 6 years
    @Denny you should not need to add both even if you are using java and kotlin. kapt should be enough
  • kingston
    kingston almost 6 years
    @Denny what do you mean? Which one would you exclude? I have replaced annotationProcessor with kapt
  • Ashok Reddy Narra
    Ashok Reddy Narra over 5 years
    After adding above, make sure to have internet permission in manifest file, otherwise it won't generate stub classes <uses-permission android:name="android.permission.INTERNET"></uses-permission‌​>