Android编译报错Program type already present: android.support.v4.app.BackStackRecord$Op求助
Hey there, let's work through this frustrating duplicate dependency error you're hitting. This specific error almost always stems from either conflicting versions of Android support libraries in your project, or a mix of old support libraries and AndroidX components. Here are step-by-step fixes you can try right away:
1. Hunt down duplicate support library dependencies
First, let's identify if multiple versions of the same support library are being pulled in.
- Open your app-level
build.gradlefile and scan through thedependenciesblock. Look for any repeated references to libraries likecom.android.support:support-v4or related support artifacts. - Run this command in your project's terminal to get a full breakdown of all dependencies and spot conflicts:
./gradlew app:dependencies - If you find a conflicting library being pulled in by a third-party dependency, exclude it explicitly. For example:
implementation('com.example:third-party-lib:1.2.3') { exclude group: 'com.android.support', module: 'support-v4' }
2. Migrate to AndroidX (recommended long-term solution)
Mixing old support libraries with AndroidX is a common cause of this error. AndroidX is the official replacement, so migrating will prevent future conflicts:
- In Android Studio, go to
Refactor > Migrate to AndroidX... - Follow the on-screen prompts to update all your project files and dependencies to their AndroidX equivalents.
- Ensure your
gradle.propertiesfile includes these two lines to enable Jetifier (which converts third-party support library uses to AndroidX):android.useAndroidX=true android.enableJetifier=true
3. Enforce consistent support library versions (if sticking with support libraries)
If you're not ready to migrate to AndroidX yet, force all dependencies to use the same support library version matching your compileSdkVersion (you're using 27, so stick to 27.x.x versions):
Add this inside the android block of your app-level build.gradle:
configurations.all { resolutionStrategy { force 'com.android.support:support-v4:27.1.1' force 'com.android.support:appcompat-v7:27.1.1' // Add other support libraries you use here with the same version } }
4. Clean and rebuild your project
Cached build files can sometimes cause odd lingering issues. Do a full clean and rebuild:
- In Android Studio, click
Build > Clean Project - Then click
Build > Rebuild Project - If that doesn't help, delete the
.gradlefolder in your project root, restart Android Studio, and try building again.
If you can share the full dependencies block from your gradle file, we can zero in on the exact conflicting library. But these steps should resolve most cases of this error.
内容的提问来源于stack exchange,提问作者Mubashar Rasheed

