Android Studio Bumblebeeの新規プロジェクトにKotlin版のRealmを追加する

Android Studioがマルハナバチになってライブラリの追加で迷いました。。。プロジェクトのbuild.gradleがほぼ空になっていて、代わりにsettings.gradleに書くようになっています。

データベースライブラリのrealm-kotlinを追加してみます。ちなみに、Java版とは異なりクラウドの同期には対応していません。(2021年9月現在)

目次

realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.

build.gradle (:app)

dependenciesにcompileOnlyを追加します。本家のサンプルを拝見するとbaseは不要なようです。

dependencies {
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.annotation:annotation:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    compileOnly 'io.realm.kotlin:library:0.5.0'
}

settings.gradle

pluginsに先程と同じバージョンで追加します。

pluginManagement {

    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }

    plugins {
        id 'com.android.application' version '7.1.0-alpha11'
        id 'com.android.library' version '7.1.0-alpha11'
        id 'org.jetbrains.kotlin.android' version '1.5.30'

        id 'io.realm.kotlin' version '0.5.0'
    }
}

以上で、importすれば使えるようになります。