Android Studio Graddle, add GooglePlayServices library

Nowadays, everything is in a continuous change, so I don’t know if you heard about this, but the android team moved to a new build system named Gradle.  Also the Android team moved from supporting Eclipse as IDE to Android Studio. The latest, has the Gradle system incorporated so you might want to started migrating to Android Studio tool and create a new project with it :).

At the moment Android Studio doesn’t have a  user interface tool to ease the project build settings for now but in the future they will add one.

What is Gradle Used For

Gradle is an advanced build toolkit  that helps you create the builds, manage dependencies and allows you to define custom logic for your builds.  You can create your Android  builds from the command line on your computer or on computers where Android Studio is not installed (such as continuous integration servers) which means the Android Gradle plugin does not depend on the Android Studio (but Android Studio is fully integrated with it) or you can create your build the usual way with Android Studio.

Here you can see how you can configure your project to load google play services library project.

I uploaded a set up GooglePlayServices library project here  ready to be added to your main project.

So to include the google play services in your project you should do the 2 following things:

1. Create Gradle GooglePlayServices library project

– You do this by creating an empty android library project using Android Studio, this will add Gradle by default to your project.
– Please give the project a proper name as you will need it to link it with your main project. I named it GooglePlayServices .
– After this, you copy all the resources from res directory of your google play services directory downloaded with android sdk into the res directory of your new project.
– Add the google-play-services.jar file into a new directory(created by you) named libs and it should be right with build and src directories (project root directory)
– The build.gradle file should have the following properties:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android-library'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aild.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['assets']
        }
    }

}

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    compile files('libs/google-play-services.jar')
}

Make sure the apply plugin command has the ‘android-library’ parameter and not ‘android’ !
Ok now we can go to step 2.

2. Add the google play services lib project to your main project

– Copy the new created GooglePlayServices project into your main project root but where you have the settings.gradle file, it should be in the same place as your main module.
– Open settings.gradle file and make sure you include the google play module after your main module:

 include ':YourMainModuleName', ':GooglePlayServices'

– Open your main module build.gradle file and make sure you have this set:

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    compile project(':GooglePlayServices')
}

GooglePlayServices name from the both files is actually the name of the google play module.

To be able to add google maps v2, go to module settings in Android Studio and add google-play-services.jar as dependency to your main module.

This should be all, just rebuild the project and it should work 🙂

Menu