Build a multi-platform game and backend API in 5 minutes

Start building a multi-platform game or an Android app with backend API in 5 minutes using Android Studio and Google App Engine!

Ali Muzaffar
Google Cloud - Community
8 min readMay 24, 2015

--

IMPORTANT: This article assume that you have Java, Android SDK and Android Studio already setup and are capable of building basic Android apps.

If you aren't interested in building a game and just want to build a backend for an existing Android app, just skip ahead to the “Build a backend for your game or app in seconds” section.

Start building a multi-platform game in 5 minutes

For the purpose of this article, we will use LibGDX to build our framework for a multi-platform game. The reasons behind our choice is simple, it uses Java as the programming language of choice and it supports Gradle. Better yet, LibGDX has a setup utility that uses Gradle and will do most of the heavy lifting for us.

Setup LibGDX Project

To setup a LibGDX project, you need to head over to the LibGDX download page and download the setup App. What you download is an executable jar file. It will likely be called gdx-setup.jar. Once you have the file downloaded, double click it to start the setup or run it from the command line using the command:

java -jar gdx-setup.jar

Once you run the setup, you should see a screen as follows:

LibGDX Setup

You'll need to fill in the Name, Package, Game class, Destination path and Android SDK path. For the LibGDX Version, currently, only the latest version is selectable and from Sub Projects select all the platforms you would like your app to run on and click Generate.

If you get a warning as shown below, I would click Yes and continue. I have personally not noticed any problems using LibGDX with the latest versions of Android build tools (there is an issue with Gradle and Google App Engine but more on that later), however, if you want, you can download 20.0.0 which is the recommended version.

LibGDX 1.6.1 recommends using Android build tools v20.0.0

Notes

  • Personally, I would skip “Desktop” which effectively gives you a jar that you can use to deploy your game, however, if you want to deploy to the Windows store or Apple store, feel free to leave it selected.
  • For iOS LibGDX uses RoboVM for compatibility, if and when you are ready to deploy or test on iOS, you can download and set-up RoboVM. RoboVM is a Cross-platform mobile app development SDK for iOS and Android. It allows you to build native UI’s with full hardware access using Java.
  • For HTML5 game, LibGDX uses GWT.

When you click Generate, the setup utility will run Gradle to download the required packages and setup the project. When it’s done, you'll see something similar to the screen below.

As instructions imply, all you need to do now, is to launch Android Studio, select File > Open then select the build.gradle file under the directory you specified under the “Destination” field above.

After selecting the build.gradle file, Android Studio will spend some time building the project and downloading the dependencies it needs. When it's done, you should see a project setup similar to what you see below.

Believe it or not, you can build and run the android project on an emulator or a phone, your multi-environment game already has a starting point! If you do build and deploy the game, you'll see the following.

LibGDX Project Architecture

When you look at a LibGDX project, it’s pretty obvious that there is a “core” project which holds all your code and the other projects, whether it’s Android, iOS or HTML simply delegate work to the core project. As mentioned, iOS uses RoboVM and HTML uses GWT.

Build a back-end for your game or app in seconds using Google App Engine

This works equally well for the game we built above or for an Android app. There are tons of cloud hosting solutions, Google's App Engine, Amazon AWS and Microsoft has Azure etc. However, because of it’s amazing integration with Android Studio we will be going with Google App Engine. There is an additional benefit here of not needing a separate development environment and a separate setup of an IDE to do backend development. You can code your clients and modify your backend to suit them all in one place.

Simply right click on your project name and select New > Module

From the New Module screen, select Google Cloud Module and click Next, when you do, you should see the screen below.

The fields should be pre-filled. If you want to change any of the fields you can, however, you should familiarize yourself with the options under Module type and select the one appropriate for you. At the bottom of the screen, you have links that point you to resources that explain what each of the module types are. In short:

  • App Engine Java Servlet Module will literally set-up a good old fashioned Servlet project for you. Very little to no libraries and modules will be included by default.
  • App Engine Java Endpoints Module builds on the Servlets and employs Google Cloud Endpoints to define a RESTful backend API from very simple annotations of the server-side Java code. Google Cloud Endpoints also provide automated Java object marshalling/unmarshalling to JSON, generation of strongly-typed client libraries that can be called from your Android app, built-in authentication support and so on.
  • App Engine Backend with Google Cloud Messaging - Same as above, but adds Google Cloud Messaging.

