AutoValue示例:错误:找不到符号类AutoValue_Animal

AutoValue示例:错误:找不到符号类AutoValue_Animal

问题描述:

我正在尝试了解@AutoValue.我遵循以下示例 https://github.com/google/auto/blob/master/value/userguide/index.md

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

我正在使用Android Studio 3.4

I'm using Android Studio 3.4

我添加了gradle依赖项

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'

我也在使用

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

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

我的代码如下

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

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

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());
    }
}

运行测试时,它会出错

error: cannot find symbol class AutoValue_Animal    

我错过了什么?

将我的设计存储库添加到 https://github.com/elye/issue_android_auto_value

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

显然,问题是因为我放了

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();
}

在测试文件夹而不是源文件夹中.将其移至源文件夹(与MainActivity相同)可以解决该问题.

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