从 Android 客户端调用后端 API

本页面介绍 Android 客户端如何调用使用 Cloud Endpoints Frameworks 为 App Engine 构建的后端 API。

生成客户端库

您的 Android 客户端需要从客户端使用的后端 API 生成的客户端库。如果您还没有客户端库,请参阅生成客户端库了解详情。本文档中的步骤说明了如何将客户端库添加到 Android 客户端项目。

设置项目

在以下说明中,您需使用 Android Studio。 如果您还没有这样做,则需要设置 Android Studio 以支持使用该框架的客户端。

配置项目

在 Android Studio 中,您的项目会将 build.gradle 文件用于依赖项和其他设置。默认情况下,Android Studio 会创建一个父项目级 build.gradle 文件,并在 Android 模块中创建一个 Android 应用专属的此文件。以下说明适用于 Android 模块中应用专属的 build.gradle。

要配置 build.gradle,请执行以下操作:

  1. 双击 build.gradle 将其打开。

  2. 修改此文件,使其包含以下几行的内容:

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.0.0'
        }
    }
    apply plugin: 'com.android.application'
    
    repositories {
        mavenCentral()
        mavenLocal()
    }
    
    android {
        compileSdkVersion 26
    
        defaultConfig {
            applicationId "com.mycompany.myapp"
            minSdkVersion 17
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        packagingOptions {
            exclude 'META-INF/DEPENDENCIES'
        }
    }
    
    dependencies {
        implementation 'com.android.support:appcompat-v7:26.1.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        // BEGIN Google APIs
        // Play Services will validate the application prior to allowing OAuth2 access.
        implementation 'com.google.