7

I'm using the latest beta of android studio 3 (currently beta 4), and I can't seem to get it to generate the dagger classes needed.

From my side I created an empty project. Then I renamed the activity to match the dagger notes, YourActivity. See "Injecting Activity objects" in https://google.github.io/dagger/android.html, which shows this:

@Subcomponent(modules = ...)
public interface YourActivitySubcomponent extends AndroidInjector<YourActivity> {
  @Subcomponent.Builder
  public abstract class Builder extends AndroidInjector.Builder<YourActivity> {}
}

@Module(subcomponents = YourActivitySubcomponent.class)
abstract class YourActivityModule {
  @Binds
  @IntoMap
  @ActivityKey(YourActivity.class)
  abstract AndroidInjector.Factory<? extends Activity>
  bindYourActivityInjectorFactory(YourActivitySubcomponent.Builder builder);
}

@Component(modules = {..., YourActivityModule.class})
interface YourApplicationComponent {}

@ActivityScope
@ContributesAndroidInjector(modules = { /* modules to install into the subcomponent */ })
abstract YourActivity contributeYourActivityInjector();

public class YourApplication extends Application implements HasActivityInjector {
  @Inject DispatchingAndroidInjector<Activity> dispatchingActivityInjector;

  @Override
  public void onCreate() {
    super.onCreate();
    DaggerYourApplicationComponent.create()
        .inject(this);
  }

  @Override
  public AndroidInjector<Activity> activityInjector() {
    return dispatchingActivityInjector;
  }
}

//the renamed activity
public class YourActivity extends Activity {

  public void onCreate(Bundle savedInstanceState) {
    //added this line
    AndroidInjection.inject(this);

    super.onCreate(savedInstanceState);
  }
}

I've also added the following from that page for the gradle build app file:

dependencies {
  compile 'com.google.dagger:dagger-android:2.11'
  compile 'com.google.dagger:dagger-android-support:2.11' // if you use the support libraries
  annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
}

because I was still not seeing anything I tried updating compile to implementation with still no luck, and if you're curious, that looks like:

dependencies {
  implementation 'com.google.dagger:dagger-android:2.11'
  implementation 'com.google.dagger:dagger-android-support:2.11' // if you use the support libraries
  annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
}

Also, I've found the default setting where I can turn on the default Build -> Compiler -> Annotation Processors to Enable it. I did this before creating the new project.

After all of this, nothing seems to work. Is this broken in android studio 3.x? If not how did you get it work?

Thanks, Kelly

KellyTheDev
  • 891
  • 2
  • 12
  • 31

9 Answers9

11

Ahhh ha! I discovered the issue. It appears I needed another annotationProcessor line for the gradle app file

annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

While their example doesn't quite work right, at least I am now seeing the DaggerYourApplicationComponent

the full gradle portion:

dependencies {
  compile 'com.google.dagger:dagger-android:2.11'
  compile 'com.google.dagger:dagger-android-support:2.11' // if you use the support libraries
  annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
  annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
}

Note: I haven't tried replacing compile with implementation, but I'm just glad this worked.

Hopefully, this helps others too

KellyTheDev
  • 891
  • 2
  • 12
  • 31
  • This solved my problem as well, their example showed only the first 3 dependencies...adding this 4th resolved my problem. – szaske Mar 14 '18 at 21:40
3

Dagger like compile-time dependency injection tools need once run application. Then it will create automatically if there is no error.

mbakgun
  • 75
  • 5
  • 1
    When I try to build, each time it errors because it cannot find `DaggerYourApplicationComponent`. And if comment that code out then compile. It builds. After which I'll comment back in the `DaggerYourApplicationComponent` line to see it fail to find it and fail to build. – KellyTheDev Sep 05 '17 at 14:39
  • After cleaning project files my ApplicationComponent also deleting and giving error. But when all my providers and others are corrent , it will creating after first build – mbakgun Sep 05 '17 at 15:18
  • 1
    That's what I found too @mbakgun – KellyTheDev Sep 26 '17 at 17:15
3

The detailed solution is here:

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


}
Uddhav P. Gautam
  • 7,362
  • 3
  • 47
  • 64
1

May be it is too late. But implementation also work on dagger 2.11

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

I have made a simple hello world project to implement dagger 2.11 using Android Studio 3.0 beta-2. Hope this might help the beginners (like me) to have a good start since google documentation does not provide a clear instructions on it

MyDaggerExample

Long Ranger
  • 5,888
  • 8
  • 43
  • 72
1

I started seeing problems when I upgraded to gradle 3.0.0 as well. For me the solution was to change the implementation dagger lines to api:

api 'com.google.dagger:dagger-android:2.11'
David Berry
  • 40,941
  • 12
  • 84
  • 95
1

Changing implementation & annotationProcessor to kapt might help you.

@Singleton
@Component(modules = [AndroidSupportInjectionModule::class])
interface AppComponent : AndroidInjector<BaseApplication?> {
@Component.Builder
interface Builder {
    @BindsInstance
    fun application(application: Application?): Builder?
    fun build(): AppComponent?
    }
}

And from your application class

class BaseApplication : DaggerApplication() {
override fun applicationInjector(): AppComponent? {
    return DaggerAppComponent.builder().application(this)?.build()
    //        return null;
   }

}

Also use change

implementation '*emphasized text*com.google.dagger:dagger:2.15'
annotationProcessor 'com.google.dagger:dagger-compiler:2.15'

to

kapt  'com.google.dagger:dagger:2.15'
kapt  'com.google.dagger:dagger-compiler:2.15'

Thank you.

Geeta Gupta
  • 1,622
  • 11
  • 17
0

add dependency in gradle for dagger all 5 library given below

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

and also add repositories mavenCenteral()

allprojects {
repositories {
    google()
    jcenter()
    mavenCentral()
MohammedAli
  • 2,361
  • 2
  • 19
  • 36
0

You might have forgot to add apply plugin: 'kotlin-kapt' to the app level build.gradle

// dependency injection
implementation "com.google.dagger:dagger:$dagger2_version"
kapt "com.google.dagger:dagger-compiler:$dagger2_version"
kapt "com.google.dagger:dagger-android-processor:$dagger2_version"
annotationProcessor "com.google.dagger:dagger-compiler:$dagger2_version"
Nilesh Deokar
  • 2,975
  • 30
  • 53
-4

You just need to run "Rebuild" of the project and fix all compile errors if any.

i_tanova
  • 667
  • 4
  • 5