3

Trying to use AutoValue of package com.google.auto.value.AutoValue in my java class and get error "AutoValue cannot be resolved to a type"

I have added this in pom.xml

<!-- https://mvnrepository.com/artifact/com.google.auto.value/auto-value -->
<dependency>
    <groupId>com.google.auto.value</groupId>
    <artifactId>auto-value</artifactId>
    <version>1.2</version>
    <scope>provided</scope>
</dependency>

Java program :

package xxxx;

import ------
import com.google.auto.value.AutoValue;

@AutoValue
@DefaultCoder(SerializableCoder.class)
public abstract class ABC{
  /**
   * Manually create a test row.
   */
  public static ABC create(List<Object> fields) {
    return new AutoValue_ABC(fields); //error is here
  }

  public abstract List<Object> fields();
}

I tried the solutions like adding it by opening the project properties, browsing to Java Compiler -> Annotation Processing -> Factory Path, clicking on "Add External JARs" and then selecting 4 jar files - auto-service-1.0-rc1.jar , guava-16.0.1.jar , jsr-305-2.0.3.jar , auto-value-1.0-rc1.jar

Also tried adding m2e-apt plugin and Maven -> "Annotation processing" -> select "Automatically configure JDT APT..."

Still getting the errror "AutoValue cannot be resolved to a type"

Any Solutions? Thanks in Advance.

Praz Solver
  • 513
  • 3
  • 12

1 Answers1

0

Seems you are a bit outdated. Newest version of auto-value is 1.6.5.

You have to import not only auto-value, but also auto-value annotations. From official documentation of auto-value:

<dependency>
  <groupId>com.google.auto.value</groupId>
  <artifactId>auto-value-annotations</artifactId>
  <version>1.6.2</version>
</dependency>
<dependency>
  <groupId>com.google.auto.value</groupId>
  <artifactId>auto-value</artifactId>
  <version>1.6.2</version>
  <scope>provided</scope>
</dependency>

That’s all you need for maven. For eclipse, you seem to have looked at Code Affine. Their article about auto-value is a bit confusing. You only need auto-value and auto-value-annotations. Guava, auto-service, and jsr are not necessary for use with auto-value.

At least, this worked for me.