Android Studio 3.0.1依赖冲突求助:preDebugAndroidTestBuild任务执行失败
Hey there, let's tackle this dependency conflict you're running into. The error spells it out clearly: your main app module is using com.android.support:support-annotations:26.1.0, but your test module is pulling in version 27.1.1. Mismatched support library versions like this break builds because they can cause runtime inconsistencies, so Android Studio flags it to prevent headaches later.
Here are two solid ways to fix this:
Method 1: Force a Unified Version Globally
This is the quickest fix—we’ll tell Gradle to use the same annotations version across your entire project, including the test module.
Open your app module’s build.gradle (not the root project one) and add this inside the android block:
android { // Keep your existing android configurations here (compileSdkVersion, defaultConfig, etc.) configurations.all { resolutionStrategy.force 'com.android.support:support-annotations:26.1.0' } }
This forces every dependency in your project (even hidden transitive ones from test libraries) to use version 26.1.0 of the annotations, wiping out the conflict entirely.
Method 2: Exclude Conflicting Dependencies in Test Config
If you’d rather not force versions globally, you can explicitly exclude the conflicting annotations from your test dependencies and manually add the correct version.
In your app module’s build.gradle, update your dependencies block like this:
dependencies { // Your main app dependencies (keep these as is) implementation 'com.android.support:support-annotations:26.1.0' // Update test dependencies to exclude conflicting annotations 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 annotations version to your test dependencies androidTestImplementation 'com.android.support:support-annotations:26.1.0' }
Final Steps
After making either change:
- Click the Sync Now button in Android Studio to apply the Gradle updates.
- Clean and rebuild your project (go to
Build > Clean Project, thenBuild > Rebuild Project).
That should resolve the preDebugAndroidTestBuild failure caused by the version mismatch.
内容的提问来源于stack exchange,提问作者mohammd qandeel

