AndroidX迁移构建报错:Program type already present问题求助
Hey there! Let's tackle that duplicate class error you're hitting while migrating to AndroidX. This is a super common issue when mixing old Support Library dependencies with AndroidX, so let's break down the fixes step by step.
What's causing this?
The error tells you that the same class exists in both your AndroidX dependencies and the old Support Library. This happens because some of your third-party libraries are still pulling in old Support Library code, which clashes with AndroidX's equivalent classes.
The main culprits in your setup:
- Outdated build tools: Your Android Gradle Plugin (3.1.2) and Kotlin (1.2.41) versions are too old—they don't have full support for AndroidX, which was introduced in newer tooling.
- Unmigrated third-party libraries:
com.theartofdev.edmodo:android-image-cropper:2.7.0uses old Support Library code instead of AndroidX.- Your Firebase dependencies (15.0.2) are outdated and likely pull in old Support Library classes.
Step-by-Step Solutions
1. Update Your Build Tools & Kotlin Version
First, we need to get your tooling up to speed to support AndroidX properly:
- Open your project-level
build.gradleand update these dependencies:dependencies { classpath 'com.android.tools.build:gradle:3.6.4' // Stable version with full AndroidX support classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72" // Compatible Kotlin version classpath 'com.google.gms:google-services:4.3.10' // Updated for Firebase compatibility } - In your app-level
build.gradle, make sure thekotlin_versionvariable matches the plugin version above (e.g.,ext.kotlin_version = '1.3.72').
2. Replace Unmigrated Third-Party Libraries
Swap out the libraries that are still using old Support Library code:
- Image Cropper: Replace the old version with an AndroidX-compatible release:
implementation 'com.theartofdev.edmodo:android-image-cropper:4.3.0' - Firebase: Upgrade to newer versions that support AndroidX:
implementation 'com.google.firebase:firebase-messaging:23.0.0' implementation 'com.google.firebase:firebase-core:21.1.1'
3. Enable Jetifier to Auto-Migrate Dependencies
Jetifier is a tool that automatically converts old Support Library references in third-party dependencies to AndroidX. Add these lines to your project-level gradle.properties:
android.useAndroidX=true android.enableJetifier=true
This ensures even if a library hasn't been fully migrated, its old Support code gets swapped for AndroidX equivalents.
4. Clean & Rebuild Your Project
Finally, clear any cached build files and rebuild:
- In Android Studio: Go to
Build > Clean Project, thenBuild > Rebuild Project. - Or run this in your terminal:
./gradlew clean build
If You Still Have Issues
If the error persists, run this command to generate a dependency tree and track down any remaining old Support Library references:
./gradlew app:dependencies
Look for any entries under com.android.support:support-v4, then exclude that dependency from the problematic library. For example:
implementation('offending-library') { exclude group: 'com.android.support', module: 'support-v4' }
内容的提问来源于stack exchange,提问作者sk panchal

