如何在库中使用Realm

我想了解如何在我正在使用的库中使用Realm,经过几天的研究,我现在了解RealmModules(如果来自Realm的读者,请务必改进关于在库中使用的文档)。 我已经做了一个简单的类,给我的库配置领域:

object ChatRealm { fun getChatRealm(): Realm{ val config = RealmConfiguration.Builder() .name("mtchat_realmDB") .schemaVersion(2) .deleteRealmIfMigrationNeeded() .modules(ChatModule()) .build() return Realm.getInstance(config) } } 

模块是这个

 @RealmModule(library = true, allClasses = true) class ChatModule {} 

并在项目应用程序类我安装领域像这样

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

现在我遇到的麻烦是构建失败或应用程序由于各种原因崩溃,基于我如何配置gradle文件。

我的:应用程序gradle是这样的

 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') } 

而我的图书馆gradle是这样的

 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' } 

这是我的项目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 } 

并基于如果我添加或不应用插件:“realm-android”到:应用程序模块我得到RealmObject“X”不是这个领域的架构的一部分,或者如果我添加插件到应用程序gradle失败完全建设项目。

有没有人在图书馆中使用Realm,并且想要解释如何,清楚而深入的,也许这可以作为未来的参考

编辑:与上面的配置,我得到这个错误时,建设

 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; 

编辑2:

我设法通过为应用程序和库定义模块,避免将应用程序中的领域模型命名为库中的领域模型(这是必需的吗?还是有办法隔离库模型,以便用户可以使用图书馆模型使用的相同名称?)但是,这仍然花了我2天的研究,我仍然不确定我是否做得对。 如果有人比我有更多的知识做了一个很好的教程或什么的,那真是太棒了。

你正面临超过64K的方法多个dex文件问题不涉及领域库你需要添加依赖关系

配置您的应用程序的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' } 

如果您不覆盖Application类,请编辑您的清单文件以在标记中设置android:name,如下所示:

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

如果您重写Application类,请将其更改为扩展MultiDexApplication(如果可能),如下所示:

 public class MyApplication extends SomeOtherApplication { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } } 

将其添加到您的项目gradle文件

 dependencies { classpath "io.realm:realm-gradle-plugin:3.5.0" }