gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:25:25 28 4
gpt4 key购买 nike

我想在项目 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com