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

标签 android dagger-2 android-studio-3.0 annotation-processor android-apt

"A long time ago in a galaxy far, far away...."

好吧,长话短说 - 我决定试一试 Android Studio 3.0 Preview (Canary 2),但我无法使用 让它与 Dagger 2 一起工作annotationProcessor 而不是 android-apt

我收到的错误消息很容易消化:

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

我已经阅读了文档(我猜那里没什么特别的):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"

哪里 daggerVersion = '2.11'

此外,我确保在 Android Studio 中选中了适当的选项(默认情况下未选中):

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

不幸的是,它没有帮助。

Gradle :

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

适用于 Gradle 的 Android 插件:

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

如何让它与 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'

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

SomeSubComponent has conflicting scopes:
AppComponent also has @Singleton

我确实将 Dagger 从 2.6.1 升级到 2.11,所以现在我正在发行说明中寻找一些“重大更改”:https://github.com/google/dagger/releases

编辑 #2

好消息是第一个“重大变更”是在 2.9 中引入的。我猜测这是由于“新验证”。坏消息是,这个问题很可能已经存在多年了。 https://github.com/google/dagger/releases/tag/dagger-2.9

审查(Sub)ComponentsScoped 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();
    }
}

Dagger 2.9+ 不再可能。 Component 必须使用与 Subcomponent 不同的 Scope。像这样:

    @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. 不要做一个“聪明人”并坚持文档所说的:

    implementation "com.google.dagger:dagger:2.11"
    annotationProcessor "com.google.dagger:dagger-compiler:2.11"
    
  2. 迁移到 Dagger 2.9+ 时使用不同的 (Custom)Scope s Component s 和 Subcomponent s “单例”。

有关更详细的解释,请参阅问题的描述。

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

关于android - 使用 annotationProcessor 而不是 android-apt 的带有 Android Studio 3.0 Preview (Canary 2) 的 Dagger 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44282157/

相关文章:

java - 在 Android Java 中通过主键获取 Realm 对象的正确方法

android - 使用 Dagger 2 将属性注入(inject) ViewModel

android - 如何在Android项目中从头开始设置DAGGER依赖注入(inject)?

android-gradle-plugin - 合并 Android 单元测试和连接测试代码覆盖率数据已损坏

android - 具有圆形内边缘的方形布局边框

android - 膨胀 ActionBarSherlock 菜单在 XML 中定义

android - : Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension; 解析失败

android - 在 Android Studio 项目上找不到参数的方法 test()

android - 在我的应用程序中显示马拉雅拉姆语文本

android - 如何让 Dagger 1 和 Dagger 2 在一个 Android 项目中共存?