Android Studio添加Google Services插件后Gradle报错求助
Gradle报错排查:添加Google Services插件后找不到registerSourceType方法
报错原因
- 版本不兼容:你使用的Android Gradle Plugin(AGP)版本为7.1.1,搭配的Google Services插件4.4.1对AGP版本要求更高,两者API不匹配,导致插件调用了AGP中不存在的
registerSourceType方法。 - 重复引入插件:根目录build.gradle同时通过
buildscript和plugins块引入Google Services插件,容易引发依赖冲突。
解决方法
1. 统一插件引入并匹配版本
删掉根目录build.gradle里buildscript块中的Google Services和AGP依赖,只保留plugins块的声明,同时将AGP版本升级到与Google Services 4.4.1兼容的版本(比如7.4.2,该版本与4.4.1适配良好)。
修改后的根目录build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.4.2' apply false id 'com.android.library' version '7.4.2' apply false id 'com.google.gms.google-services' version '4.4.1' apply false } task clean(type: Delete) { delete rootProject.buildDir }
2. 统一依赖库(必须操作)
你的模块依赖同时混合了Android Support库和AndroidX库,会导致编译冲突,需统一迁移到AndroidX:
- 删除所有
com.android.support开头的依赖 - 检查
gradle.properties文件,确保添加以下配置(无则补充):
android.useAndroidX=true android.enableJetifier=true
调整后的模块dependencies示例:
dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.9.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' implementation 'com.github.f0ris.sweetalert:library:1.5.6' implementation 'de.hdodenhof:circleimageview:3.1.0' implementation 'nl.psdcompany:duo-navigation-drawer:3.0.0' implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' implementation 'com.google.firebase:firebase-auth:22.3.1' implementation("com.google.firebase:firebase-database:20.3.1") implementation 'com.android.volley:volley:1.2.1' }
3. 同步并重建项目
- 点击Android Studio右上角的「Sync Project with Gradle Files」同步项目
- 依次执行「Build > Clean Project」和「Build > Rebuild Project」
内容的提问来源于stack exchange,提问作者Fatima_Afzaal
相关产品推荐
相关产品推荐

