Flutter/Android项目Duplicate Class错误求助
Hey there! I see you're new to Android and Flutter and ran into this duplicate class issue—don't worry, this is a super common problem when mixing old and new Google Play Services/Firebase dependencies, and we can fix it step by step.
What's Causing the Issue?
The error boils down to conflicting versions of Google Play Services/Firebase modules:
- Your project directly uses the monolithic
play-services:12.0.1package (a bloated bundle that includes dozens of sub-modules like places, measurement, etc.) - Your Flutter dependencies (like
firebase_core,google_sign_in,google_maps_flutter) automatically pull in newer versions of these same sub-modules.
These overlapping, mismatched versions create duplicate class definitions that Gradle can't resolve.
Step-by-Step Fix
1. Replace the Monolithic Play Services Dependency
Instead of using the entire play-services package (which is outdated and adds unnecessary bloat), use only the specific sub-modules your app actually needs. For example, if you're using location features, replace the full package with the location sub-module.
Update the dependencies section in your app/build.gradle:
dependencies { implementation 'com.android.support:multidex:1.0.3' // Replace the full play-services package with targeted sub-modules implementation 'com.google.android.gms:play-services-location:17.1.0' // Match version with Flutter's dependencies implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" }
Note: If you need other sub-modules (like maps or auth), add them individually. Check the official Google Play Services docs for the latest compatible versions.
2. Update Google Services Plugin Version
Your root build.gradle uses an older version of the Google Services plugin that might not play nice with newer Firebase dependencies. Update it to a more recent compatible version:
buildscript { ext.kotlin_version = '1.3.50' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:4.0.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Updated to a newer compatible version classpath 'com.google.gms:google-services:4.3.15' } }
3. Clean and Rebuild the Project
After making these changes, clear cached dependencies and rebuild to ensure everything syncs properly:
flutter clean flutter pub get flutter run
Verify Dependencies (Optional)
If you want to double-check for remaining conflicts, run this command to see all resolved dependencies:
flutter pub deps
This will show you exactly which versions of each module are being used, so you can confirm no mismatches remain.
Original Files for Reference
app/build.gradle (Original)
def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { compileSdkVersion 30 sourceSets { main.java.srcDirs += 'src/main/kotlin' } lintOptions { disable 'InvalidPackage' } defaultConfig { applicationId "com.example.ipill" minSdkVersion 25 targetSdkVersion 30 versionCode flutterVersionCode.toInteger() versionName flutterVersionName multiDexEnabled true } buildTypes { release { signingConfig signingConfigs.debug } } } flutter { source '../..' } dependencies { implementation 'com.android.support:multidex:1.0.3' implementation 'com.google.android.gms:play-services:12.0.1' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" } apply plugin: 'com.google.gms.google-services'
Root build.gradle (Original)
buildscript { ext.kotlin_version = '1.3.50' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:4.0.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.3.3' } } allprojects { repositories { google() jcenter() } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(':app') } task clean(type: Delete) { delete rootProject.buildDir }
Error Log
* What went wrong: Execution failed for task ':app:checkDebugDuplicateClasses'. > 1 exception was raised by workers: java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class com.google.android.gms.location.places.zza found in modules jetified-play-services-places-12.0.1-runtime.jar (com.google.android.gms:play-services-places:12.0.1) and jetified-play-services-places-placereport-17.0.0-runtime.jar (com.google.android.gms:play-services-places-placereport:17.0.0) Duplicate class com.google.android.gms.measurement.AppMeasurement found in modules jetified-firebase-analytics-impl-12.0.1-runtime.jar (com.google.firebase:firebase-analytics-impl:12.0.1) and jetified-play-services-measurement-impl-17.4.1-runtime.jar (com.google.android.gms:play-services-measurement-impl:17.4.1) Duplicate class com.google.android.gms.measurement.AppMeasurement$ConditionalUserProperty found in modules jetified-firebase-analytics-impl-12.0.1-runtime.jar (com.google.firebase:firebase-analytics-impl:12.0.1) and jetified-play-services-measurement-impl-17.4.1-runtime.jar (com.google.android.gms:play-services-measurement-impl:17.4.1) Duplicate class com.google.android.gms.measurement.AppMeasurement$Event found in modules jetified-firebase-analytics-impl-12.0.1-runtime.jar (com.google.firebase:firebase-analytics-impl:12.0.1) and jetified-play-services-measurement-impl-17.4.1-runtime.jar (com.google.android.gms:play-services-measurement-impl:17.4.1) Duplicate class com.google.android.gms.measurement.AppMeasurement$EventInterceptor found in modules jetified-firebase-analytics-impl-12.0.1-runtime.jar (com.google.firebase:firebase-analytics-impl:12.0.1) and jetified-play-services-measurement-impl-17.4.1-runtime.jar (com.google.android.gms:play-services-measurement-impl:17.4.1) Duplicate class com.google.android.gms.measurement.AppMeasurement$OnEventListener found in modules jetified-firebase-analytics-impl-12.0.1-runtime.jar (com.google.firebase:firebase-analytics-impl:12.0.1) and jetified-play-services-measurement-impl-17.4.1-runtime.jar (com.google.android.gms:play-services-measurement-impl:17.4.1) Duplicate class com.google.android.gms.measurement.AppMeasurement$Param found in modules jetified-firebase-analytics-impl-12.0.1-runtime.jar (com.google.firebase:firebase-analytics-impl:12.0.1) and jetified-play-services-measurement-impl-17.4.1-runtime.jar (com.google.android.gms:play-services-measurement-impl:17.4.1) Duplicate class com.google.android.gms.measurement.AppMeasurement$UserProperty found in modules jetified-firebase-analytics-impl-12.0.1-runtime.jar (com.google.firebase:firebase-analytics-impl:12.0.1) and jetified-play-services-measurement-impl-17.4.1-runtime.jar (com.google.android.gms:play-services-measurement-impl:17.4.1) Duplicate class com.google.firebase.analytics.FirebaseAnalytics found in modules jetified-firebase-analytics-impl-12.0.1-runtime.jar (com.google.firebase:firebase-analytics-impl:12.0.1) and jetified-play-services-measurement-api-17.4.1-runtime.jar (com.google.android.gms:play-services-measurement-api:17.4.1) Duplicate class com.google.firebase.analytics.FirebaseAnalytics$Event found in modules jetified-firebase-analytics-impl-12.0.1-runtime.jar (com.google.firebase:firebase-analytics-impl:12.0.1) and jetified-play-services-measurement-api-17.4.1-runtime.jar (com.google.android.gms:play-services-measurement-api:17.4.1) Duplicate class com.google.firebase.analytics.FirebaseAnalytics$Param found in modules jetified-firebase-analytics-impl-12.0.1-runtime.jar (com.google.firebase:firebase-analytics-impl:12.0.1) and jetified-play-services-measurement-api-17.4.1-runtime.jar (com.google.android.gms:play-services-measurement-api:17.4.1) Duplicate class com.google.firebase.analytics.FirebaseAnalytics$UserProperty found in modules jetified-firebase-analytics-impl-12.0.1-runtime.jar (com.google.firebase:firebase-analytics-impl:12.0.1) and jetified-play-services-measurement-api-17.4.1-runtime.jar (com.google.android.gms:play-services-measurement-api:17.4.1)
Flutter pubspec.yaml Dependencies
flutter: sdk: flutter cupertino_icons: ^0.1.3 firebase_core: ^0.5.0 firebase_auth: ^0.18.0+1 cloud_firestore: ^0.14.0+2 google_sign_in: ^4.5.3 google_maps_flutter: ^0.5.27+3 http: ^0.12.0+1 json_serializable: ^3.3.0 geolocator: ^6.0.0 google_place: ^0.3.3 dev_dependencies: pedantic: ^1.9.0 build_runner: ^1.10.0 flutter_test: sdk: flutter
内容的提问来源于stack exchange,提问作者Alex97




