如何在Android库中实现Realm数据库?

时间:2018-07-13 12:30:03

标签: android android-gradle

将Realm安装为Gradle插件。

步骤1:将类路径依赖项添加到项目级别的build.gradle文件中。

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "io.realm:realm-gradle-plugin:5.3.1"
    }
}

步骤2:将realm-android插件应用于应用程序级别build.gradle文件的顶部。

apply plugin: 'realm-android'

第3步:添加依赖项

 implementation 'io.realm:android-adapters:2.0.0'

但是当我构建项目时,发现此错误:

配置'compile'已过时,并已由'implementation'和'api'代替。

完整的build.gradle(模块)文件。

apply plugin: 'com.android.application'
apply plugin: 'realm-android'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.myfirastapp"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:design:28.0.0-alpha3'
    implementation 'com.android.support:cardview-v7:28.0.0-alpha3'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'io.realm:android-adapters:2.0.0'
}

和build.gradle(Project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath "io.realm:realm-gradle-plugin:3.1.4"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

1 个答案:

答案 0 :(得分:0)

  

在gradle文件中用“实现”替换所有“编译”