使用annotationProcessor而不是android-apt的Dagger 2与Android Studio 3.0 Preview(Canary 2)

viz*_*izZ 14 android dagger-2 android-apt annotation-processor android-studio-3.0

"A long time ago in a galaxy far, far away...."
Run Code Online (Sandbox Code Playgroud)

好吧,长话短说 - 我决定尝试Android Studio 3.0 Preview (Canary 2)一下,我不能Dagger 2使用annotationProcessor而不是使用它android-apt.

我得到的错误消息是一个简单的消化:

Error:(59, 24) error: cannot find symbol variable DaggerAppComponent
Run Code Online (Sandbox Code Playgroud)

我已经阅读了文档(我想没有什么花哨的):https://developer.android.com/studio/preview/features/new-android-plugin-migration.html#annotationProcessor_config

并将build.gradle文件更改为:

implementation "com.google.dagger:dagger:$rootProject.ext.daggerVersion"
annotationProcessor "com.google.dagger:dagger-android-processor:$rootProject.ext.daggerVersion"
Run Code Online (Sandbox Code Playgroud)

哪里 daggerVersion = '2.11'

此外,我确保在Android Studio中检查了相应的选项(默认情况下未选中):

File -> Other Settings -> Default Settings -> 
Build, Execution, Deployment -> Compiler -> Annotation Processors -> 
Enable annotation processors -> IS CHECKED
Run Code Online (Sandbox Code Playgroud)

不幸的是,它没有帮助.

摇篮:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-milestone-1-all.zip
Run Code Online (Sandbox Code Playgroud)

Gradle的Android插件:

dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0-alpha2'
    ...
}
Run Code Online (Sandbox Code Playgroud)

我该如何使用annotationProcessor而不是android-apt

编辑#1

我已经添加了这些"应该是额外的"依赖项"以防万一"

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'
Run Code Online (Sandbox Code Playgroud)

现在我得到一个关于范围冲突的错误......噢

SomeSubComponent has conflicting scopes:
AppComponent also has @Singleton
Run Code Online (Sandbox Code Playgroud)

我确实将Dagger升级2.6.12.11,所以现在我在发行说明中寻找一些"重大变化":https://github.com/google/dagger/releases

编辑#2

好消息是,第一个"突破性变化"在2.9我的猜测中引入,这是由于"新验证".坏消息是,很可能这个问题已经存在很久了.https://github.com/google/dagger/releases/tag/dagger-2.9

审查(Sub)Components和的结构Scoped Dependencies.

编辑#3

目前,此问题与此问题相关:https://github.com/google/dagger/issues/107

请考虑以下示例:

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是不可能的Dagger 2.9+.在Component必须使用不同的Scope那么Subcomponent秒.像这样:

    @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);
    }
Run Code Online (Sandbox Code Playgroud)

viz*_*izZ -3

  1. 不要成为“自作聪明的人”并坚持遵循文档所说的内容:

    implementation "com.google.dagger:dagger:2.11"
    annotationProcessor "com.google.dagger:dagger-compiler:2.11"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 当迁移Dagger 2.9+使用不同的(Custom)ScopesComponentSubcomponents“单例”时。

请参阅问题的描述以获取更详细的解释。

另外,我认为发行说明2.9可以更明确地说明此更改,不是吗?:/ https://github.com/google/dagger/releases/tag/dagger-2.9