objectbox-java icon indicating copy to clipboard operation
objectbox-java copied to clipboard

ObjectBox on multi-module project

Open JuanFcoMiranda opened this issue 6 years ago • 16 comments

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?

JuanFcoMiranda avatar Oct 18 '18 19:10 JuanFcoMiranda

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.

abdurahmanadilovic avatar Oct 24 '18 10:10 abdurahmanadilovic

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.

JuanFcoMiranda avatar Oct 24 '18 20:10 JuanFcoMiranda

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

greenrobot-team avatar Oct 29 '18 09:10 greenrobot-team

Would it be possible in the future?

JuanFcoMiranda avatar Oct 29 '18 19:10 JuanFcoMiranda

For now we have no concrete plans for this.

Leaving this open as a suggestion. -ut

greenrobot-team avatar Oct 30 '18 06:10 greenrobot-team

Android community runs into modularization pretty quickly now. So it would be great to have this feature.

Gaket avatar Dec 02 '18 08:12 Gaket

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?

greenrobot avatar Dec 02 '18 09:12 greenrobot

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.

Gaket avatar Dec 02 '18 09:12 Gaket

Would love to be able to have entities in one module and be able to use them (create a Box) in another one

pgetsos avatar Apr 23 '20 01:04 pgetsos

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 avatar Apr 20 '22 15:04 shashi-appgolive

@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?

greenrobot-team avatar Apr 25 '22 10:04 greenrobot-team

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

greenrobot-team avatar May 17 '22 11:05 greenrobot-team

Wonderful, thanks for providing this.

shashi-appgolive avatar May 17 '22 13:05 shashi-appgolive

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()

Screenshot 2022-05-23 at 6 43 28 PM

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 ?

shashi-appgolive avatar May 23 '22 13:05 shashi-appgolive

different DBs/Box for different modules ?

This. Each module is completely separate from each other, so also has its own database files.

greenrobot-team avatar May 24 '22 06:05 greenrobot-team

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?

showwiki avatar Jun 05 '22 00:06 showwiki