Android Studio中JCenter仓库警告移除及依赖错误解决咨询
问题背景
项目级build.gradle里出现警告:
Warning:(22, 9) JCenter Maven repository is no longer receiving updates: newer library versions may be available elsewhere
移除jcenter()配置后,又出现大量依赖解析错误,比如:
Could not resolve all files for configuration ':classpath'.
Could not find org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.7.10/kotlin-stdlib-jdk8-1.7.10.pom
可行解决方案
方案1:替换JCenter为Maven Central
JCenter已停止维护,Maven Central是目前主流的替代仓库,大部分开源库都已迁移至此。直接将项目中所有jcenter()替换为mavenCentral()即可,同时保留google()仓库(Android官方依赖、Google服务依赖都在这里)。
修改后的项目级build.gradle代码如下:
buildscript { repositories { google() mavenCentral() // 替换jcenter() } dependencies { classpath 'com.android.tools.build:gradle:7.4.2' classpath 'com.google.gms:google-services:4.3.15' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() mavenCentral() // 替换jcenter() } }
方案2:保留JCenter但限制依赖范围(针对部分未迁移的旧库)
如果项目里有某些老库仅存在于JCenter,无法从Maven Central获取,可以通过内容过滤仅允许特定依赖从JCenter拉取,这样既能消除全局使用JCenter的警告,又能解决依赖缺失问题:
buildscript { repositories { google() mavenCentral() // 仅允许指定组的依赖从JCenter获取 jcenter() { content { includeGroup("com.example.oldlibrary") // 替换为你需要的依赖组名 } } } dependencies { classpath 'com.android.tools.build:gradle:7.4.2' classpath 'com.google.gms:google-services:4.3.15' } } allprojects { repositories { google() mavenCentral() // 同样在allprojects里做相同配置 jcenter() { content { includeGroup("com.example.oldlibrary") } } } }
验证效果
修改后同步Gradle,若仍有依赖找不到,检查对应库的最新版本是否已迁移到Maven Central,或者确认内容过滤的组名是否正确。
内容的提问来源于stack exchange,提问作者Sweta Jain

