简体   繁体   English

无法使用 AutoValue 和 IntelliJ 解析符号

[英]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:我对我的项目的要求是:

  • Building between IntelliJ and Gradle should be seamless and not interfere with the process of each other IntelliJ 和 Gradle 之间的构建应该是无缝的,并且不会相互干扰进程
  • I need to use IntelliJ's Create separate module per source set option我需要使用 IntelliJ 的Create separate module per source set选项
  • I need to use IntelliJ's folder based structure我需要使用 IntelliJ 的基于文件夹的结构
  • IntelliJ needs to be able to recognise and autocomplete AutoValue classes IntelliJ 需要能够识别和自动完成 AutoValue 类

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 的问题:

  • Create a new Gradle project from IntelliJ从 IntelliJ 创建一个新的 Gradle 项目
  • Check the Create separate module per source set option选中为每个源集创建单独的模块选项
  • Open build.gradle file:打开build.gradle文件:
  • Add the following plugins block:添加以下plugins块:

plugins {
    id 'java'
    id 'net.ltgt.apt' version '0.12'
}
  • Add the following dependencies block添加以下dependencies

dependencies {
    compileOnly 'com.google.auto.value:auto-value:1.5'
    apt 'com.google.auto.value:auto-value:1.5'
}
  • Go to Settings → Build, Execution, Deployment → Annotation Processors Go 到设置→构建、执行、部署→注解处理器
  • Check the Enable Annotation Processing检查启用注释处理
  • Create a class:创建一个 class:

@AutoValue
public abstract class GeneratedSourcesTest {

    static GeneratedSourcesTest create(String field) {
        return new AutoValue_GeneratedSourcesTest(field);
    }

    public abstract String field();
}
  • On IntelliJ run Build → Build Project在 IntelliJ 上运行Build → Build Project
  • Open the 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项目后,请执行以下步骤:

  1. Set annotation processing configuration as follows: 设置注释处理配置如下: 在此输入图像描述

  2. Run menu: Build - Build Project 运行菜单: 构建 - 构建项目

  3. Right click on each new generated folder and select: Mark Directory as - Generated Sources Root so it was marked as follows: 右键单击每个新生成的文件夹,然后选择:将目录标记为 - 生成的源根,以便标记如下: 在此输入图像描述

    1. Add /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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM