AutoValue 示例:错误:找不到符号类 AutoValue_Animal [英] AutoValue sample: error: cannot find symbol class AutoValue_Animal

查看:28
本文介绍了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());
    }
}

当我运行测试时,它出错

When I run the test, it errors out

error: cannot find symbol class AutoValue_Animal    

我错过了什么?

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.

这篇关于AutoValue 示例:错误:找不到符号类 AutoValue_Animal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