Search code examples
javaandroidgoogle-play-servicesads

How to use correct version in dependencies to compare play-services-ads:17.2.0


I want to use 'com.google.android.gms:play-services-ads:17.2.0' in my android project, but when after sync I found it has something problems.

My project can still compile, but the app will crashed, and I find my 'com.android.support:appcompat-v7:28.0.0' seens like not compare for 'com.google.android.gms:play-services-ads:17.2.0'.

I try to use 'com.android.support:appcompat-v7:26.1.0' to fixed that, but still not works.

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 28.0.0, 26.1.0. Examples include com.android.support:animated-vector-drawable:28.0.0 and com.android.support:customtabs:26.1.0 less... (Ctrl+F1) 
There are some combinations of libraries, or tools and libraries, that are incompatible, or can lead to bugs. One such incompatibility is compiling with a version of the Android support libraries that is not the latest version (or in particular, a version lower than your targetSdkVersion).  Issue id: GradleCompatible

Here is my log: https://pastebin.com/vYEcKsMh

How can I fixed that, thank you.

Here is my code:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "playground.com.pgapp"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    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'
    compile 'com.android.support:design:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:customtabs:28.0.0'
    implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
    compile 'com.google.android.gms:play-services-ads:17.2.0'
    implementation 'com.google.firebase:firebase-ads:17.2.0'
}

configurations.all {
    resolutionStrategy.eachDependency {  details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion "28.0.0"
            }
        }
    }
}

Solution

  • Add this at the end of app level build.gradle file

    configurations.all {
     resolutionStrategy.eachDependency {  details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion "28.0.0"
            }
        }
      }
    }
    

    Credit to Eugen Pechanec

    EDIT: did you add meta data in manifest

     <meta-data
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true"/>
    

    and

     <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="YOUR_ADMOB_APP_ID"/>