Saturday, December 21, 2013

Minimalistic GAE Endpoints in Android Studio

The goal

The goal is to remove deprecated code that is auto-generated by Android Studio.  I only want my entities in the project, no depreciated dependencies.  I will add push notifications through Google Cloud Messaging library.

Q/A

Q: Which code is deprecated?
A: Any generated code that depends on com.google.android.gcm is deprecated.

Generating initial backend and client libraries

  1. Open an existing Android project
    • Let's call it Project
    • Let's assume that the package name is com.example.package
  2. Generate App Engine Backend
    • Tools -> Google Cloud Tools -> Generated App Engine Backend
    • This will create two projects:
      • Project-AppEngine
      • Project-endpoints

Cleanup

  1. Delete
    • Under Project-AppEngine
      • google_generated
      • target
    • Under Project-AppEngine/src/main/java/com.example.package
      • Device*.java
      • Message*.java
    • From Project-AppEngine/src/main/webapp/index.html
      • scripts
      • content of the <body/> tag
      • add something like <h1>Sample Project</h1> under <body/>
    • Under Project-endpoints/src/endpoint-src/java
      • com.example.package
    • Under Project-endpoints/src/main
      • com.example.package
    • From Project-endpoints/src/main/AndroidManifest.xml
      • GCMIntentService
      • GCMBroadcastReceiver
      • RegisterActivity
      • More stuff can be removed from this file, like unused permissions.
    • From Project-endpoints/build.gradle
      • compile files('libs/gcm.jar')
      • more dependencies can be removed from this file
    • Under Project-endpoints/libs
      • gcm.jar

Adding Entities and deploying

  1. Add a test entity under Project-AppEngine/src/main/java/com.example.package
    • Test.java
  2. Generate TestEndpoint.java
    • Select Test.java
    • Tools -> Google Cloud Tools -> Generate Endpoint
  3. Deploy the project to GAE 
    • Maven Projects -> Project-AppEngine/Plugins/appengine/appengine:update
  4. At this point you can interact with TestEndpoint in APIs Explorer

Client libraries

  1. Generate client libraries
    • Select Project-AppEngine
    • Tools -> Google Cloud Tools ->  Generate Client Libraries
  2. Synchronize the project (refresh)
    • This step creates
      • Project-AppEngine/google_generated
      • Project-AppEngine/target/Project-
        AppEngine-1.0
    • Nothing is yet created under Project-endpoints/src/endpoint-src/java
  3. Generate client libraries - AGAIN!
    • Select Project-AppEngine
    • Tools -> Google Cloud Tools ->  Generate Client Libraries
  4. Synchronize the project (refresh) - AGAIN!
  5. Look under Project-endpoints/src/endpoint-src/java/com.example.project.testendpoing
    • On the second attempt the client code for TestEndpoint is generated
    • Now this code can be used in the original Android Project to communicate with GAE.

Notes

When you add a new entity under Project-AppEngine/src/main/java/com.example.package, the step of generating client libraries has to be performed twice, at least at the moment.
  • First run will modify Project-AppEngine
  • Second run will generate client code under Project-endpoints
Hopefully this will be fixed in the future.

No comments:

Post a Comment