Realm库安装失败:无法解析io.realm:realm-gradle-plugin:7.0.0-beta依赖的解决方法咨询
解决Realm Gradle插件无法找到的问题
这个问题的核心原因是Realm 7.x及以上版本已经不再发布到JCenter仓库,而是迁移到了Maven Central,而你的项目配置里没有声明这个仓库,导致Gradle找不到对应的插件构件。另外你的build.gradle还有一些冗余配置需要修正,下面是具体的解决步骤:
步骤1:更新项目级build.gradle配置
打开build.gradle(:project),在buildscript和allprojects的repositories块中添加mavenCentral(),同时建议移除已经停止维护的jcenter()(可选但推荐):
buildscript { ext.kotlin_version = "1.3.72" repositories { google() mavenCentral() // 添加这行以访问Realm插件仓库 // jcenter() // 可选:移除已停止维护的JCenter仓库 } dependencies { classpath "com.android.tools.build:gradle:4.1.1" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "io.realm:realm-gradle-plugin:7.0.0-beta" } } allprojects { repositories { google() mavenCentral() // 同样添加这行 // jcenter() // 可选:移除已停止维护的JCenter仓库 } } task clean(type: Delete) { delete rootProject.buildDir }
步骤2:修正模块级build.gradle的冗余配置
你的build.gradle(:Module)里存在重复应用插件的问题(plugins块中已经声明的插件,不需要再用apply plugin重复添加),优化后的代码如下:
plugins { id 'com.android.application' id 'kotlin-android' id 'kotlin-kapt' id 'kotlin-android-extensions' id 'realm-android' // 用plugins块的方式声明Realm插件,替代重复的apply语句 } android { compileSdkVersion 30 buildToolsVersion "30.0.3" defaultConfig { applicationId "com.example.myapplication" minSdkVersion 23 targetSdkVersion 30 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' } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.2.1' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' implementation 'com.github.bumptech.glide:glide:4.11.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'io.reactivex.rxjava3:rxandroid:3.0.0' implementation 'io.reactivex.rxjava3:rxjava:3.0.7' implementation 'io.reactivex.rxjava3:rxkotlin:3.0.0' implementation 'com.facebook.stetho:stetho:1.5.1' implementation 'com.facebook.stetho:stetho-okhttp3:1.5.1' implementation 'com.facebook.stetho:stetho-js-rhino:1.5.1' }
步骤3:同步并构建项目
完成上述修改后,点击Android Studio右上角的Sync Project with Gradle Files按钮,等待同步完成。如果仍然存在问题,可以尝试执行Clean Project -> Rebuild Project来清除缓存并重新构建。
额外建议
如果7.0.0-beta版本仍然出现异常,建议尝试使用Realm的稳定版(比如7.0.0正式版),beta版本可能存在不稳定的情况,稳定版的兼容性会更好。
内容的提问来源于stack exchange,提问作者Kyu
相关产品推荐
相关产品推荐

