Reputation: 53
In my project, I have an app module
with the 'app
' name and an Android Library
called 'app_lib
'. I want to have a database
that both work with this database
.
The question is, how can I do this?
I've done my job so far, I've made my models
and Dao's
classes
in the 'app_lib
', and I've called in the abstract class database
in the 'app
' and created tables
in this way. Now, the question is, how do you do in the 'app_lib
' Call the queries
?
Upvotes: 5
Views: 4559
Reputation: 3562
When creating AppDatabase:
AppDatabase db = Room.databaseBuilder(getApplicationContext(),
AppDatabase.class, "database-name").build();
Second parameter is the name of the database, if you give same name in both modules, it should use same database, provided that they are part of the same application.
You should not import models and dao from your app module. Your app module should be dependent on the library not the other way around.
However, if you don't want to copy over same class files to both modules, you could either move your room classes to the library and import those classes from lib module. If you don't want those classes to be part of lib module, you can create another module that handles database operations and then use classes from that module in both the library module and the app module.
Upvotes: 7