Android StudioにRealmをインストールする

Android Studio(AndroidSDK)にRealmを入れようとしたところ、build.gradleファイルを編集して同期させる際にエラーが出まくってハマりました。

詳しいことは不明なのですが、GitHubのIssuesを参考にして何とかインストールできました。

settings.gradleの一文を編集するのがポイントです。

スポンサードリンク

■環境

M1 MacBook Pro(macOS Monterey 12.0.1)

Android Studio Arctic Fox | 2020.3.1 Patch 4

Realm 10.4.0

▼build.gradle(Project)のdependenciesに「classpath “io.realm:realm-gradle-plugin:10.4.0″」を追加

buildscript {
    repositories {
        google()
        mavenCentral()
    }


    dependencies {
        classpath "com.android.tools.build:gradle:7.0.4"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"

        classpath "io.realm:realm-gradle-plugin:10.4.0"

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

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

▼build.gradle(Module)のpluginsに「id ‘kotlin-kapt’」「id ‘realm-android’」の順で追加

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'realm-android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.mytestproject"
        minSdk 23
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }

}

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

▼settings.gradleのrepositoriesMode.set()の値を「RepositoriesMode.PREFER_SETTINGS」に変更

dependencyResolutionManagement {
//    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}

rootProject.name = "MyTestProject"
include ':app'

この状態でプロジェクトとGradleファイルをSyncさせます。

参考
GitHub Issues #7374:https://github.com/realm/realm-java/issues/7374

RepositoriesModeについて:https://docs.gradle.org/current/javadoc/org/gradle/api/initialization/resolve/RepositoriesMode.html

スポンサードリンク

コメント

タイトルとURLをコピーしました