ale*_*rdi 5 android module realm android-library kotlin
我正在尝试了解如何在我正在制作的库中使用 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)
}
}
Run Code Online (Sandbox Code Playgroud)
模块是这个
@RealmModule(library = true, allClasses = true)
class ChatModule {}
Run Code Online (Sandbox Code Playgroud)
在项目应用程序类中我设置了这样的领域
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()
)
}
}
Run Code Online (Sandbox Code Playgroud)
现在我遇到的麻烦是,根据我配置 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')
}
Run Code Online (Sandbox Code Playgroud)
我的图书馆 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'
}
Run Code Online (Sandbox Code Playgroud)
这是我的项目 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
}
Run Code Online (Sandbox Code Playgroud)
并根据我是否添加 apply plugin: '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;
Run Code Online (Sandbox Code Playgroud)
编辑2:
我设法通过为应用程序和库定义模块并避免将应用程序中的领域模型命名为与库中的领域模型相同的方式来使其工作(这是必需的吗?或者是否有一种方法可以隔离库模型,以便用户可以使用库模型使用的相同名称吗?)但这仍然花了我 2 天的研究时间,我仍然不确定我是否做得正确。如果有比我知识更多的人制作一个很好的教程或其他东西,那就太好了。
将其添加到您的项目 gradle 文件中
dependencies {
classpath "io.realm:realm-gradle-plugin:3.5.0"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2224 次 |
| 最近记录: |