Skip to content

Instantly share code, notes, and snippets.

@rafaelloab
Last active December 28, 2015 20:29
Show Gist options
  • Save rafaelloab/7558002 to your computer and use it in GitHub Desktop.
Save rafaelloab/7558002 to your computer and use it in GitHub Desktop.
Steps to generate App Engine Backend in Android Studio
useful links
http://bradabrams.com/2013/06/google-io-2013-demo-android-studio-cloud-endpoints-synchronized-stopwatch-demo/
http://android-developers.blogspot.com.br/2013/06/adding-backend-to-your-app-in-android.html
https://code.google.com/p/gcm/
https://github.com/bradabrams/stopwatchio13
https://developers.google.com/appengine/docs/java/endpoints/getstarted/backend/configure_pom
http://developer.android.com/google/gcm/index.html
https://developers.google.com/events/io/sessions/324893448
Android Studio 0.3.5
Java JDK 7u45
Maven 3.1.1 (requer Java 7)
Android SDK Manager Extras:
Google Play Services
Google Repository
[Depracted] Google Cloud Messaging for Android Library
1. Create an android project
2. Select the project and go Tools>Google Cloud Tools> Generate App Engine Backend
Create a project in Google Cloud Console (new version)
Go APIs & auth and turn on Google Cloud Messaging for Android
Go Registered Apps and register a new app. Select that app and look for the API Key.
Now fill the form in Android Studio, the app id and project number are visible on Overview in Google Cloud Console.
3. It will generate a folder project-AppEngine in Android Studio.
At this time it build with an error, the solution found was to edit the pom.xml insider project-AppEngine folder, changing
the <appengine.target.version>1.8.0</appengine.target.version> for 1.8.7, in the gcm support page they say to use the 1.8.8 but didn't work for me. Make sure the appengine under plugins is the 1.8.7, refresh or if not working restart android studio.
After editting, refresh the maven project and run the following goals under the Maven Projects tab:
Lifecycle - compile
Plugins - appengine - appengine:endpoints_get_discovery_doc
Plugins - appengine - appengine:endpoints_get_client_lib
it should now build succesfully.
4. Run the goal appengine:devserver_start and go on you browser http://localhost:8080/_ah/api/explorer.
It should show the APIs explorer, choose one of the APIs to test and execute, you should receive a response 200 ok.
5. Now add a custom endpoint to the App Engine app. It means POJO with the following notations:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class StopWatchState {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long key;
private String sourceDevice;
private double timestamp;
private long serverTimestamp;
private boolean running;
}
6. Create a new Android Module with the name <project>-endpoints
Select that class and go Tools> Google Cloud Tools> Generate Endpoint
It will generate a classnameEndPoint class in this new project.
Now run the goal appengine:devserver_start again and look if the apis for this endpoint appears, and test it.
Add a method proteted by OAuth.
1. On google cloud console, make sure you have one aoo created for each plataform you want to access from (ios, web app, android)
2. To get a Android client ID follow the steps presented here: https://developers.google.com/appengine/docs/java/endpoints/auth#Java_Creating_OAuth_20_client_IDs
3. Edit your app engine API to look similar like this:
@Api(name = "deviceinfoendpoint",
version = "v1",
scopes = {Constants.EMAIL_SCOPE},
clientIds = {Constants.WEB_CLIENT_ID, Constants.ANDROID_CLIENT_ID, com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID},
audiences = {Constants.ANDROID_AUDIENCE},
namespace = @ApiNamespace(.............))
the clients ID you can get from console, the web client ID is automaticcally genereate when a new app is registered.
The ANDROID_AUDIENCE is the same of WEB_CLIENT_ID, and both are necessary to access the app from android clients.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment