3

I'm trying to learn about @AutoValue. I follow the example in https://github.com/google/auto/blob/master/value/userguide/index.md

I'm using Android Studio 3.4

I add my gradle dependency

    implementation 'com.google.auto.value:auto-value-annotations:1.6.6'
    annotationProcessor 'com.google.auto.value:auto-value:1.6.6'

I'm also using

classpath 'com.android.tools.build:gradle:3.4.2'

and

distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip

I have my code as below

@AutoValue
abstract class Animal {
    static Animal create(String name, int numberOfLegs) {
        return new AutoValue_Animal(name, numberOfLegs);
    }

    abstract String name();
    abstract int numberOfLegs();
}

and

public class ExampleUnitTest {
    @Test
    public void testAnimal() {
        Animal dog = Animal.create("dog", 4);
        assertEquals("dog", dog.name());
        assertEquals(4, dog.numberOfLegs());

        // You probably don't need to write assertions like these; just illustrating.
        assertTrue(Animal.create("dog", 4).equals(dog));
        assertFalse(Animal.create("cat", 4).equals(dog));
        assertFalse(Animal.create("dog", 2).equals(dog));

        assertEquals("Animal{name=dog, numberOfLegs=4}", dog.toString());
    }
}

When I run the test, it errors out

error: cannot find symbol class AutoValue_Animal    

What did I missed?

Added my design repository in https://github.com/elye/issue_android_auto_value

Elye
  • 53,639
  • 54
  • 212
  • 474
  • Is there any message other than that in your gradle logs? If AutoValue encounters a problem or is misconfigured, it will not generate a file. What you've posted looks fine so far, but we can only see here what you show us. – Jeff Bowman Oct 08 '19 at 14:09
  • @JeffBowman, That's all I have from creating an empty Java Android project. I have my design in https://github.com/elye/issue_android_auto_value , and the auto value is in the test side – Elye Oct 08 '19 at 22:26
  • I have fix the issue in the repo I share. – Elye Oct 09 '19 at 06:30

1 Answers1

0

Apparent, the issue is because, I put my

@AutoValue
abstract class Animal {
    static Animal create(String name, int numberOfLegs) {
        return new AutoValue_Animal(name, numberOfLegs);
    }

    abstract String name();
    abstract int numberOfLegs();
}

In the test folder instead of the source folder. Moving it over to the source folder (same place as MainActivity) solve the problem.

Elye
  • 53,639
  • 54
  • 212
  • 474