【问题标题】:AutoValue sample: error: cannot find symbol class AutoValue_AnimalAutoValue 示例:错误:找不到符号类 AutoValue_Animal
【发布时间】:2019-10-08 12:42:19
【问题描述】:

我正在尝试了解@AutoValue。我按照中的示例 https://github.com/google/auto/blob/master/value/userguide/index.md

我使用的是 Android Studio 3.4

我添加了我的 gradle 依赖项

    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添加了我的设计库

【问题讨论】:

  • 您的 gradle 日志中是否还有其他消息?如果 AutoValue 遇到问题或配置错误,它不会生成文件。到目前为止,您发布的内容看起来还不错,但我们只能在这里看到您向我们展示的内容。
  • @JeffBowman,这就是我创建一个空的 Java Android 项目所拥有的一切。我的设计在github.com/elye/issue_android_auto_value,自动值在测试端
  • 我已经在我分享的 repo 中解决了这个问题。

标签: java android android-studio auto-value


【解决方案1】:

显然,问题是因为,我把我的

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

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

在测试文件夹而不是源文件夹中。将其移至源文件夹(与 MainActivity 相同的位置)即可解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 2020-05-03
    • 2019-01-13
    • 2018-05-30
    • 2015-01-26
    • 1970-01-01
    相关资源
    最近更新 更多