android jetpack room 使用问题总结

android jetpack room 使用问题总结

问题一:Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide room.schemaLocation annotation processor argument OR set exportSchema to false.

模式导出目录未提供给注释处理器,因此我们无法导出模式。 您可以提供“ room.schemaLocation”注释处理器参数,也可以将exportSchema设置为false。

解决方法

1.在app的build.gradle中添加如下配置:

      javaCompileOptions {
    
    
            annotationProcessorOptions {
    
    
                arguments = ["room.schemaLocation":
                                     "$projectDir/schemas".toString()]
            }
        }

2、在数据库注解中添加exportSchema = false

@Database(entities = {
    
    User.class},version = 1,exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
    
    
    public abstract UserDao userDao();

}

原因:

在编译时,Room 会将数据库的架构信息导出为 JSON 文件(默认exportSchema = true导出架构)。要导出架构,请在 build.gradle 文件中设置 room.schemaLocation 注释处理器属性(设置将json存放的位置)。

问题二:There are multiple good constructors and Room will pick the no-arg constructor. You can use the @Ignore annotation to eliminate unwanted constructors.

解决方法:
实体有多个构造函数的时候可以使用 @Ignore注解忽略掉其余的构造函数只留一个 。

    public User() {
    
    
    }
    @Ignore
    public User(int uid, String firstName, String lastName) {
    
    
        this.uid = uid;
        this.firstName = firstName;
        this.lastName = lastName;
    }

注意:不能把所有的构造函数都忽略掉,构造函数要么一个都没有要么有一个其他的忽略掉

猜你喜欢

转载自blog.csdn.net/guojingbu/article/details/115245007