Gradle Tutorial : Part 7 : Android Studio + App Engine + Gradle

Welcome to Part 7 of the Gradle Tutorial. In the previous episode i.e. Part 6, we took a first look at understanding the Gradle build files that are generated for a basic Android application inside of Android Studio.

LAST UPDATE : January 22, 2015 :
i) Updated Screenshots for latest version of Android Studio 1.0.2
ii) Updated app/build.gradle as per the latest version of Android Studio 1.0.2

In this part of the tutorial, we shall expand on what we did in the last episode. We will continue to be with our basic Android application that has been generated in Android Studio. In addition, we are going to look at adding another module to this Android project. In other words, we are going to take a look at multi-module Java project inside of Android Studio and our knowledge from Part 3, where we looked at multi-module Java projects will help us.

We looked at App Engine Gradle plugin in Part 5 and in this episode, the second module that we shall be adding to our Android Project will be an App Engine module. Yes, you read that right. Android Studio has good support for App Engine modules, such that if you are looking at writing a backend for your Android Application, this can be a great choice and you can do all of that inside of Android Studio.

Our goal in this episode will only be to familiarize ourselves with an Android Project in Android Studio that has 2 modules : Android App and the App Engine App. This is not an tutorial for learning, either Android or App Engine.

gradle

This part assumes that you have installed Android Studio on your development machine and that you are familiar with basic Gradle commands, project structure and files as we have seen in the series so far.

I have used Android Studio (Beta) 1.0.2 on Windows for this blog post. It is not necessary to have the same version or the OS, things should be pretty much similar from a Gradle perspective.

The areas that we shall cover in this blog post are as follows:

  • Begin with a default Android application in Android Studio. We shall not cover the basics of the gradle files for this. We saw that in the previous episode i.e. Part 6. Please read that.
  • Add an App Engine module to the Android Project i.e. convert the Android Project into a multi-module project i.e. Android App and App Engine App
  • Study the Gradle files for the multi-module project that we now have
  • Cover some basic operations of dealing with App Engine within Android Studio. This is the Android Studio version of the stuff that we tried out via the App Engine Gradle plugin in Part 5.

Android Application

All right, lets go ahead and create/generate our Android Application. We are going to be using the Android Studio Wizard to do that. Nothing fancy.

Click on Start a new Android Studio project in the Quick Start dialog. This should bring up the New Project wizard as shown below. I have stuck to tradition and called my project HelloWorldAndroid 🙂

part6-3

Just take all the defaults as shown in the next screens for the wizard.

gradle-ep6-5

gradle-ep6-6

part6-4

Click on Finish to end the wizard and then it weaves some magic to generate the Android Studio project files for you. Be a bit patient.

Add an App Engine Module

Now that we have an Android project and an Android app module within the project i.e. app, let us move forward and make this a multi-module project by adding an App Engine module to this project.

To add a new module to your project, simply select File –> New Module. This will bring up the dialog as shown below. Select Google Cloud Module as shown below and click on Next.

part7-1

 

This will bring up Configure your new Google Cloud module dialog as shown below:

part7-2


Current, there are three Google Cloud Module types that are supported:

  • App Engine Java Servlet module
  • App Engine Java Endpoints module
  • App Engine Backend with Google Cloud Messaging.

For now, we shall just take the most basic one i.e. App Engine Java Servlet Module as the type. Additionally, I am naming it serverapp and I give it a package name for my Java code.

This is the same basic template that you would have got if you were using Eclipse and had generated a new Web Application using the Google Eclipse plugin. 

Click on Finish to generate the module. The module will be generated and added to the Project.

Once that is done, you will find that you have now an additional module named serverapp in the Project hierarchy as shown below:

ep7-4

You will also notice that you can now Run both the Android App and the App Engine if you want to. Just check out the Run option as shown below:

ep7-6

Gradle Files

In Part 3, we had covered multiple Java projects and how at the root folder, we can specify the settings.gradle and build.gradle files that apply across all the modules.

Let us recap, what we have so far:

  • We have an Android Studio Project
  • It has 2 modules
  • One module (app) is the Android app
  • One module (serverapp) is the App Engine app

So, let us look at the settings.gradle file at the root:

include ':app', ':serverapp'

It includes now both the app module and the serverapp module, which is how we had seen in Part 3 of this series.

Take a look at the build.gradle file at the root:

// Top-level build file where you can add configuration options 
// common to all sub-projects/modules.

buildscript {
 repositories {
   jcenter()
 }

dependencies {
 classpath 'com.android.tools.build:gradle:1.0.0'

// NOTE: Do not place your application dependencies here; they 
// belong in the individual module build.gradle files
 }
}

allprojects {
  repositories {
    jcenter()
  }
}

No surprises here. This should be understandable too. For allprojects, we are specifying the repository to be used. JCenter is another repository similar to MavenCentral that we had seen earlier. Additionally, we had seen in the earlier episode, how buildscript can be used to specify what is going to be needed to run the script itself. At that time, we had included the App Engine Plugin/SDK and now we have the Gradle library for the Android Tools Project.

app module – build.gradle

This section is a repeat from the previous episode:

Let us now, step inside of the app module. There you will find the module specific build.gradle that is shown below. Remember it is an Android app, so it will utilize the Android plugin and have Android plugin specific closures that make sense when compiling, building , running, packaging the Android application.

The app/build.gradle file is shown below:

apply plugin: 'com.android.application'

