Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import google cloud endpoints client library class in Android project

I'm having difficulty getting Google Cloud Endpoints working. I have an Python endpoints project running on GAE and it works perfectly using the api explorer. However I'm struggling to properly generate the client library and use it in my android app. I've tried a number of sample projects and have the same problem every time; I can't import and use the model classes from the client libraries.

Here's what I'm doing (for this example I'll use the helloworld api python sample at https://github.com/GoogleCloudPlatform/appengine-endpoints-helloendpoints-python)

  1. Unzip the sample code
  2. Generate the client library by navigating to folder and running

    <gae-sdk>\endpointscfg.py get_client_lib java helloworld_api.HelloWorldApi
    
  3. Unzip the generated folder and copy into root of project in eclipse

  4. In Eclipse add "your_app_id_appspot_com-helloworld-v1-20140310110152-java-1.17.0-rc-sources.jar" to build path (Right click the JAR> Build Path>Add to Build Path)

At this stage, I can import com.appspot.your_app_id.helloworld.model.*but I cannot import com.appspot.your_app_id.helloworld.model.Greeting

Can anyone shed any light on what's happening here? I have tried many different ways to get this to work but have the same problem every time.

Many thanks,

Tom

like image 715
Tom Avatar asked Mar 10 '14 14:03

Tom


1 Answers

The problem is that by default, the generated zip file only contains a sources jar, not an actual compiled library jar which your Android app can use.

Here's the solution:

  1. In your backend api folder (from the same place where your app.yaml is located), generate the client library as a gradle library, like so:
    <gae-sdk-path>\endpointscfg.py get_client_lib java -bs gradle helloworld_api.HelloWorldApi

  2. You'll now have a helloworld-v1.zip. Unzip this (either right here or in somewhere convenient like ~/temp)

  3. This will create a folder called helloworld, which should have a build.gradle in there along with a src folder.

  4. Build your client library using "gradle install" in this folder.

  5. Copy build/libs/helloworld-v1-1.X.X-SNAPSHOT.jar into your Android app's libs folder.

  6. Add it as a library in Android Studio by right-click/add-as-library.

  7. Your classes should now resolve correctly.

Step 4 should install the just-built client library into your local maven repository. You can follow the instructions in readme.html in the helloworld/ folder you extracted to integrate directly with your Android app's gradle build system instead of copying the jar your built manually.

like image 87
PacificSky Avatar answered Oct 23 '22 11:10

PacificSky