且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法在Android项目中导入Google云端点客户端库类

更新时间:2023-09-18 11:37:52

问题是,默认情况下,生成的zip文件只包含一个源jar,而不是您的Android应用程序可以使用的实际编译库jar。



这是解决方案:


  1. 在您的后端api文件夹(从您的应用程序的同一个地方) .yaml位于),生成客户端库作为一个gradle库,如下所示:_
    < gae-sdk-path> \endpointscfg.py get_client_lib java -bs gradle helloworld_api.HelloWorldApi


  2. 你现在将有一个helloworld-v1.zip。解压缩(在这里或在〜/ temp的某个方面)


  3. 这将创建一个名为helloworld的文件夹,其中应该有一个build.gradle和src文件夹。


  4. 使用此文件夹中的gradle install构建您的客户端库。


  5. build / libs / helloworld-v1-1.XX-SNAPSHOT.jar 复制到您的Android应用程序的libs文件夹中。 p>


  6. 通过右键单击/ add-as-library将其添加到Android Studio中的库。


  7. 您的课程现在应该正确解决。


步骤4应该将刚构建的客户端库安装到本地maven仓库。您可以按照您提取的helloworld /文件夹中的readme.html中的说明直接与您的Android应用程序的gradle构建系统集成,而不是复制您手动构建的jar。


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

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.