[英]Cannot resolve symbol with AutoValue and IntelliJ
I have been trying to find the correct settings for IntelliJ's annotation processing in order for it to co-exist with Gradle's build process.我一直在尝试为 IntelliJ 的注释处理找到正确的设置,以便它与 Gradle 的构建过程共存。
Whenever I build from IntelliJ I cannot get it to recognise the generated sources from the gradle-apt-plugin .每当我从 IntelliJ 构建时,我都无法让它识别gradle-apt-plugin生成的源。
My requirements for my project are:我对我的项目的要求是:
Here are the steps for a MCVE in order to reproduce the issue with IntelliJ 2017.2.4 and Gradle 3.5:以下是 MCVE 的步骤,以重现 IntelliJ 2017.2.4 和 Gradle 3.5 的问题:
plugins
block:plugins
块:plugins {
id 'java'
id 'net.ltgt.apt' version '0.12'
}
dependencies
blockdependencies
块dependencies {
compileOnly 'com.google.auto.value:auto-value:1.5'
apt 'com.google.auto.value:auto-value:1.5'
}
@AutoValue
public abstract class GeneratedSourcesTest {
static GeneratedSourcesTest create(String field) {
return new AutoValue_GeneratedSourcesTest(field);
}
public abstract String field();
}
GeneratedSourcesTest
class, on the static factory method, everything compiles fine but I get the error:GeneratedSourcesTest
class,在 static 工厂方法上,一切都可以正常编译,但出现错误: cannot resolve symbol 'AutoValue_GeneratedSourcesTest'
How can I make the AutoValue_GeneratedSourcesTest
class accessible from IntelliJ?如何使
AutoValue_GeneratedSourcesTest
class 可从 IntelliJ 访问?
After importing your Gradle project under IDEA do the following steps: 在IDEA下导入Gradle项目后,请执行以下步骤:
Set annotation processing configuration as follows: 设置注释处理配置如下:
Run menu: Build - Build Project 运行菜单: 构建 - 构建项目
Right click on each new generated folder and select: Mark Directory as - Generated Sources Root so it was marked as follows: 右键单击每个新生成的文件夹,然后选择:将目录标记为 - 生成的源根,以便标记如下:
/generated
to project's .gitignore file /generated
到项目的.gitignore文件 That's a minimal viable configuration which will provide full IDE support for generated classes. 这是一个最小的可行配置,它将为生成的类提供完整的IDE支持。 The drawback is, whenever Gradle project gets re-imported the generated folders will need be marked as Generated Sources Root again.
缺点是,每当重新导入Gradle项目时, 生成的文件夹都需要再次标记为Generated Sources Root 。 Perhaps this can be improved with adding these paths as source sets under build.gradle .
也许这可以通过在build.gradle下添加这些路径作为源集来改进。
Sometimes it happens that IDEA modules lose their compiler output path settings in result of the above. 有时,由于上述结果,IDEA模块会丢失其编译器输出路径设置。 It's sufficient to just set it back to their default folders.
只需将其设置回默认文件夹就足够了。
The answers are (should be) in the README for the gradle-apt-plugin: https://github.com/tbroyer/gradle-apt-plugin 答案是(应该)在gradle-apt-plugin的自述文件中: https : //github.com/tbroyer/gradle-apt-plugin
Namely, also apply the net.ltgt.apt-idea
plugin. 即,也应用
net.ltgt.apt-idea
插件。
Btw, I recommend delegating build/run actions to Gradle in IntelliJ. 顺便说一句,我建议将构建/运行操作委派给IntelliJ中的Gradle。 Sure it's a bit slower, but requires zero setup in the IDE and works reliably.
当然它有点慢,但在IDE中需要零设置并且可靠地工作。 That said, it should also work OK if you don't.
也就是说,如果你不这样做,它也应该可行。
Just have your build.gradle with these and it works fine, no need of touching intellij, source set etc.. 只需要你的build.gradle与它们工作正常,不需要触摸intellij,源集等。
plugins {
id 'java'
id "net.ltgt.apt" version "0.20"
}
apply plugin: 'idea'
apply plugin: 'net.ltgt.apt-idea'
group 'abc'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile "com.google.auto.value:auto-value-annotations:1.6.2"
annotationProcessor "com.google.auto.value:auto-value:1.6.2"
}
I didn't have to do anything to intellij using maven by adding the optional true tag.通过添加可选的 true 标记,我不必使用 maven 对 intellij 做任何事情。
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.9</version>
<optional>true</optional>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.