AWS Mobile Hub Developer Guide
Developer Guide

Android: Amazon Machine Learning

The following reference content only applies to existing apps that were built using the AWS Mobile SDKs for iOS and Android. If you’re building a new mobile or web app, or you're adding cloud capabilities to an existing app, visit the Amplify Framework website instead. Documentation for the AWS Mobile SDKs for iOS and Android is now part of the Amplify Framework.

Amazon Machine Learning (ML) is a service that makes it easy for developers of all skill levels to use machine learning technology. The SDK for Android provides a simple, high-level client designed to help you interface with Amazon Machine Learning service. The client enables you to call Amazon ML's real-time API to retrieve predictions from your models and enables you to build mobile applications that request and take actions on predictions. The client also enables you to retrieve the real-time prediction endpoint URLs for your ML models.



You must complete all of the instructions on the Set Up the SDK for Android page before beginning this tutorial.

Granting Access to Amazon Machine Learning Resources

To use Amazon Machine Learning in an application, you must set the proper permissions. The following IAM policy allows the user to perform the actions shown in this tutorial on two actions identified by ARN.

{ "Statement": [{ "Effect": "Allow", "Action": [ "machinelearning:GetMLModel", "machinelearning:Predict" ], "Resource": "arn:aws:machinelearning:use-east-1:11122233444:mlmodel/example-model-id" }] }

This policy should be applied to roles assigned to the Amazon Cognito identity pool, but you will need to replace the Resource value with the correct account ID and ML Model ID. You can apply policies at the IAM console. To learn more about IAM policies, see Introduction to IAM.

Add Import Statements

Add the following imports to the main activity of your app:


Initialize AmazonMachineLearningClient

Pass your initialized Amazon Cognito credentials provider to the AmazonMachineLearningClient constructor:

Android - JavaAndroid - Kotlin
Android - Java
AmazonMachineLearningClient client = new AmazonMachineLearningClient(credentialsProvider);
Android - Kotlin
val client = AmazonMachineLearningClient(credentialsProvider)

Create an Amazon Machine Learning Client

Making a Predict Request

Prior to calling Predict, make sure you have not only a completed ML Model ID but also a created real-time endpoint for that ML Model ID. This cannot be done through the mobile SDK; you will have to use the Machine Learning Console or an alternate SDK. To validate that this ML can be used for real-time predictions:

Android - JavaAndroid - Kotlin
Android - Java
// Use a created model that has a created real-time endpoint String mlModelId = "example-model-id"; // Call GetMLModel to get the realtime endpoint URL GetMLModelRequest getMLModelRequest = new GetMLModelRequest(); getMLModelRequest.setMLModelId(mlModelId); GetMLModelResult mlModelResult = client.getMLModel(getMLModelRequest); // Validate that the ML model is completed if (!mlModelResult.getStatus().equals(EntityStatus.COMPLETED.toString())) { System.out.println("ML Model is not completed: " + mlModelResult.getStatus()"); return; } // Validate that the realtime endpoint is ready if (!mlModelResult.getEndpointInfo().getEndpointStatus().equals(RealtimeEndpointStatus.READY.toString())){ System.out.println("Realtime endpoint is not ready: " + mlModelResult.getEndpointInfo().getEndpointStatus()); return; }
Android - Kotlin
// Call GetMLModel to get the realtime endpoint URL val modelRequest = new GetMLModelRequest() modelRequest.mLModelID = "example-model-id" val modelResult = client.getMLModel(modelRequest); // Validate that the ML model is completed if (modelResult.status != EntityStatus.COMPLETED.toString()) { Log.d(TAG, "ML Model is not completed: ${modelResult.status}"); return; } // Validate that the realtime endpoint is ready if (modelResult.endpointInfo.endpointStatus != RealtimeEndpointStatus.READY.toString()) { Log.d(TAG, "Realtime endpoint is not ready: ${modelResult.endpointInfo.endpointStatus}"); return; }

Once the real-time endpoint is ready, we can begin calling Predict. Note that you must pass the real-time endpoint through the PredictRequest.

Android - JavaAndroid - Kotlin
Android - Java
// Create a Predict request with your ML model ID and the appropriate Record mapping PredictRequest predictRequest predictRequest = new PredictRequest(); predictRequest.setMLModelId(mlModelId); HashMap<String, String> record = new HashMap<String, String>(); record.put("example attribute", "example value"); predictRequest.setRecord(record); predictRequest.setPredictEndpoint(mlModelResult.getEndpointInfo().getEndpointUrl()); // Call Predict and print out your prediction PredictResult predictResult = client.predict(predictRequest); Log.d(LOG_TAG. predictResult.getPrediction()); // Do something with the prediction // ...
Android - Kotlin
// Create a Predict request with your ML model ID and the appropriate Record mapping val predictRequest predictRequest = PredictRequest().apply { mLModelID = "example-model-id" record = mapOf("example attribute" to "example value") predictEndpoint = modelResult.endpointInfo.getEndpointUrl } val predictResult = client.predict(predictRequest) Log.d(LOG_TAG, predictResult.prediction) // Do something with the prediction // ...

Additional Resources