You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中Java项目无法安装Realm问题求助

在Android Studio Java项目中安装Realm的正确配置(适配Gradle 7.0+)

你的项目采用的是Gradle 7.0+引入的新插件配置格式(plugins DSL),而你参考的是旧版的buildscript配置结构,这是导致安装失败的核心原因。以下是适配你当前项目结构的Realm完整配置步骤:

1. 修改Project级build.gradle

在plugins块中添加Realm插件的classpath:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.3.0' apply false
    id 'com.android.library' version '7.3.0' apply false
    // 添加Realm Kotlin插件(支持Java项目)
    id 'io.realm.kotlin' version '1.10.0' apply false
}

2. 修改Module级build.gradle

在plugins块中应用Realm插件,并添加核心依赖:

plugins {
    id 'com.android.application'
    // 应用Realm插件
    id 'io.realm.kotlin'
}

android {
    namespace 'com.example.notetaking'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.notetaking"
        minSdk 21
        targetSdk 33
        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
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    
    // 添加Realm核心依赖
    implementation 'io.realm.kotlin:library-base:1.10.0'
    // 若需RxJava支持,可添加以下依赖(可选)
    // implementation 'io.realm.kotlin:library-rxjava2:1.10.0'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

3. 确认settings.gradle配置

你的settings.gradle已正确配置mavenCentral仓库(Realm依赖托管在此),无需修改,确保以下代码段存在:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

4. 同步项目

点击Android Studio右上角的「Sync Project with Gradle Files」按钮,等待Gradle同步完成即可。

额外说明

  • Realm Kotlin插件完全支持Java项目,无需切换到Kotlin语言;
  • 你已切换至JDK11是正确的,Gradle 7.x版本要求JDK11及以上,当前配置符合要求。

内容的提问来源于stack exchange,提问作者Daniel Tobi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 00:07:48