Dagger 2 with Android Studio 3.0 Preview (Canary 2) using annotationProcessor instead of android-apt

2.8k Views Asked by At
"A long time ago in a galaxy far, far away...."

Ok, long story short - I decided to give Android Studio 3.0 Preview (Canary 2) a shot and I cannot make it work with Dagger 2 using annotationProcessor instead of android-apt.

The error message I get is a simple one to digest:

Error:(59, 24) error: cannot find symbol variable DaggerAppComponent

I've read the docs (nothing fancy there, I guess): https://developer.android.com/studio/preview/features/new-android-plugin-migration.html#annotationProcessor_config

And changed the build.gradle file to:

implementation "com.google.dagger:dagger:$rootProject.ext.daggerVersion"
annotationProcessor "com.google.dagger:dagger-android-processor:$rootProject.ext.daggerVersion"

Where daggerVersion = '2.11'

Also, I made sure that appropriate options are checked in Android Studio (were not checked by default):

File -> Other Settings -> Default Settings -> 
Build, Execution, Deployment -> Compiler -> Annotation Processors -> 
Enable annotation processors -> IS CHECKED

Unfortunately, it does not help.

Gradle:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-milestone-1-all.zip

Android Plugin for Gradle:

dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0-alpha2'
    ...
}

How do I make it work with annotationProcessor instead of android-apt?

EDIT #1

I've added these "supposed-to-be-extra-ones" dependencies "just in case of"

implementation "com.google.dagger:dagger:$rootProject.ext.daggerVersion"
implementation "com.google.dagger:dagger-android:$rootProject.ext.daggerVersion"
implementation "com.google.dagger:dagger-android-support:$rootProject.ext.daggerVersion"
annotationProcessor "com.google.dagger:dagger-android-processor:$rootProject.ext.daggerVersion"
annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
compileOnly 'javax.annotation:jsr250-api:1.0'

And now I get an error about conflicting scopes... Oo

SomeSubComponent has conflicting scopes:
AppComponent also has @Singleton

I did upgrade Dagger from 2.6.1 to 2.11, so now I'm looking for some "breaking changes" in Release Notes: https://github.com/google/dagger/releases

Edit #2

The good news is that the first "breaking change" was introduced in 2.9 My wild guess is that this is due to "New Validation". The bad news is that, most probably, the problem was already there for ages. https://github.com/google/dagger/releases/tag/dagger-2.9

Reviewing structure of (Sub)Components and Scoped Dependencies.

Edit #3

Currently, this issue is related to this one: https://github.com/google/dagger/issues/107

Consider the following example:

@Singleton
@Component(modules = {
        AppModule.class
})
public interface AppComponent {
    SomeComponent plus(SomeModule someModule);
}

@Module
public class AppModule {

    @Provides
    @Singleton
    public Integer provideInteger() {
        return 1;
    }
}

@Singleton
@Subcomponent(modules = {
        SomeModule.class
})
public interface SomeComponent {
    void inject(MainActivity activity);
}

@Module
public class SomeModule {

    @Provides
    @Singleton
    public String provideText(Integer number) {
        return number.toString();
    }
}

This is no longer possible with Dagger 2.9+. The Component has to use a different Scope then the Subcomponents. Like this:

    @Scope
    public @interface ApplicationScope {
    }

    @Module
    public class AppModule {

        @Provides
        @ApplicationScope
        public Integer provideInteger() {
            return -1;
        }
    }

    @ApplicationScope
    @Component(modules = {
            AppModule.class
    })
    public interface AppComponent {
        SomeComponent plus(SomeModule someModule);
    }
1

There are 1 best solutions below

0
On
  1. Don't be a "smartass" and stick to what the docs say:

    implementation "com.google.dagger:dagger:2.11"
    annotationProcessor "com.google.dagger:dagger-compiler:2.11"
    
  2. When migrating to Dagger 2.9+ use different (Custom)Scopes for Components and Subcomponents "singletons".

See the Question's description for more verbose explanation.

Also, I think that the Release Notes for 2.9 could be more explicit about this change, no? :/ https://github.com/google/dagger/releases/tag/dagger-2.9