【发布时间】:2018-02-25 20:26:17
【问题描述】:
我正在尝试了解如何在我正在制作的库中使用 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 文件。
我的 :app 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' 添加到 :app 模块,我得到 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 天的时间研究,我仍然不确定我是否做得对。如果有比我知识更多的人制作一个很好的教程或其他东西,那就太好了。
【问题讨论】:
-
(if someone from Realm reads this, you should really improve on documentation about use in libraries)有an official example for it. -
@EpicPandaForce 是的,我读过它,它澄清了一些事情,但没有文档能够真正解释在库中使用领域的规则,比如如果您必须在以下内容中包含“应用插件”: app gradle 如果您已经在库中拥有它,如果库用户可以使用与库中的名称相同的 RealmObjects 以及类似的深度内容。
-
Realm 不考虑模块和模型类的包名,所以是的,在库和应用程序中具有相同的名称会产生冲突。我们确实尝试在文档realm.io/docs/java/latest/#schemas 中记录有关架构的限制,并在上面链接了示例项目,但听起来我们需要更好地描述文档中的限制。
-
啊谢谢,我还以为我做错了什么
标签: android module realm kotlin android-library