javaandroidandroid-studioauto-value

AutoValue sample: error: cannot find symbol class AutoValue_Animal


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


Solution

  • 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.