Menu
Amazon API Gateway
Developer Guide

Use an Android SDK Generated by API Gateway

In this section, we will outline the steps to use an Android SDK generated by API Gateway of an API. Before proceeding further, you must have already completed the steps in Generate SDKs for an API Using the API Gateway Console.

Note

The generated SDK is not compatible with Android 4.4 and earlier. For more information, see Known Issues.

To install and use an Android SDK Generated by API Gateway

  1. Extract the contents of the API Gateway-generated .zip file that you downloaded earlier.

  2. Download and install Apache Maven (preferably version 3.x).

  3. Download and install the JDK (preferably version 1.7 or later).

  4. Set the JAVA_HOME environment variable.

  5. Run the mvn install command to install the compiled artifact files to your local Maven repository. This creates a target folder containing the compiled SDK library.

  6. Copy the SDK file (the name of which is derived from the Artifact Id and Artifact Version you specified when generating the SDK, e.g., simple-calcsdk-1.0.0.jar) from the target folder, along with all of the other libraries from the target/lib folder, into your project's lib folder.

    If you use Android Studio, create a libs folder under your client app module and copy the required .jar file into this folder. Verify that the dependencies section in the module's gradle file contains the following.

    Copy
    compile fileTree(include: ['*.jar'], dir: 'libs') compile fileTree(include: ['*.jar'], dir: 'app/libs')

    Make sure no duplicated .jar files are declared.

  7. Use the ApiClientFactory class to initialize the API Gateway-generated SDK. For example:

    Copy
    ApiClientFactory factory = new ApiClientFactory(); // Create an instance of your SDK. final MyApiClient client = factory.build(MyApiClient.class); // Invoke a method: // If the API exposes a 'GET /?a=1&b=2&op=+' method, you can call the following SDK method: // Result output = client.rootGet("1", "2", "+"); // where Result is the SDK class of the like-named API model. // // If the API exposes a 'GET /{a}/{b}/{op}' method, you can call the following SDK method to invoke the request, Result.output = client.aBOpGet(a, b, c); // where a, b, c can be "1", "2", "add", respectively. // You can call the following SDK methods invoke this POST API method: // POST / // host: ... // Content-Type: application/json // // { "a": 1, "b": 2, "op": "+" } Input body = new Input(); input.a=1; input.b=2; input.op="+"; Result output = client.rootPost(body); // Parse the result: // If the 'Result' object is { "a": 1, "b": 2, "op": "add", "c":3"}, you retrieve the result 'c') as String result=output.c;
  8. To use an Amazon Cognito credentials provider to authorize calls to your API, use the ApiClientFactory class to pass a set of AWS credentials by using the SDK generated by API Gateway, as shown in the following example.

    Copy
    // Use CognitoCachingCredentialsProvider to provide AWS credentials // for the ApiClientFactory AWSCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider( context, // activity context "identityPoolId", // Cognito identity pool id Regions.US_EAST_1 // region of Cognito identity pool }; ApiClientFactory factory = new ApiClientFactory() .credentialsProvider(credentialsProvider);
  9. To set an API key by using the API Gateway- generated SDK, use code similar to the following.

    Copy
    ApiClientFactory factory = new ApiClientFactory() .apiKey("YOUR_API_KEY");