Using Room DB in library project

夙愿已清 提交于 2019-12-24 00:39:58

问题


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"

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

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


回答1:


First of uninstall your app into device and emulator.

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;
}

}

then make app database..

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

Dao ..

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

}

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

        android:name=".AppActivity"


来源:https://stackoverflow.com/questions/50508318/using-room-db-in-library-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!