Cloud World

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 26 June 2013

Tutorial: Adding a cloud backend to your application with Android Studio

Posted on 06:00 by Unknown
Android Studio lets you easily add a cloud backend to your application, 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, real-time interactions, sending push notifications throughGoogle Cloud Messaging for Android, and more. Additionally, having your application’s backend hosted on Google App Engine means that you can focus on what the cloud application does, without having to worry about administration, reliability or scalability.



When you create a backend using Android Studio, it generates a new App Engine application under the same project, and gives your Android application the necessary libraries and a sample activity to interact with that backend. Support for GCM is built-in, making it easy to sync data across multiple devices. Once you've generated the project, you can

build and run your client and server code together, in a single environment, and even deploy your backend code right from Android Studio.



In this post we’ll focus on how to get started with the basic setup. From there it's easy 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 already and set it up.

  • Make sure you have an application 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 image.

  • Create a Google Cloud Platform project: In the Cloud Console, create 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 Number. Copy this as well.


  • Enable GCM and obtain API Key: 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 Key” box and copy the API key.


  • 1. Generate an App Engine project

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

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



    This will create:

    • An App Engine project which contains the backend application source

    • An endpoints module with a RegisterActivity class, related resources, and client libraries for the Android app to communicate with the backend

    The generated App Engine application (<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-generated from DeviceInfo.java) that your Android app will “register” itself through. Your backend will use that registration info to send a push notification to the device.

    • A sample endpoint, MessageEndpoint.java, to list previously sent GCM messages and send new ones.

    • A starter web frontend application (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 notification.



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

    • A RegisterActivity.java class that, when invoked, will go through the GCM registration flow and also register itself with the recently created backend through DeviceInfoEndpoint.

    • Client libraries, so that the application can talk to the backend using an object rather than directly using raw REST calls.

    • XML files related to the newly created activity.

    2. Add GCM registration to your app

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

    ...
    import android.content.Intent;
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    ...
    Intent intent = new Intent(this, RegisterActivity.class);
    startActivity(intent);
    }

    3. Deploy the sample backend server

    When you're ready to deploy an update to your ( the sample ) production backend in the cloud, you can do that easily 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 image, 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 easy to expand your cloud services right in Android Studio. You can add new server-side code and through Android Studio instantly generate your own custom endpoints to access those services from your Android app.



    - Posted by Sachin Kotwani, Product Manager
    Email ThisBlogThis!Share to XShare to Facebook
    Posted in | No comments
    Newer Post Older Post Home

    0 comments:

    Post a Comment

    Subscribe to: Post Comments (Atom)

    Popular Posts

    • Tutorial: Adding a cloud backend to your application with Android Studio
      Android Studio lets you easily add a cloud backend to your application, right from your IDE. A backend allows you to implement functionality...
    • A Day in the Cloud, new articles on scaling, and fresh open source projects for App Engine
      The latest release of Python SDK 1.2.3, which introduced the Task Queue API and integrated support for Django 1.0, may have received a lot ...
    • New Admin Console Release
      Posted by Marzia Niccolai, App Engine Team Today we've released some new features in our Admin Console to make it easier for you to mana...
    • JPA/JDO Java Persistence Tips - The Year In Review
      If you’re developing a Java application on App Engine you probably already know that you can use JPA and JDO, both standard Java persistence...
    • The new Cloud Console: designed for developers
      In June, we unveiled the new Google Cloud Console , bringing together all of Google’s APIs, Services, and Infrastructure in a single interfa...
    • Best practices for App Engine: memcache and eventual vs. strong consistency
      We have published two new articles about best practices for App Engine. Are you aware of the best ways to keep Memcache and Datastore in syn...
    • Pushing Updates with the Channel API
      If you've been watching Best Buy closely, you already know that Best Buy is constantly trying to come up with new and creative ways to...
    • Outfit 7’s Talking Friends built on Google App Engine, recently hit one billion downloads
      Today’s guest blogger is Igor Lautar, senior director of technology at Outfit7 (Ekipa2 subsidiary), one of the fastest-growing media enterta...
    • Bridging Mobile Backend as a Service to Enterprise Systems with Google App Engine and Kinvey
      The following post was contributed by Ivan Stoyanov , VP of Engineering for Kinvey, a mobile Backend as a Service provider and Google Cloud ...
    • Easy Performance Profiling with Appstats
      Since App Engine debuted 2 years ago, we’ve written extensively about best practices for writing scalable apps on App Engine. We make writ...

    Categories

    • 1.1.2
    • agile
    • android
    • Announcements
    • api
    • app engine
    • appengine
    • batch
    • bicycle
    • bigquery
    • canoe
    • casestudy
    • cloud
    • Cloud Datastore
    • cloud endpoints
    • cloud sql
    • cloud storage
    • cloud-storage
    • community
    • Compute Engine
    • conferences
    • customer
    • datastore
    • delete
    • developer days
    • developer-insights
    • devfests
    • django
    • email
    • entity group
    • events
    • getting started
    • google
    • googlenew
    • gps
    • green
    • Guest Blog
    • hadoop
    • html5
    • index
    • io2010
    • IO2013
    • java
    • kaazing
    • location
    • mapreduce
    • norex
    • open source
    • partner
    • payment
    • paypal
    • pipeline
    • put
    • python
    • rental
    • research project
    • solutions
    • support
    • sustainability
    • taskqueue
    • technical
    • toolkit
    • twilio
    • video
    • websockets
    • workflows

    Blog Archive

    • ▼  2013 (143)
      • ►  December (33)
      • ►  November (15)
      • ►  October (17)
      • ►  September (13)
      • ►  August (4)
      • ►  July (15)
      • ▼  June (12)
        • Google Cloud Platform Powers Minyanville’s Buzz an...
        • Tutorial: Adding a cloud backend to your applicati...
        • Get Coding Faster Thanks to Little Green Buttons
        • Enabling Google App Engine to run in the Private C...
        • Enabling offline disk import for Google Cloud Storage
        • Google App Engine 1.8.1 Released
        • Cube Slam meets Google Cloud Platform
        • Google BigQuery gets bigger, faster, and smarter w...
        • Building Google Apps Extensions running on Google ...
        • Bridging Mobile Backend as a Service to Enterprise...
        • Cloud SQL API: YOU get a database! And YOU get a d...
        • Get your mobile application backed by the cloud wi...
      • ►  May (15)
      • ►  April (4)
      • ►  March (4)
      • ►  February (9)
      • ►  January (2)
    • ►  2012 (43)
      • ►  December (2)
      • ►  November (2)
      • ►  October (8)
      • ►  September (2)
      • ►  August (3)
      • ►  July (4)
      • ►  June (2)
      • ►  May (3)
      • ►  April (4)
      • ►  March (5)
      • ►  February (3)
      • ►  January (5)
    • ►  2011 (46)
      • ►  December (3)
      • ►  November (4)
      • ►  October (4)
      • ►  September (5)
      • ►  August (3)
      • ►  July (4)
      • ►  June (3)
      • ►  May (8)
      • ►  April (2)
      • ►  March (5)
      • ►  February (3)
      • ►  January (2)
    • ►  2010 (38)
      • ►  December (2)
      • ►  October (2)
      • ►  September (1)
      • ►  August (5)
      • ►  July (5)
      • ►  June (6)
      • ►  May (3)
      • ►  April (5)
      • ►  March (5)
      • ►  February (2)
      • ►  January (2)
    • ►  2009 (47)
      • ►  December (4)
      • ►  November (3)
      • ►  October (6)
      • ►  September (5)
      • ►  August (3)
      • ►  July (3)
      • ►  June (4)
      • ►  May (3)
      • ►  April (5)
      • ►  March (3)
      • ►  February (7)
      • ►  January (1)
    • ►  2008 (46)
      • ►  December (4)
      • ►  November (3)
      • ►  October (10)
      • ►  September (5)
      • ►  August (6)
      • ►  July (4)
      • ►  June (2)
      • ►  May (5)
      • ►  April (7)
    Powered by Blogger.

    About Me

    Unknown
    View my complete profile