java - 在没有 Android 的情况下使用 Dagger2

标签 java intellij-idea dependency-injection dagger-2

我想在项目 Gradle 中使用 Dagger2 和 MVP,但不使用 Android,而是使用 native Java。 但我无法构建我的项目,DaggerAppComponent 类永远不会生成。 该类由 Dagger lib 在编译时自动生成。

构建.gradle:

plugins {
    id "net.ltgt.apt" version "0.11"
}
apply plugin: 'java'
apply plugin: 'idea'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

compileJava {
    options.annotationProcessorPath = configurations.apt
}

configurations {
    apt
}


dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compile "com.google.dagger:dagger:2.11"
    apt     "com.google.dagger:dagger-compiler:2.11"
    apt     "com.google.dagger:dagger-producers:2.11"


    compileOnly "com.google.auto.factory:auto-factory:1.0-beta3"
    apt         "com.google.auto.factory:auto-factory:1.0-beta3"

    compileOnly "org.immutables:value:2.2.10:annotations"
    apt         "org.immutables:value:2.2.10"


    provided 'javax.annotation:jsr250-api:1.0'
    compile 'org.glassfish:javax.annotation:10.0-b28'

}

Main.java

public class Main {

private static AppComponent appComponent;

    public static void main(String[] args) {

        /**
         * Launch the application.
         */
        EventQueue.invokeLater(new Runnable() {
            public void run() {

                appComponent = initDagger();

                try {
                    MainViewImpl mainView = new MainViewImpl();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public AppComponent getAppComponent() {
        return appComponent;
    }

    public static AppComponent initDagger() {
        return DaggerAppComponent.builder().appModule(new AppModule())
                .build();
    }
}

当我构建项目时,出现此错误:

Error:(38, 16) java: cannot find symbol
  symbol:   variable DaggerAppComponent
  location: class main.Main

DaggerAppComponent 类从未构建。 你有什么想法吗?

最佳答案

您需要遵循 Gradle 注释处理插件的 IDE 说明 https://github.com/tbroyer/gradle-apt-plugin

像 Dagger2 和 Lombok 这样的注释需要一个注释处理器。这可以通过配置 IDE 来完成,但最好尽可能由 gradle 来处理。

话虽这么说,您仍然需要为 Eclipse 或 IntelliJ 做一些事情。 对于 IntelliJ 来说,这是重要的部分:

When using the Gradle integration in IntelliJ IDEA (rather than the ida task), it is recommended to delegate the IDE build actions to Gradle itself starting with IDEA 2016.3: https://www.jetbrains.com/idea/whatsnew/#v2016-3-gradle Otherwise, you'll have to manually enable annotation processing: in Settings… → Build, Execution, Deployment → Compiler → Annotation Processors, check Enable annotation processing and Obtain processors from project classpath. To mimic the Gradle behavior and generated files behavior, you can configure the production and test sources directories to build/generated/source/apt/main and build/generated/source/apt/test respectively and choose to Store generated sources relative to: Module content root.

在启动器的“项目默认值”中进行配置,这样您只需执行一次。

Note that starting with IntelliJ IDEA 2016.1, and unless you delegate build actions to Gradle, you'll have to uncheck Create separate module per source set when importing the project.

关于java - 在没有 Android 的情况下使用 Dagger2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46074673/

相关文章:

java - 如何从连接mysql中的两个表中选择特定范围的值?

java - 尽管通过 @Test(testName = "sth") 设置测试名称,ITestResult getTestName() 返回 null

intellij-idea - 为什么默认的 IntelliJ 默认类 javadoc 注释使用非标准语法?

ios - 台风与单例 AppDelegate

java - 所有文件均不打印

java - 如何让.properties从Jenkins读取参数

maven - 从 IntelliJ 调用 maven 时如何解决缺少的 'javadoc' 命令问题?

java - Intellij 中的 Visual Studio Ctrl+ 逗号等效项

c# - MVC3 IControllerFactory ReleaseController内存泄漏

java - Guice 字段注入(inject)不起作用(返回 null)