How to publish your Android app to an S3 bucket using Gradle

How to Publish your Android app to an S3 bucket using Gradle - Coletiv Blog

If you ever need to publish your Android app in a simple way outside the Play Store, publishing the app .apk file to an S3 bucket could be a good solution. Then you’ll just need to share the generated link to that .apk file.

Here’s how to do it.

1. Create an AWS S3 bucket

If you don’t have one already, refer to the AWS documentation for this step.

2. Create an IAM user

Again, refer to the AWS documentation to check how to do this. Just make sure that you select Programmatic access as the type of access for this user and save the Access Key and Secret Key.

3. Setup your S3 variables

You can either set up the S3 variables in the project build script or per individual module if you prefer. In this example, I will do it in the project. Just open the build.gradle file and add the following code.

allprojects {
    project.ext.AWS_S3_BUCKET = "YOUR BUCKET NAME"
    project.ext.AWS_S3_KEY_ID = "YOUR BUCKED SECRET ID"
    project.ext.AWS_S3_KEY_SECRET = "YOUR BUCKED SECRET KEY"
    project.ext.AWS_S3_APK_TARGET_FOLDER = "YOUR S3 TARGET FOLDER"
}

For security reasons, I highly recommend that you save the Access Key and Secret Key as environment variables so they won’t end up in your code repository.

4. Add the publish task to your project

First, you need to download the code for the publish task, find it in this gist, and put it in your app folder.

Then, in the build.gradle of each application you want to publish, add the following:

apply from: 'publishApkToS3.gradle'

Keep in mind that this task uses CURL, so make sure you have it installed in your machine.

5. Publish your app to S3

Finally, you just need to execute the following command to publish your app to the S3 bucket.

publish{flavor-name}ToS3

If you’re planning on running this on your CI, add the following commands to your configuration file.

chmod +x ./gradlew
./gradlew publish{flavor-name}ToS3

Thank you for reading!

Thank you so much for reading, it means a lot to us! Also don’t forget to follow Coletiv on Twitter and LinkedIn as we keep posting more and more interesting articles on multiple technologies.

In case you don’t know, Coletiv is a software development studio from Porto specialised in Elixir, iOS, and Android app development. But we do all kinds of stuff. We take care of UX/UI design, web development, and even security for you.

So, let’s craft something together?