AutoValue sample: error: cannot find symbol class AutoValue_Animal

时光毁灭记忆、已成空白 提交于 2019-12-02 01:42:54

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/58286591/autovalue-sample-error-cannot-find-symbol-class-autovalue-animal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!