Connecting Java mobile apps to AWS cloud services


Java developers are using Gluon Mobile and Gluon CloudLink to bind mobile apps to cloud services and to leverage functionality offered in enterprise environments.

We have been writing about integrations with a number of cloud services before. Today, we will show how you can integrate your Java mobile apps with the
Amazon AWS Mobile Hub.

Using the AWS Mobile Hub, mobile apps can leverage AWS cloud services in a very convenient way. AWS provides a number of cloud services that many developers are familiar with.
In the following article, we will show how you can connect a Java mobile app via the AWS Mobile Hub to the Amazon S3 service for downloading and uploading content.

In general, when connecting a mobile app via the AWS Mobile Hub to the AWS cloud services, an SDK is needed.
We forked the AWS SDK for Android and created a Java Mobile SDK that works with Java Mobile apps on iOS, Android and embedded. The source code for this SDK, that allows to connect Java mobile apps to the AWS Mobile Hub is available at Github.

One of the advantages of the AWS Mobile Hub is that it provides developers with a single point of access to some of the AWS cloud services. In order to achieve this, the client needs to authenticate itself with the AWS Mobile Hub where credentials and permissions are checked.

In order to get started, you have to follow the setup scenario and create a project at the AWS Mobile Hub. If you don’t have an AWS account yet, you can easily create a free trial account that will get you started.
For the app platform, you can select “Android” but don’t worry, it will work on iOS as well with exactly the same Java code.

Since we want to use the AWS S3 service, you have to enable the “User File Storage” tile, as explained in the docs.
Once you have done that, you can download the cloud configuration file named awsconfiguration.json.

We have a very simple sample that contains all the code that is needed to authenticate with the AWS Mobile Hub and use the S3 service. You can checkout the sample code and in order to make it work for your S3 buckets, you have to copy the `awsconfiguration.json` file to `/src/main/resources` in the sample project.

The relevant code that will query the AWS S3 service is pasted below:

       
TransferUtility transferUtility = TransferUtility.builder()
                    .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
                    .s3Client(new AmazonS3Client(AWSMobileClient.getInstance().getCredentialsProvider()))
                    .build();
...
TransferObserver downloadObserver = transferUtility.download("public/" + bucketName, new File(publicRoot, fileName));

We always recommend testing your Java mobile apps on a desktop system. The Gluon Mobile tools make sure that the exact same code you use for desktop deployment runs on iOS, Android and embedded systems as well.

Running your app in a desktop simulator environment is done by using the Gluon IDE plugins, or in a terminal using the following command:
./gradlew run
which will download the required dependencies, and then execute the app.

If this is working fine, you can run it on the iPhoneSimulator or an iOS device if you have a mac using
./gradlew launchIphoneSim
or
./gradlew launchIosDevice

If you have an Android device connected to your development system, you can launch the app there using
./gradlew androidInstall

For more information about getting started with Java on Mobile, have a look at our documentation.