Cannot resolve symbol with AutoValue and IntelliJ
Asked Answered
F

4

13

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?

Faceharden answered 26/9, 2017 at 11:15 Comment(1)
See youtrack.jetbrains.com/issue/IDEA-124090Upgrade
H
15

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

  1. Set annotation processing configuration as follows: enter image description here

  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: enter image description here

    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.

Horntail answered 9/10, 2017 at 15:51 Comment(2)
Thanks a lot.Performing first 2 steps worked for me. But why is that things like these always make you struggle, no matter which IDE you are working with?Symploce
Wow! thanks for the detailed answer! also worth to mention to use AutoValue Plugin that make life even easier :)Senter
D
3

The answers are (should be) in the README for the gradle-apt-plugin: https://github.com/tbroyer/gradle-apt-plugin

Namely, also apply the net.ltgt.apt-idea plugin.

Btw, I recommend delegating build/run actions to Gradle in IntelliJ. Sure it's a bit slower, but requires zero setup in the IDE and works reliably. That said, it should also work OK if you don't.

Dogfight answered 4/10, 2017 at 18:10 Comment(3)
Thanks very much. I think that the idea plugin obliges me to use file based settings. I would much rather use folder based settings for code format reasons.Faceharden
Assuming you mean .idea/ vs. *.iws, then no, absolutely not. I used folder based settings in Idea, with the Gradle integration (not the idea task), along with delegating build/run actions to Gradle.Dogfight
Thanks for the clarification and correction. That sounds great. I will go back and investigate.Faceharden
J
1

Just have your build.gradle with these and it works fine, no need of touching intellij, source set etc..

    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"
}
Jockey answered 31/1, 2019 at 5:58 Comment(0)
C
-1

I didn't have to do anything to intellij using maven by adding the optional true tag.

<dependency>
    <groupId>com.google.auto.value</groupId>
    <artifactId>auto-value</artifactId>
    <version>1.9</version>
    <optional>true</optional>
</dependency>
Confucius answered 18/2, 2022 at 12:25 Comment(2)
we're talking about gradleFaceharden
The title says "Cannot resolve symbol with AutoValue and IntelliJ" and this is a way to fix it if you are using maven.Confucius

© 2022 - 2024 — McMap. All rights reserved.