Android项目构建失败:com.android.support:support-annotations依赖版本冲突
Hey there! I’ve dealt with this exact dependency conflict plenty of times, so let’s get your build back on track.
What’s Causing the Error?
The issue is straightforward: your main app module is using com.android.support:support-annotations:26.1.0, but your Android Test module is pulling in version 27.1.1. Gradle can’t automatically resolve this mismatch, hence the build failure.
Here Are 3 Reliable Fixes:
1. Force All Modules to Use the Same Annotation Version
If you don’t want to update your main app’s support library version right now, you can force Gradle to use the 26.1.0 version across all modules. Add this block to your app-level build.gradle file:
configurations.all { resolutionStrategy.force 'com.android.support:support-annotations:26.1.0' }
2. Upgrade Your Main App’s Support Library Version
If you’re open to updating your app to use the newer support library (27.1.1), make these changes to your app-level build.gradle:
- First, update your compile and target SDK versions to match:
android { compileSdkVersion 27 defaultConfig { targetSdkVersion 27 // Keep your other default config settings here } } - Then, update all your
com.android.supportdependencies to 27.1.1:dependencies { implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:support-annotations:27.1.1' // Update any other support library dependencies (like recyclerview, design, etc.) to 27.1.1 }
3. Exclude Conflicting Annotations from Test Dependencies
You can explicitly exclude the support-annotations from your test dependencies, then manually add the version your main app uses. Update your test dependencies block like this:
dependencies { // Exclude annotations from test runner and espresso androidTestImplementation('com.android.support.test:runner:1.0.2') { exclude group: 'com.android.support', module: 'support-annotations' } androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2') { exclude group: 'com.android.support', module: 'support-annotations' } // Add the matching version for your app androidTestImplementation 'com.android.support:support-annotations:26.1.0' }
Final Step
After making any of these changes, click the Sync Now button in Android Studio to apply the Gradle updates, then try rebuilding your project.
内容的提问来源于stack exchange,提问作者vaibhav

