Android 具有 AutoValue 的 Room Persistence 库实体

Android Room Persistence library entity with AutoValue

是否可以在同一个 POJO 上同时使用 Room persistence library's @Entity with AutoValue@AutoValue 和构建器?我应该怎么做?

AFAIK,截至 1.0.0-alpha3,这是不可能的。房间需要田地; AutoValue 不公开字段。密切关注 this feature request 以了解这方面的进展。

完成此任务后,显然他们添加了支持。我认为在未来的 Room 版本(可能是 androidx.room 版本 2.0.0)中是可能的。 虽然仍未合并为 2.0.0-beta1 的一部分

此功能将在 Room 2.1.0 中提供 https://issuetracker.google.com/issues/62408420#comment27

2.1.0-alpha01 发布!

请注意,您必须添加 @CopyAnnotations 注释才能正常工作

Auto Value: Room now supports declaring AutoValue annotated classes as entities and POJOs. The Room annotations @PrimaryKey, @ColumnInfo, @Embedded and @Relation can now be declared in an auto value annotated class’ abstract methods. Note that these annotation must also be accompanied by @CopyAnnotations for Room to properly understand them.

see the release notes

现在可以使用 Room 数据库 2.1.0。

您可以通过使用@PrimaryKey、@ColumnInfo、@Embedded 和@Relation 注释抽象class 方法来实现这一点。摘要class必须用@Autovalue注解。