Android Studio添加play-services-ads后support库版本冲突问题
Hey there, let's tackle this support library version conflict you're facing after adding the Google Play Services Ads dependency!
What's Going On Here?
The error message spells it out clearly: when you added implementation 'com.google.android.gms:play-services-ads:15.0.1', this library secretly pulls in an older version of the Android Support Library (com.android.support:customtabs:26.1.0). But your project is using com.android.support:appcompat-v7:27.1.1—mixing different support library versions can cause weird runtime crashes, so Android Studio is right to flag this as a problem.
Solution 1: Force All Support Libraries to Use Your Target Version
You can add a configuration rule to your build.gradle (Module: app) file that forces every support library dependency to match the version you're using (27.1.1 here). Here's how to update your file:
apply plugin: 'com.android.application' android { compileSdkVersion 27 defaultConfig { applicationId "dea1993.morsetorch" minSdkVersion 23 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } // Add this block to enforce unified support library version configurations.all { resolutionStrategy.eachDependency { DependencyResolveDetails details -> def requested = details.requested if (requested.group == 'com.android.support') { if (!requested.name.startsWith("multidex")) { details.useVersion '27.1.1' } } } } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.0' implementation 'com.google.android.gms:play-services-ads:15.0.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
Solution 2: Explicitly Add the Conflicting Library with Your Version
If you prefer a more direct fix, just explicitly add the customtabs library with version 27.1.1 to your dependencies. This will override the older version that Play Services Ads is trying to use:
dependencies { // ... keep all your existing dependencies ... implementation 'com.android.support:customtabs:27.1.1' // Add this line }
Either solution will resolve the version mismatch. After making the change, click the "Sync Now" button in Android Studio to sync your project with Gradle files, and the error should vanish.
内容的提问来源于stack exchange,提问作者Dea1993

