我正在尝试在一个用纯 Java 编写的模块中使用 AutoValue,该模块充当我的 Android 应用程序的域模块。我的应用程序由 3 层组成,表示层、域和数据。Presentation 和 Data 都具有 android 依赖项,但 domain 没有。
这是 AutoValue 类数据实现:
import com.google.auto.value.AutoValue;
import org.jetbrains.annotations.Nullable;
import java.util.Date;
@AutoValue
public abstract class Trip {
public abstract int id();
public abstract String name();
public abstract int totalDistance();
@Nullable public abstract Date startDate();
@Nullable public abstract Date endDate();
public static Builder builder() {
return new AutoValue_Trip.Builder().totalDistance(0);
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder id(int id);
public abstract Builder name(String name);
public abstract Builder totalDistance(int totalDistance);
public abstract Builder startDate(Date startDate);
public abstract Builder endDate(Date endDate);
public abstract Trip build();
}
}
Android Studio 找不到生成的 AutoValue_Trip 类,因此将其标记为错误,但是,项目构建并运行良好。
这是我的域模块的 build.gradle
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.2.3'
classpath "net.ltgt.gradle:gradle-apt-plugin:0.12"
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: "net.ltgt.apt"
apply plugin: 'me.tatarka.retrolambda'
//noinspection GroovyUnusedAssignment
sourceCompatibility = 1.8
//noinspection GroovyUnusedAssignment
targetCompatibility = 1.8
configurations {
provided
}
sourceSets {
main {
compileClasspath += configurations.provided
}
}
dependencies {
def domainDependencies = rootProject.ext.domainDependencies
def domainTestDependencies = rootProject.ext.domainTestDependencies
apt domainDependencies.autoValue
compileOnly domainDependencies.autoValue
provided domainDependencies.javaxAnnotation
compile domainDependencies.javaxInject
compile domainDependencies.rxJava
compile domainDependencies.arrow
testCompile domainTestDependencies.junit
testCompile domainTestDependencies.mockito
testCompile domainTestDependencies.assertj
}
我尝试使用如下的 sourceSet 将生成的构建文件和文件夹添加到源路径:
main {
java {
srcDirs += 'src/../build/generated'
}
}
但后来我得到了 compileJava 错误。
有没有人遇到过这个问题,你是如何纠正的?如有必要,将提供其他信息。