房间数据库未实现

Room DataBase not implemented

我正在试用来自 googles Android 架构组件的新 Room ORM,我遵循了文档,但是当我 运行 我的代码时,我得到 java.lang.RuntimeException: cannot find implementation for com.zeyad.usecases.app.UserDatabase. UserDatabase_Impl does not exist

这是我的代码:

Gradle 项目:

allprojects {
repositories {
    jcenter()
    maven { url 'https://jitpack.io' }
    maven { url 'https://maven.google.com' }
}
}

Gradle模块:

compile "android.arch.lifecycle:runtime:1.0.0-alpha1"
compile "android.arch.lifecycle:extensions:1.0.0-alpha1"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha1"
compile "android.arch.persistence.room:runtime:1.0.0-alpha1"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"

用户:

@Entity(tableName = "User")
public class User {

    static final String LOGIN = "login";
    private static final String ID = "id", AVATAR_URL = "avatar_url";
    @PrimaryKey
    @SerializedName(LOGIN)
    @ColumnInfo(name = LOGIN)
    String login;

    @SerializedName(ID)
    @ColumnInfo(name = ID)
    int id;

    @SerializedName(AVATAR_URL)
    @ColumnInfo(name = AVATAR_URL)
    String avatarUrl;

    public User() {
    }

    public User(String login, int id, String avatarUrl) {
        this.login = login;
        this.id = id;
        this.avatarUrl = avatarUrl;
    }

    public static boolean isEmpty(User user) {
        return user == null || (user.login == null && user.avatarUrl == null);
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        return id == user.id && (login != null ? login.equals(user.login) : user.login == null
                && (avatarUrl != null ? avatarUrl.equals(user.avatarUrl) : user.avatarUrl == null));
    }

    @Override
    public int hashCode() {
        int result = login != null ? login.hashCode() : 0;
        result = 31 * result + id;
        result = 31 * result + (avatarUrl != null ? avatarUrl.hashCode() : 0);
        return result;
    }
}

用户数据库:

@Database(entities = {User.class}, version = 1)
public abstract class UserDatabase extends RoomDatabase {
    public abstract RoomBaseDao roomBaseDao();
}

RoomBaseDao:

@Dao
public interface RoomBaseDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertItemsReplace(User... objects);

    @Update(onConflict = OnConflictStrategy.REPLACE)
    void updateItemsReplace(User... objects);

    @Delete
    void deleteItem(User object);
}

创建时的应用程序:

UserDatabase gDb = Room.databaseBuilder(this, UserDatabase.class, "test-db").build(); // throws the exception above

提前致谢。

在我的例子中,我使用的是旧版本的 ButterKnife,它需要 android-apt 插件。所以我更新了 ButterKnife(现在使用注释处理器)并删除了 Gradle 文件中的插件声明。

可能这不完全是您的问题,但也许会对其他人有所帮助。

另一件可能导致问题的事情:

configurations.all {
  resolutionStrategy {
    force "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION"
  }
}