我想在项目Gradle中使用Dagger2和MVP,但不使用Android,使用本地Java。 但我无法构建我的项目,DaggerAppComponent类永远不会生成。 这个类是由Dagger lib在编译时自动生成的。使用Dagger2而不使用Android
的build.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永远不会建立。 你有想法吗?
你有没有''导入''班? – f1sh
匕首编译器依赖范围不应该是“compileOnly”吗?我甚至不确定'apt'类型适用于非android项目。 – yegodm
哪些类? 问题是我无法生成类DaggeAppComponent。 认为 –