使用Firebase ML Kit的Android应用运行时出现重复类错误
Hey there, let's sort out that duplicate class issue you're facing with your Android app's Firebase ML Kit setup!
What's Causing the Error?
The duplicate class messages you're seeing happen because the com.google.firebase:firebase-ml-vision:24.0.3 dependency pulls in play-services-vision:20.0.0, while another indirect dependency brings in an older play-services-vision-common:19.1.0—these two modules share overlapping internal classes, leading to the build conflict.
On top of that, the old firebase-ml-vision library is deprecated; Google now recommends using the standalone ML Kit libraries for text recognition, which are better maintained and less prone to version conflicts.
Solution 1: Migrate to the Latest ML Kit Text Recognition Library (Recommended)
Replace the deprecated Firebase ML Vision dependency with the modern standalone ML Kit text recognition library, and let the Firebase BOM handle versioning to avoid conflicts.
Here's your updated build.gradle (Module: app) configuration:
apply plugin: 'com.google.gms.google-services' dependencies { implementation 'androidx.appcompat:appcompat:1.2.0' implementation platform('com.google.firebase:firebase-bom:26.1.0') implementation 'com.google.firebase:firebase-analytics' // Replace deprecated ML Vision with standalone text recognition library implementation 'com.google.mlkit:text-recognition:16.0.0' }
Why This Works:
- The standalone ML Kit libraries are decoupled from older Firebase dependencies, eliminating the overlapping class issue.
- The Firebase BOM (
com.google.firebase:firebase-bom) automatically syncs versions across all Firebase/ML Kit libraries, so you don't have to manually manage version numbers and risk conflicts.
Solution 2: Fix the Conflict Without Migrating (Not Recommended)
If you need to stick with the deprecated firebase-ml-vision library for now, you can exclude the conflicting play-services-vision-common module from the ML Vision dependency:
apply plugin: 'com.google.gms.google-services' dependencies { implementation 'androidx.appcompat:appcompat:1.2.0' implementation platform('com.google.firebase:firebase-bom:26.1.0') implementation 'com.google.firebase:firebase-analytics' implementation ('com.google.firebase:firebase-ml-vision:24.0.3') { // Exclude the conflicting module exclude group: 'com.google.android.gms', module: 'play-services-vision-common' } }
Note: This is a temporary fix—Google won't update the deprecated library anymore, so migrating to the new ML Kit is the long-term solution.
内容的提问来源于stack exchange,提问作者Mohamad

