Tuesday, May 24, 2016

Adding a Backend to Your App In Android Studio

Posted by Sachin Kotwani, Google Cloud Platform tm

Android Studio lets you sily add a cloud backend to your appliion, right from your IDE. A backend allows you to implement functionality such as backing up user data to the cloud, serving content to client apps, rl-time interactions, sending push notifiions through Google Cloud Messaging for Android (GCM), and more. Additionally, having your appliion’s backend hosted on Google App Engine mns that you can focus on what the cloud appliion does, without having to worry about administration, reliability or scalability.

When you crte a backend using Android Studio, it erates a new App Engine appliion under the same project, and gives your Android appliion the necessary libraries and a sample activity to interact with that backend. Support for GCM is built-in, making it sy to sync data across multiple devices. Once you've erated the project, you can build and run your client and server together, in a single environment, and even deploy your backend right from Android Studio.

In this post we’ll focus on how to get started with the basic setup. From there it's sy to extend the basic setup to meet your needs.

Preliminary setup

Before you get started, make sure you take care of these tasks first:

Download Android Studio if you haven’t done so alrdy and set it up.
Make sure you have an appliion project set up in Android Studio. You can use any working app that you want to integrate with your backend, even a sample app.
If you'll be running the app on an emulator, download the Google APIs Addon from the SDK Manager and run your app on that .

Crte a Google Cloud Platform project: In the Cloud Console, crte a new project (or reuse an old one) and make note of the Project ID. Click on the words “Project ID” on the top left to toggle to the Project . Copy this as well.
Enable GCM and obtain API : In the Cloud Console, click on APIs and turn on the Google Cloud Messaging for Android API. Then, click on the “Register App” button on the top left, enter a name for the app, then select “Android” and “Accessing APIs via a web server”. In the resulting screen, expand the “Server ” box and copy the API .

1. erate an App Engine project

In Android Studio, open an existing Android appliion that you want to modify, or crte a new one. Select the Android app module under the Project node. Then click Tools> Google Cloud Endpoints> Crte App Engine Backend.

In the wizard, enter the Project ID, Project , and API of your Cloud project.

This will crte:

An App Engine project which contains the backend appliion source
An endpoints module with a RegisterActivity class, related resources, and client libraries for the Android app to communie with the backend

The erated App Engine appliion (<app_name>-AppEngine) is an Apache Maven-based project. The Maven pom.xml file takes care of downloading all the dependencies, including the App Engine SDK. This module also contains the following:

A Google Cloud Endpoint (DeviceInfoEndpoint.java, auto-erated from DeviceInfo.java) that your Android app will “register” itself through. Your backend will use that info to send a push notifiion to the device.
A sample endpoint, MessageEndpoint.java, to list previously sent GCM messages and send new ones.
A starter web frontend appliion (index.html in webapp directory) that will show all the devices that have registered with your service, and a form to send them a GCM notifiion.

The endpoints module (<app_name>-endpoints) erated for you contains the classes and libraries needed by the Android appliion to interact with the backend:

A RegisterActivity.java class that, when invoked, will go through the GCM flow and also register itself with the recently crted backend through DeviceInfoEndpoint.
Client libraries, so that the appliion can talk to the backend using an object rather than directly using raw REST calls.
XML files related to the newly crted activity.

2. Add GCM to your app

In your Android appliion, you can call RegisterActivity whenever you want the to take place (for example, from within the onCrte() method of your main activity.

import android.content.Intent;

protected void onCrte(Bundle savedInstanceState) {
Intent intent = new Intent(this, RegisterActivity.class);

3. Deploy the sample backend server

When you're rdy to deploy an update to your ( the sample ) production backend in the cloud, you can do that sily from the IDE. Click on the “Maven Projects” button on the right edge of the IDE, under Plugins> App Engine, right-click and run the appengine:update goal.

As soon as the update is deployed, you can also access your endpoints through the APIs Explorer at http://<project-id>.appspot.com/_ah/api/explorer.

For testing and debugging, you can also run your backend server locally without having to deploy your changes to the production backend. To run the backend locally, just set the value of LOCAL_ANDROID_RUN to true in CloudEndpointUtils.java in the App Engine module.

4. Build and run the Android app

Now build and run your Android app. If you called RegisterActivity from within your main activity, the device will register itself with the GCM service and the App Engine app you just deployed. If you are running the app on an emulator, note that GCM functionality requires the Google APIs Addon , which you can download from the SDK Manager.

You can access your sample web console on any browser at http://<project-id>.appspot.com. There, you will see that the app you just started has registered with the backend. Fill out the form and send a message to see GCM in action!

Extending the basic setup

It's sy to expand your cloud services right in Android Studio. You can add new server-side and through Android Studio instantly erate your own custom endpoints to access those services from your Android app.

Join the discussion on

+Android Developers

No comments:

Post a Comment