objectbox-java
objectbox-java copied to clipboard
ObjectBox on multi-module project
I have a project with a app and multi modules(app, domain, data).
I created an Application in module-app, init objectbox in the module app application, for regenerate the database from some json files.
My entities are in the domain module, and some repositories in the data module.
My problem is that MyObjectBox class is not generated.
How should I make it work?
I also have a multi module project with similar structure. Before MyObjectBox is generated you need to build the project (Make -> Build Project) after you annotate your DB classes with @Entity annotation.
I also have a multi module project with similar structure. Before MyObjectBox is generated you need to build the project (Make -> Build Project) after you annotate your DB classes with @entity annotation.
I tried that, but it didn't generate MyObjectBox class.
MyObjectBox generation will only work for the module where the ObjectBox plugin is applied and only if the plugin can find @Entity
classes in that modules sources.
So it is not possible to have the plugin applied to a module that depends on another module that has the actual @Entity
classes.
Edit: The common solution here is to have a separate database for each feature module (define @Entity
classes, apply the plugin and supply a name when building the store). This also clearly separates concerns.
-ut
Would it be possible in the future?
For now we have no concrete plans for this.
Leaving this open as a suggestion. -ut
Android community runs into modularization pretty quickly now. So it would be great to have this feature.
Just to make sure I understood the requirements here. If you would put all entities in one module, and apply the ObjectBox to it to generate MyObjectBox, would that be sufficient?
It may depend from team to team.
I am inclined to do it that way now and it looks like it works: all the entities in android-library module, and a class providing a BoxStore in the same module. Then, app
module takes the BoxStore and provides boxes to other classes using DI.
The small drawback is that initially we wanted to make a module with our modules java-only and generate the MyObjectBox
class in the main app
module, but it seems impossible for now, because we need to generate a BoxStore
in the same module where entities lie and we need a Context
as we work in Android project. But that's not a critical thing.
However, I saw that some people try to put different entities into different modules, according to features. I can't comment on that approach.
Would love to be able to have entities in one module and be able to use them (create a Box) in another one
Keeping all the Entities in one module may not be feasible solution, there will be requirement to have the the Entities defined in their individual modules specially when projects wants to follow the Clean Architecture approach.
Is there a support for this, if not may have to look for alternative..
@shashi-appgolive It should be fine to have entities in different modules, if you also apply the plugin and have a separate database (e.g. supply a different name when building BoxStore) in each of those feature modules. This will ensure the modules remain independent.
E.g. how would you access BoxStore from each of the feature modules?
There is now an example Android app that uses multiple feature modules, each with its own ObjectBox database: https://github.com/objectbox/objectbox-examples/tree/main/android-app-multimodule
Wonderful, thanks for providing this.
I see that there are 2 different DBs created for with names notes and tasks
store = MyObjectBox.builder()
.androidContext(context)
.name("tasks")
.build()
store = MyObjectBox.builder() .androidContext(context) .name("notes") .build()

Is it possible to have one DB name and different entities ? Or is it like we will be having different DBs/Box for different modules ?
different DBs/Box for different modules ?
This. Each module is completely separate from each other, so also has its own database files.
different DBs/Box for different modules ?
This. Each module is completely separate from each other, so also has its own database files.
Maybe the key is the MyObjectBox.java produce by gradle plugin, which has a reference of Bean_._INSTANCE, Bean.__INSTANCE has a reference of Bean , the bean of other module is not avaiable for current module,
Maybe the arouter give an example , produce the template java code in every module, through interface to communicate for each module, not the interface implementation . Its arouter-compiler module scan other module annotation or interface implementation through DexClassLoader.
by the way , are the sources of objectbox-processor objectbox-gradle-plugin objectbox-generator avaiable now?