Back in August I wrote a brief introduction to Gradle for Android developers.  In the article I promised a follow up piece outlining how Golgi can be integrated into your Gradle build. Well never let it be said that I don’t deliver – even though it is substantially later than I had planned. Let’s take a look at what additions you’ll need to add Golgi to your Android Gradle build.

androidAndGradleWithGolgi

There are three main additions to your Gradle file to use Golgi.

  1. Define some Golgi specific values.
  2. Apply the Golgi Gradle file.
  3. Include Golgi specific compile dependencies.

These values should be included in your module level build.gradle file not in your top level file – see the previous post linked at the top of this article for more details on the two build.gradle files. Let’s look at defining the Golgi specific values first. These are values that are required by the Golgi Gradle file as part of the build. The code we would use for this looks like the following.

def PROJECT_DIR = System.env.HOME + "/work/Golgi/TestProject"

project.ext {
    GOLGI_DIR = System.env.HOME + "/Golgi-Pkg/LATEST"
    GOLGI_THRIFT_FILE = PROJECT_DIR + "/GolgiTest.thrift"
    GOLGI_GEN_DIR = "src/main/java/io/golgi/example/testproject/gen"
    GOLGI_GEN_PKG = "io.golgi.example.testproject.gen"
    GOLGI_DEV_KEY_FILE = PROJECT_DIR + "/Golgi.DevKey"
    GOLGI_APP_KEY_FILE = PROJECT_DIR + "/Golgi.AppKey"
}

The first line defines a variable that isn’t technically required by Golgi. However, it will make things a bit neater so I’ve included it. The project.ext block is where are variables are really defined. GOLGI_DIR specifies where your Golgi SDK is installed. On my Mac this was in the default directory, i.e. “~/Golgi-Pkg/LATEST”. System.env.HOME allows you to reference a user’s home directory. GOLGI_THRIFT_FILE specifies where your projects Golgi Thrift file is located. GOLGI_GEN_DIR and GOLGI_GEN_PKG specify the directory where Android code generated by Golgi should be written and the package identifier that should be used for it respectively. Finally, GOLGI_DEV_KEY_FILE and GOLGI_APP_KEY_FILE specify the location of files containing your Golgi Dev and App keys – Golgi will auto-generate a GolgiKeys class that can be used to easily add the keys to your Android application. All of this information will be used to generate the Android code required by your application.

Applying the Golgi Gradle file is much easier. It can be done by adding the line:

apply from: project.GOLGI_DIR + '/Android/golgi.gradle'

This line simply instructs Gradle to include the functionality defined in the specified file. Notice we are using the project.GOLGI_DIR variable we specified in the previous step.

The last step we require is to add Golgi to Gradle’s compile dependencies list. To do this we include the following lines.

compile project.GOLGI_AAR
compile 'com.google.android.gms:play-services:5.+'

The first of these lines instructs Gradle to include a compile dependency on project.GOLGI_AAR. This dependency ensures all of your Golgi code (GolgiKeys class and Thrift data types and transactions) is auto-generated and deployed for use in your project. The second line instructs Gradle to include Google’s Play Services as a compile dependencies. This will be required if you wish to use Google Cloud Messaging push notifications in your application. Note that I have used “play-services:5.+” version in this example. This is a catch all for the latest available version of Play Services with major version 5. You can change this to load a specific version (or another major version) of Play Services if required.

And that’s it. Your Android application is now ready to build with Golgi. I’ll leave you now with the entire build.gradle file – a step by step explanation is great but sometimes I find I just want to see all the code in one place. Enjoy.

apply plugin: 'com.android.application'

def PROJECT_DIR = System.env.HOME + "/work/Golgi/TestProject"
project.ext {
    GOLGI_DIR = System.env.HOME + "/Golgi-Pkg/LATEST"
    GOLGI_THRIFT_FILE = PROJECT_DIR + "/GolgiTest.thrift"
    GOLGI_GEN_DIR = "src/main/java/io/golgi/example/testproject/gen"
    GOLGI_GEN_PKG = "io.golgi.example.testproject.gen"
    GOLGI_DEV_KEY_FILE = PROJECT_DIR + "/Golgi.DevKey"
    GOLGI_APP_KEY_FILE = PROJECT_DIR + "/Golgi.AppKey"
}

apply from: project.GOLGI_DIR + '/Android/golgi.gradle'

android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "io.golgi.example.testproject"
        minSdkVersion 15
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project.GOLGI_AAR
    compile 'com.google.android.gms:play-services:5.+'
}
ShareShare on Facebook0Share on Google+1Tweet about this on TwitterShare on LinkedIn0
Back to Articles

Leave a Reply