Migrating Android projects to Cloud Endpoints Frameworks 2.0

This page describes migrating an existing Android Cloud Endpoints version 1.0 app to Endpoints Frameworks for App Engine. Note that this page refers to Endpoints version 1.0 as Endpoints Frameworks version 1.0 and new Endpoints Frameworks for App Engine as Endpoints Frameworks version 2.0.

Benefits

The new framework brings a number of benefits, including:

  • Reduced request latency
  • Better integration with App Engine features, such as custom domains.
  • Official support for Guice configurations
  • Optionally, new API management features

Endpoints Frameworks version 2.0 doesn't affect the interfaces to your API. Existing clients continue to work after migration without any client-side code changes.

Migrating Android multi-module projects to Endpoints Frameworks version 2.0

The following steps guide you through moving Android Studio Endpoints Frameworks version 1.0 projects to Endpoints Frameworks version 2.0. The guide migrates an Android Studio project with a Endpoints module.

Task list

Use the following high-level task list as you work through the migration guide. This migration guide assumes you have an existing Android project using a Google Cloud module.

  1. Before you begin.
  2. Setup the Google Cloud CLI.
  3. Optional: Download the sample code.
  4. Migrate to Endpoints Frameworks version 2.0.
  5. Deploy your backend module.
  6. Generate client libraries.

Before you begin

  1. Install Android Studio.
  2. Install Android SDK 26+.
  3. Install the gcloud CLI.

Set up the gcloud CLI

To setup the gcloud CLI:

  1. Initialize the gcloud CLI:

    gcloudinit
    
  2. Use Application Default Credentials:

    gcloudauthapplication-defaultlogin
    
  3. Install the app-engine-java component:

    gcloudcomponentsinstallapp-engine-java
    

Optional: Download the sample code

To clone the legacy and v2 sample projects from GitHub:

  1. Clone the sample repository to your local machine:

    gitclonehttps://github.com/GoogleCloudPlatform/android-docs-samples
    
  2. Change to the directory containing the sample code for legacy and v2:

    cdandroid-docs-samples/endpoints-frameworks/
    

Migrate to Endpoints Frameworks version 2.0

Update the build.gradle file

Endpoints Frameworks version 2.0 dependencies use Guava 19 and Android Gradle build plugin com.android.tools.build:gradle:2.3.3 uses Guava 18. Add Guava 19 to the build script dependencies closure of the build.gradle to override this transitive dependency.

Legacy

buildscript{
repositories{
jcenter()
}
dependencies{
classpath'com.android.tools.build:gradle:2.3.3'
}
}

v2

buildscript{
repositories{
jcenter()
}
dependencies{
// V2: Include this dependency when using Endpoints Framework v2
classpath'com.google.guava:guava:19.0'
classpath'com.android.tools.build:gradle:2.3.3'
}
}

Update the app/build.gradle file

In Endpoints version 1.0, you used the Gradle plugin, gradle-appengine-plugin, to generate an API Discovery Document. In Endpoints Frameworks version 2.0, you use the endpoints-framework-gradle-plugin to generate a Discovery Document.

This build script uses the client-side plugin, com.google.cloud.tools.endpoints-framework-client.

Legacy