android {
  compileSdkVersion 21
  buildToolsVersion "21.1.2"

  defaultConfig {
     applicationId "com.mindstorm.helloworldandroid"
     minSdkVersion 19
     targetSdkVersion 21
     versionCode 1
     versionName "1.0"
  }

  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
    }
 }

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 compile 'com.android.support:appcompat-v7:21.0.3'
}

Some of the important points are:

  • We specify the use of the Android plugin via the apply plugin command
  • Take a look at dependencies closure that we have seen earlier. Currently it simply adds all the JAR Files that you might add to the libs folder. Alternately, you can also add more of your dependencies here as we have seen earlier.
  • The android closure is a standard one that is used for specifying which SDK version to use for compiling, the minimum SDK Version supported, package name, versioning information and pro Guard related stuff. If you have built Android applications, this will be familiar. If not, you need to do a 101 course on Android Development and this stuff will be clear to you.

serverapp module – build.gradle

Let us now, step inside of the serverapp module. There you will find the module specific build.gradle that is shown below. Remember, this one is an App Engine application, so it will utilize the App Engine plugin. We had seen a similar looking build.gradle file in Part 5 of the series.

The serverapp/build.gradle file is shown below:

// If you would like more information on the gradle-appengine-plugin please refer to the github page
// https://github.com/GoogleCloudPlatform/gradle-appengine-plugin

buildscript {
 repositories {
 mavenCentral()
 }
 dependencies {
 classpath 'com.google.appengine:gradle-appengine-plugin:1.9.14'
 }
}

repositories {
 mavenCentral();
}

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
dependencies {
 appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.14'
 compile 'javax.servlet:servlet-api:2.5'
}

appengine {
 downloadSdk = true
 appcfg {
 oauth2 = true
 }
}

Some of the important points are:

  • The appengine plugin is required by the buildscript, so we are including it in the buildscript closure. This is same from Part 5.
  • We are applying the plugins : java, war and appengine. This is standard stuff again since we have java files, war tasks are needed to build/assemble the package into a WAR file and appengine plugins adds several tasks that include running local dev server, uploading your application to appengine runtime in the cloud, etc.
  • Take a look at dependencies closure that we have seen earlier. It includes the App Engine SDK 1.9.6 and also the Servlet jar , since this is a basic JAva Web app and we require the JAR to compile our Servlet classes. Take a peek into src/main/java folder and you will get it why.
  • And finally we have the appengine closure. We looked at that too in Part 5. The downloadSDK downloads the App Engine SDK in case you do not have it locally in the cache. Additionally, we are using the OAuth 2 mechanism in case you wish to deploy your project via the appengineUpdate task. This will open up a browser window, where you authenticate, get a code and paste it back to proceed with update of your application in to the cloud.

That’s about it really as far as the Gradle stuff is concerned. Once you understand the multi-module projects in Gradle, it becomes that much simple to understand the files that are getting generated in Android Studio vis-a-vis Gradle.

Gradle Tasks

Since we have now two modules in the application and each of them utilizes the respective plugins for Android and App Engine, you will notice that the Gradle tab found in the vertical navigation bar on the right will have tasks categorized based on both the modules.

ep7-3

Since we added the serverapp module, you can see that various App Engine tasks are now available. The Gradle support inside of Android Studio has done a gradle tasks in the background for both the folders/modules and presented the tasks here for you.

ep7-5

To execute any of the tasks, without the hassle of going to the terminal and execute it, simply double click on the tasks and it will start executing.

Keep in mind that anytime you make any changes to your build files, do remember to click on the Sync Project with Gradle Files as shown below. It should launch the build and you should be able to see the Gradle commands getting executed in the Gradle Console.

gradle-ep6-11

App Engine – Start / Stop Application

If you develop App Engine applications, then you will end up starting / stopping the local server. We had seen this in Part 5 via the App Engine plugin for gradle. Over here we can do that right within the terminal itself. Remember that as mentioned in the previous section, you can always double click on one of the Gradle Tasks and it should launch up. But the Terminal gives you good control over things and it sort of reinforces our understanding from previous episodes and hence I am covering that here. 

To start the Terminal, simply click on the Terminal Option that you will see in the status bar at the bottom. It will launch up a Terminal window for you and the directory will be shown.

ep7-7

When it opens up, it does so within the app module. I just go back one level to the root folder where the gradle wrapper files (gradlew and gradle) are present.

Remember that the task appengineRun was used to launch the App Engine dev server locally. We are simply firing up that task over here. Since it is inside the serverapp module, we are providing the whole namespace here. i.e. modulename:taskname

This will launch the App Server as shown below:

ep7-8

Once the Local Dev Server is started, we can test out if it is running by navigating to the localhost:8080 URL as shown below. And all should work well.

ep7-9

ep7-10

 

You can always open up another Terminal window by clicking on the sign. Over here, we open another terminal and actually fire the appengineStop task to shutdown the Dev Server when we want.

ep7-11

Recommended: Do try out other appengine Tasks directly from the Terminal and or by clicking on the tasks in the Gradle View.

Moving forward

This tutorial helped you understand a multi-module Android project within Android Studio. We took our existing knowledge of Gradle for multi-module Java projects and applied it to understand the files that were generated for both the Android and App Engine module within a particular Android Studio project.

In the next episode, we shall continue keep exploring the multi-module project inside of App Engine. Specifically, we shall look at another App Engine module Endpoints that helps you create a backend for your Android applications.

Till then, Happy Gradling!

Advertisements

10 thoughts on “Gradle Tutorial : Part 7 : Android Studio + App Engine + Gradle

  1. On the terminal running serverapp by gradlew serverapp:appengineRun command, FAILED due to port 8080 already used. Where can I change the port number?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s