Cannot import google cloud endpoints client library class in Android project

青春壹個敷衍的年華 提交于 2020-01-12 03:41:06

问题


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


回答1:


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.




回答2:


This post said that there is a bug with Android Studio's Add As Library: Android Studio: IncorrectOperationException when 'Add as Library' is clicked whilst trying to configure Google Apps Endpoints client libraries

Not sure if it applies to Eclipse.




回答3:


I was able to solve this problem and have provided the solution below.

I switched to Android Studio Preview 0.4.6 from Eclipse which helped get rid of some of Googles library import issues(through I guess build.gradle config). I feel it was not a problem of Eclipse which I was using earlier.

I was able to to fix the import issues. The code on the tutorial segments on the official Google docs needs to be put in sync.

The solution is to roughly do the following changes:

In MainActivity.java, replace:

HelloGreeting with HelloworldApiGreeting 

In MainActivity.java, add the following at the top:

import android.widget.Toast; 
import android.os.AsyncTask; 
import android.util.Log 
import com.appspot.androidbackend1.helloworld.model.HelloworldApiGreeting;
import com.appspot.androidbackend1.helloworld.model.HelloworldApiGreetingCollection;

In MainActivity.java, comment the following at the top:

import com.appspot.androidbackend1.helloworld.Helloworld.Greetings.Multiply; 
import com.appspot.androidbackend1.helloworld.model.HelloGreeting;

In Application.java, add the following at the top:

import com.appspot.androidbackend1.helloworld.model.HelloworldApiGreeting;

In Application.java, replace:

HelloGreeting with HelloworldApiGreeting

Hope this saves time for others



来源:https://stackoverflow.com/questions/22303111/cannot-import-google-cloud-endpoints-client-library-class-in-android-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!