在图书馆项目中使用Room DB [英] Using Room DB in library project

查看:75
本文介绍了在图书馆项目中使用Room DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将会议室DB集成到图书馆项目中

I am trying to integrate room DB in a library project

apply plugin: 'com.android.library'
.
.
.
.
.
compile "android.arch.persistence.room:runtime:$rootProject.roomVersion"

annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"

当我在应用中使用此库并尝试访问Room DB时,它总是崩溃并给我以下异常消息

When I use this library inside my app and try to access Room DB, it always crashes and give me following exception

Android room persistent: AppDatabase_Impl does not exist

但是,当我直接在应用程序中使用Room DB时,它可以正常工作. 在图书馆项目中使用Room DB有什么限制吗?

However when I use room DB directly in my application it works fine. Is there any limitation in using room DB inside library project?

推荐答案

首先将应用卸载到设备和仿真器中.

First of uninstall your app into device and emulator.

然后按照以下步骤流向房间db. 将以下Dependecy添加到应用程序级别gradle文件中.

Then flow below step to room db. add below dependecy into app level gradle file.

    implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"

而不是像下面这样进行应用程序级别的活动..

than make app level activity like below ..

public class AppActivity extends Application {

static AppDatabase db;

@Override
public void onCreate() {
    super.onCreate();
    db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").build();
}

public static AppDatabase getDatabase() {
    return db;
}

}

然后制作应用数据库.

@Database(entities = {MyTable.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract MyTableDao getTableDao();
}

道..

@Dao
public interface MyTableDao {
@Insert
void insertData(MyTable myTable);
@Query("SELECT * FROM MyTable ")
List<MyTable> getData();

}

然后还将应用程序活动定义到android清单文件中的应用程序标签中.

then also define app activity into android manifest file in to application tag.

        android:name=".AppActivity"

这篇关于在图书馆项目中使用Room DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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