Search code examples
android-studioandroid-gradle-plugingoogle-play-servicesandroid-library

Adding dependency to Google Play Services inside Android library


I am developing an Android library that depends on Google Play Services.

The instructions provided by Google on how to setup Google Play Services only specify the steps to follow when adding it to an application project and not a library.

I tried following the same steps for my library and ended up with the following build.gradle file:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
        classpath 'com.google.gms:google-services:2.0.0-alpha3'
    }
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-annotations:+'
    provided 'com.google.android.gms:play-services-auth:8.4.0'
}

apply plugin: 'com.google.gms.google-services'

The problem with this is that when building the project, I get the following exception:

* What went wrong:
Execution failed for task ':processDebugGoogleServices'.
> File google-services.json is missing from module root folder. The Google Services Plugin cannot function without it.

I understand this because, as stated in the Google Play Services documentation (https://developers.google.com/android/guides/setup) , it depends on a json file for configuration. However, since I am building this as a library, I wanted the google-services.json file to be added in the project that will use my library instead of putting it in the library itself.

So how can I do this? How can I compile my library that depends on classes defined in Google Play Services but not really configure Google Play Services inside the library and let it be done by the application that consumes my library?

Thank you!


Solution

  • It was simpler than I thought.

    All I had to do was remove the lines classpath 'com.google.gms:google-services:2.0.0-alpha3' and apply plugin: 'com.google.gms.google-services' from the library's build.gradle file and then add them only to the gradle file in my application project that depends on this custom library.