@Retention(value=RUNTIME) @Target(value=TYPE) @Inherited public @interface RealmModule
A RealmModule is a collection of classes extending RealmObject that can be combined with other RealmModules to create the schema for a Realm. This makes it easier to control versioning and migration of those Realms.
A RealmModule can either be a library module or an app module. The distinction is made by setting
library = true
. Setting library = true
is normally only relevant for library authors. See below for
further details.
Currently, it is not possible to have multiple RealmModule declarations in a single file. If you have more than one RealmModule, you will have to use separate Java files for each module.
DefaultRealmModule
which contains all
classes extending RealmObject in a project. This module is automatically known by Realm.
This behavior is problematic when combining a library project and an app project that both uses Realm. This is
because the DefaultRealmModule
will be created for both the library project and the app project, which will
cause the project to fail with duplicate class definition errors.
Library authors are responsible for avoiding this conflict by using explicit modules where library = true
is
set. This disables the generation of the DefaultRealmModule for the library project and allows the library to be
included in the app project that also uses Realm. This means that library projects that uses Realm internally are
required to specify a specific module using RealmConfiguration.modules()
.
App developers are not required to specify any modules, as they implicitly use the DefaultRealmModule
, but
they now has the option of adding the library project classes to their schema using
RealmConfiguration.addModule()
.
Modifier and Type | Optional Element and Description |
---|---|
boolean |
allClasses
Instead of adding all Realm classes manually to a module, set this boolean to true to automatically include all
Realm classes in this project.
|
Class<?>[] |
classes
Specifies the classes extending RealmObject that should be part of this module.
|
RealmNamingPolicy |
classNamingPolicy
The naming policy applied to all classes part of this module.
|
RealmNamingPolicy |
fieldNamingPolicy
The naming policy applied to all field names in all classes part of this module.
|
boolean |
library
Setting this to true will mark this module as a library module.
|
public abstract boolean library
DefaultRealmModule
containing all classes. This is required by libraries so they do not interfere with
Realms running in app code, but it also means that all libraries using Realm must explicitly use a module and
cannot rely on the default module being present.
Creating library modules and normal modules in the same project is not allowed and will result in the annotation
processor throwing an exception.public abstract boolean allClasses
allClasses = true
and classes()
will result in the annotation processor throwing
an exception.public abstract Class<?>[] classes
allClasses = true
and classes()
will result in the annotation processor throwing
an exception.public abstract RealmNamingPolicy classNamingPolicy
RealmNamingPolicy.NO_POLICY
.
To define a naming policy for all fields in the classes, use fieldNamingPolicy()
.
It is possible to override the naming policy specified in the module in each class using the RealmClass
annotation.
If a class is part of multiple modules, the same naming policy must be applied to both modules, otherwise an error will be thrown.
public abstract RealmNamingPolicy fieldNamingPolicy
RealmNamingPolicy.NO_POLICY
. To define a naming policy for class names, use classNamingPolicy()
.
It is possible to override this naming policy using either RealmClass.fieldNamingPolicy()
or
RealmField.name()
.