我正在尝试了解如何在我正在制作的库中使用 Realm,经过几天的研究,我现在了解了 RealmModules(如果来自 Realm 的人阅读了这篇文章,您应该真正改进有关在中使用的文档库)。我制作了一个简单的类,它为我提供了库配置 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 {}
在项目应用程序类中,我像这样设置 Realm
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"is not of the schema for this realm 或者如果我将插件添加到应用程序gradle 它无法完全构建项目。
有没有人在库中使用过 Realm 并想解释清楚和深入,也许这可以作为 future 的引用
编辑:使用上面的配置我在构建时遇到了这个错误
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:
我设法通过为应用程序和库定义模块并避免将应用程序中的 Realm 模型命名为与库中的 Realm 模型相同(这是必需的吗?或者有没有办法隔离库模型以便用户可以使用与库模型使用的名称相同的名称吗?)这仍然花了我 2 天的时间进行研究,我仍然不确定我是否做对了。如果比我知识渊博的人制作一个不错的教程或其他东西,那就太好了。
最佳答案
你面临Over 64K Methods Multiple dex file problem not related to realm library you need to add dependencies
为 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 类,请编辑您的 list 文件以在标记中设置 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);
}
}
关于android - 如何在库中使用 Realm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46259994/