无法使用AutoValue和IntelliJ解析符号 [英] Cannot resolve symbol with AutoValue and IntelliJ

查看:386
本文介绍了无法使用AutoValue和IntelliJ解析符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力为IntelliJ的注释处理找到正确的设置,以便它与Gradle的构建过程共存。



每当我使用IntelliJ构建时无法通过


  • 运行菜单:构建 - 构建项目


  • 右键单击每个新的生成的文件夹并选择:将Mark Directory标记为 - Gene评级来源,因此标记如下:


    1. / generated 添加到项目的 .gitignore 文件


  • 这是一个最小的可行配置,它将为生成的类提供完整的IDE支持。
    缺点是,每当重新导入Gradle项目时,生成的文件夹将需要再次标记为 Generated Sources Root
    也许这可以通过在 build.gradle 下添加这些路径作为源集来改进。



    有时会发生IDEA模块丢失它们的编译器输出路径设置在上面的结果中。只需将其设置回默认文件夹即可。


    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.

    Whenever I build from IntelliJ I cannot get it to recognise the generated sources from the 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
    • I need to use IntelliJ's Create separate module per source set option
    • I need to use IntelliJ's folder based structure
    • IntelliJ needs to be able to recognise and autocomplete AutoValue classes

    Here are the steps for a MCVE in order to reproduce the issue with IntelliJ 2017.2.4 and Gradle 3.5:

    • Create a new Gradle project from IntelliJ
    • Check the Create separate module per source set option
    • Open build.gradle file:
    • Add the following plugins block:

    plugins {
        id 'java'
        id 'net.ltgt.apt' version '0.12'
    }
    

    • Add the following dependencies block

    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
    • Check the Enable Annotation Processing
    • Create a 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
    • Open the GeneratedSourcesTest class, on the static factory method, everything compiles fine but I get the error:

    cannot resolve symbol ‘AutoValue_GeneratedSourcesTest’

    How can I make the AutoValue_GeneratedSourcesTest class accessible from IntelliJ?

    解决方案

    After importing your Gradle project under IDEA do the following steps:

    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

    That's a minimal viable configuration which will provide full IDE support for generated classes. The drawback is, whenever Gradle project gets re-imported the generated folders will need be marked as Generated Sources Root again. Perhaps this can be improved with adding these paths as source sets under build.gradle.

    Sometimes it happens that IDEA modules lose their compiler output path settings in result of the above. It's sufficient to just set it back to their default folders.

    这篇关于无法使用AutoValue和IntelliJ解析符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