Reputation: 959
i'm trying to understand how to use Realm in a library that i'm making, i now understand RealmModules after a couple of days of research (if someone from Realm reads this, you should really improve on documentation about use in libraries). I've made a simple class that gives me the realm with library configuration:
object ChatRealm {
fun getChatRealm(): Realm{
val config = RealmConfiguration.Builder()
.name("mtchat_realmDB")
.schemaVersion(2)
.deleteRealmIfMigrationNeeded()
.modules(ChatModule())
.build()
return Realm.getInstance(config)
}
}
the module is this
@RealmModule(library = true, allClasses = true)
class ChatModule {}
and in the project Application class i setup realm like this
class App: Application() {
override fun onCreate() {
super.onCreate()
initRealm()
setupRealm()
}
private fun initRealm() {
Realm.init(this)
}
private fun setupRealm(){
Realm.setDefaultConfiguration(
RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.modules(Realm.getDefaultModule(), ChatModule())
.build()
)
}
}
Now the trouble i'm having is that the build is failing or the app is crashing for various reasons based on how i configure the gradle files.
My :app gradle is this
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'
repositories {
maven { url "https://jitpack.io" }
maven { url 'https://maven.google.com' }
mavenCentral()
}
buildscript {
repositories {
jcenter()
}
}
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary= true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:26.0.2'
compile 'com.android.support:design:26.0.2'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:support-vector-drawable:26.0.2'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile project(':mtchat')
}
and my library gradle is this
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
repositories {
maven { url "https://jitpack.io" }
maven { url 'https://maven.google.com' }
mavenCentral()
}
buildscript {
repositories {
jcenter()
}
}
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile "com.android.support:appcompat-v7:$compat_version"
compile "com.android.support:support-v4:$compat_version"
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile "com.android.support:cardview-v7:$compat_version"
compile "com.android.support:recyclerview-v7:$compat_version"
compile "com.android.support:design:$compat_version"
compile "de.hdodenhof:circleimageview:2.1.0"
compile 'com.github.bumptech.glide:glide:4.1.1'
compile 'com.android.volley:volley:1.0.0'
}
and this is my project gradle
buildscript {
ext.kotlin_version = '1.1.4-3'
ext.compat_version = '26.0.2'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "io.realm:realm-gradle-plugin:3.7.2"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
and based on if i add or not the apply plugin: 'realm-android' to the :app module i get either RealmObject "X" is not part of the schema for this realm or if i add the plugin to the app gradle it fails to build the project completely.
Is there anyone that has used Realm in a library and wants to explain how, clearly and in depth, maybe this could be used as future reference
EDIT: With the above configuration i get this error when building
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lio/realm/ChatRealmProxyInterface;
EDIT 2:
I managed to get it working by defining modules for both app and library and avoiding naming the realm models in the app the same as the ones in the library ( is this required? Or is there a way to isolate the library models so that the user can use the same names that the library models use?) Still this took me 2 days of research and i'm still unsure if I'm doing it right. It would be REALLY great if someone with more knowledge than me made a nice tutorial or something.
Upvotes: 5
Views: 2401
Reputation:
you are facing Over 64K Methods Multiple dex file problem not related to realm library you need to add dependencies
Configure your app for multidex
android {
defaultConfig {
...
multiDexEnabled true
}
productFlavors {
dev {
// Enable pre-dexing to produce an APK that can be tested on
// Android 5.0+ without the time-consuming DEX build processes.
minSdkVersion 21
}
prod {
// The actual minSdkVersion for the production version.
minSdkVersion 14
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
If you do not override the Application class, edit your manifest file to set android:name in the tag as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:name="android.support.multidex.MultiDexApplication" >
...
</application>
</manifest>
If you do override the Application class, change it to extend MultiDexApplication (if possible) as follows:
public class MyApplication extends SomeOtherApplication {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
Upvotes: 2
Reputation: 1977
Add this to your project gradle file
dependencies {
classpath "io.realm:realm-gradle-plugin:3.5.0"
}
Upvotes: 0