applyplugin:'com.android.application'
android{
compileSdkVersion26
buildToolsVersion'26.0.1'
defaultConfig{
applicationId'com.example.migration.endpoints.app'
minSdkVersion25
targetSdkVersion26
versionCode1
versionName'1.0'
testInstrumentationRunner'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes{
release{
minifyEnabledfalse
proguardFilesgetDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
configurations.all{
resolutionStrategy.force'com.google.code.findbugs:jsr305:2.0.1'
}
}
dependencies{
compilefileTree(dir:'libs',include:['*.jar'])
// androidTestCompile compiles instrumentation tests written using Espresso
// used by Firebase Test Lab
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{
excludegroup:'com.android.support',module:'support-annotations'
})
compile'com.android.support:appcompat-v7:26.+'
compile'com.android.support.constraint:constraint-layout:1.0.1'
compile'com.google.code.findbugs:jsr305:2.0.1'
compile'com.google.http-client:google-http-client-android:1.22.0'
compile'com.google.api-client:google-api-client:+'
testCompile'junit:junit:4.12'
// LEGACY: Legacy compile :backend project
compileproject(path:':backend',configuration:'android-endpoints')
}

v2

applyplugin:'com.android.application'
// V2: Apply the new Endpoints Framework client plugin
applyplugin:'com.google.cloud.tools.endpoints-framework-client'
buildscript{
repositories{
jcenter()
}
dependencies{
// V2: Add the new Endpoints Framework plugin dependencies
classpath'com.google.cloud.tools:endpoints-framework-gradle-plugin:1.0.2'
}
}
android{
compileSdkVersion26
buildToolsVersion'26.0.1'
defaultConfig{
applicationId'com.example.migration.endpoints.app'
minSdkVersion25
targetSdkVersion26
versionCode1
versionName'1.0'
testInstrumentationRunner'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes{
release{
minifyEnabledfalse
proguardFilesgetDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
configurations.all{
resolutionStrategy.force'com.google.code.findbugs:jsr305:2.0.1'
}
}
dependencies{
compilefileTree(dir:'libs',include:['*.jar'])
// androidTestCompile compiles instrumentation tests written using Espresso
// used by Firebase Test Lab
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{
excludegroup:'com.android.support',module:'support-annotations'
})
compile'com.android.support:appcompat-v7:26.+'
compile'com.android.support.constraint:constraint-layout:1.0.1'
compile'com.google.code.findbugs:jsr305:2.0.1'
testCompile'junit:junit:4.12'
// V2: Endpoints Framework v2 migration
endpointsServerproject(path:':backend',configuration:'endpoints')
compile'com.google.api-client:google-api-client:+'
compile'com.google.http-client:google-http-client-android:1.22.0'
}

Update the backend/build.gradle file

The App Engine standard environment Gradle tooling was updated. Endpoints Frameworks version 1.0 projects used the gradle-appengine-plugin in com.google.appengine, and Endpoints Frameworks version 2.0 projects use appengine-gradle-plugin.

This build script uses the server-side plugin, com.google.cloud.tools.endpoints-framework-server.

Legacy

buildscript{
repositories{
jcenter()
}
dependencies{
// LEGACY: Deprecated App Engine plugin dependency
classpath'com.google.appengine:gradle-appengine-plugin:1.9.59'
}
}
repositories{
jcenter();
}
applyplugin:'java'
applyplugin:'war'
// LEGACY: Deprecated Apply App Engine plugin dependency
applyplugin:'appengine'
sourceCompatibility=JavaVersion.VERSION_1_7
targetCompatibility=JavaVersion.VERSION_1_7
dependencies{
// LEGACY: Deprecated App Engine plugin dependency
appengineSdk'com.google.appengine:appengine-java-sdk:1.9.59'
// LEGACY: Legacy Endpoints Framework dependencies
compile'com.google.appengine:appengine-endpoints:1.9.59'
compile'com.google.appengine:appengine-endpoints-deps:1.9.59'
compile'javax.servlet:servlet-api:2.5'
}
appengine{// LEGACY: Deprecated App Engine plugin Tasks configuration
downloadSdk=true
appcfg{
oauth2=true
// extraOptions is used for acceptance test and
// is not required.
defapplication=findProperty("appengine.deploy.application")
defversion=findProperty("appengine.deploy.version")
defserviceAccount=findProperty("appengine.deploy.serviceAccount")
extraOptions=["--application="+application,"--version="+version,
'--service_account_json_key_file='+serviceAccount]
}
endpoints{
getClientLibsOnBuild=true
getDiscoveryDocsOnBuild=true
}
}

v2

buildscript{
repositories{
jcenter()
}
dependencies{
// V2: Add the new App Engine and Endpoints Frameworks plugin dependencies
classpath'com.google.cloud.tools:endpoints-framework-gradle-plugin:1.0.2'
classpath'com.google.cloud.tools:appengine-gradle-plugin:1.3.3'
}
}
repositories{
jcenter();
}
applyplugin:'java'
applyplugin:'war'
// V2: Apply new App Engine and Endpoints Framework server plugins
applyplugin:'com.google.cloud.tools.appengine'
applyplugin:'com.google.cloud.tools.endpoints-framework-server'
sourceCompatibility=JavaVersion.VERSION_1_7
targetCompatibility=JavaVersion.VERSION_1_7
dependencies{
// V2: Endpoints Framework v2 migration
compile'com.google.endpoints:endpoints-framework:2.0.9'
compile'javax.inject:javax.inject:1'
compile'javax.servlet:servlet-api:2.5'
}
// V2: Define deployment configuration using the new App Engine plugin
// with the appengine closure
appengine{// App Engine tasks configuration
deploy{// deploy configuration
// The following is used for acceptance tests and
// is not required for a migration.
project=findProperty("appengine.deploy.project")
version=findProperty("appengine.deploy.version")
defpromoteProp=findProperty("appengine.deploy.promote")
if(promoteProp!=null){
promote=newBoolean(promoteProp)
}
}
}

Update the web.xml file

In Endpoints Frameworks version 2.0, the servlet-class changed from SystemServiceServlet to EndpointsServlet.

The URL pattern was updated from /_ah/spi/* to /_ah/api/*.

Legacy

<servlet>
<!--LEGACY:StartofLegacysection-->
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
<!--LEGACY:EndofLegacysection-->
<init-param>
<param-name>services</param-name>
<param-value>com.example.migration.endpoints.backend.MyEndpoint</param-value>
</init-param>
</servlet>
<servlet-mapping>
<!--LEGACY:Startoflegacysection-->
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
<!--LEGACY:Endoflegacysection-->
</servlet-mapping>

v2

<servlet>
<!--V2:Startofv2section-->
<servlet-name>EndpointsServlet</servlet-name>
<servlet-class>com.google.api.server.spi.EndpointsServlet</servlet-class>
<!--V2:Endofv2section-->
<init-param>
<param-name>services</param-name>
<param-value>com.example.migration.endpoints.backend.MyEndpoint</param-value>
</init-param>
</servlet>
<servlet-mapping>
<!--V2:Startofv2section-->
<servlet-name>EndpointsServlet</servlet-name>
<url-pattern>/_ah/api/*</url-pattern>
<!--V2:Endofv2section-->
</servlet-mapping>

Rebuild project

Finally, use the Android Studio build list to clean and rebuild the Android Studio project with the new dependencies.

Android Studio Rebuild

Deploy your backend module

The new Gradle App Engine plugin is now used to deploy the backend module by using the following Gradle task while in the backend module:

gradleappengineDeploy

Generate client libraries

To generate client libraries, use the following Gradle task while in the backend module:

gradleendpointsClientLibs

Learn more about the available tasks for the Endpoints Frameworks Gradle plugin.

Adding Endpoints API management

Endpoints Frameworks version 2.0 also lets you turn on API management features, including:

  • API key management
  • API sharing
  • User authentication
  • API metrics
  • API logs

To get started using these and other Cloud Endpoints Frameworks 2.0 features, go to the Java page about Endpoints Frameworks for App Engine.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026年08月26日 UTC.