Recommendation for module type: If you have previous backend Java experience and know the kind of backend you want to build, then I would just select Java Servlet Module and change it as you see fit. If you aren't sure, then just go with App Engine Java endpoints Module. Changes are, this will provide all the functionality you need and takes advantage of Google’s cloud engine. The rest of this tutorial assumes you are using App Engine Java endpoints Module.

When you click Finish you'll see Android Studio building your new backend project. When all is done, the backend module should be added to your project.

When doing this on a Windows machine, my build was broken. There were 2 reasons:

  1. Because under the Android app > build.gradle there was no space at the end of the file between a the closing bracket and the word “dependencies”.
//wrong
}dependencies {
compile project(path: ':backend', configuration: 'android-endpoints')
}
//correct
}
dependencies {
compile project(path: ':backend', configuration: 'android-endpoints')
}

2. And secondly, because of some known issues with the Google Cloud Plugin, you can't use the latest Gradle, unless you need the most recent versions of Gradle you can just go with 2.2.1. In order to change the Gradle version open <Project>/gradle/wrapper/gradle-wrapper.properties and change the version of gradle you use. Make sure it reads:

distributionUrl=http\://services.gradle.org/distributions/gradle-2.2.1-all.zip

When all that is done, the project should build again and you should be able to launch the android app as before.

Run the backend

From the edit configuration drop down, select backend then simply click the play button next to it to run the backend. You should see log output similar to that below

May 25, 2015 12:29:20 AM com.google.apphosting.utils.jetty.JettyLogger info
INFO: Started SelectChannelConnector@localhost:8080
May 25, 2015 12:29:20 AM com.google.appengine.tools.development.AbstractModule startup
INFO: Module instance default is running at http://localhost:8080/
May 25, 2015 12:29:20 AM com.google.appengine.tools.development.AbstractModule startup
INFO: The admin console is running at http://localhost:8080/_ah/admin
May 25, 2015 12:29:20 AM com.google.appengine.tools.development.DevAppServerImpl doStart
INFO: Dev App Server is now running

As the output suggests, if you visit localhost:8080 you should see a “Hello Endpoints” page.

That’s it! You have a starting point for your game and your backend api! How easy was that!

Accessing the backend API from your app

In order to test the backend with your app in order to verify that there is a backend api to access. You can use some code from courtesy of Google. In your Android project, create an AsyncTask.

import android.content.Context;
import android.os.AsyncTask;
import android.util.Pair;
import android.widget.Toast;

import com.example.ali.myapplication.backend.myApi.MyApi;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;

import java.io.IOException;

public class EndpointsAsyncTask extends AsyncTask<Pair<Context, String>, Void, String> {
private static MyApi myApiService = null;
private Context context;

@Override
protected String doInBackground(Pair<Context, String>... params) {
if(myApiService == null) { // Only do this once
MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
.setRootUrl("http://10.0.2.2:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
// end options for devappserver

myApiService = builder.build();
}

context = params[0].first;
String name = params[0].second;

try {
return myApiService.sayHi(name).execute().getData();
} catch (IOException e) {
return e.getMessage();
}
}

@Override
protected void onPostExecute(String result) {
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}
}

Then in your AndroidLauncher class, add the following at the end of the onCreate method:

new EndpointsAsyncTask().execute(new Pair<Context, String>(this, "Manfred"));

When you run the app while your backend is running, you should see the Toast message as shown below:

Note: The IP address 10.0.2.2 is the IP address that the Android emulator uses to access your machine. For Genymotion users, this IP address will be 10.0.3.2. For people using devices, they will have to use the IP address of their development machine.

How easy was that!

I've always preferred Eclipse over IntelliJ, my reason is simple, not everyone can afford to keep their machines up to date or recent, and Eclipse for all it’s problems uses native code and just runs faster.

That being said, I’m impressed with how far Android Studio has come as a development environment. Arguable a lot of this is thanks to Gradle and growing community behind it. I've done this same setup with Eclipse and while it was simple enough, it was not even close to as smooth as the setup with Gradle and Android Studio.

--

--

Ali Muzaffar
Google Cloud - Community

A software engineer, an Android, and a ray of hope for your darkest code. Residing in Sydney.