AWS Mobile Blog

Using ProGuard with the AWS SDK for Android

by Bob Kinney | on | Permalink | Comments |  Share

Version 2 of the AWS Mobile SDK

  • This article and sample apply to Version 1 of the AWS Mobile SDK. If you are building new apps, we recommend you use Version 2. For details, please visit the AWS Mobile SDK page.
  • This content is being maintained for historical reference.

With storage on mobile devices at a premium, ProGuard has become a popular option for Android developers looking to minimize the size of their apps and also protect their code.

We added support for ProGuard in version 1.3.0 of the AWS SDK for Android, including a recommended ProGuard config and shipping with a validated ProGuard config for each included sample. This post is meant to provide an example of the benefits of using ProGuard in your Android app and how you can use it to ship smaller Android apps to your customers.

Benefits of ProGuard: An Example

To provide an example of the possible benefits of ProGuard, let’s look at a real application. Included in the AWS SDK for Android is a demo application that shows basic operations with Amazon Simple Storage Service (Amazon S3), Amazon Simple Queue Service (Amazon SQS), Amazon Simple Notification Service (Amazon SNS), and Amazon SimpleDB (S3_SimpleDB_SNS_SQS_Demo).

Below is the file size listing (in KB) for the exported .apk file of this demo in four different configurations:

  1. Using the debug JAR with ProGuard disabled
  2. Using the non-debug JAR with ProGuard disabled
  3. Using the core and service JARs with ProGuard disabled
  4. Using the core and service JARs with ProGuard enabled

 

1272 S3_SimpleDB_SNS_SQS_Demo1.apk
1100 S3_SimpleDB_SNS_SQS_Demo2.apk
 796 S3_SimpleDB_SNS_SQS_Demo3.apk
 512 S3_SimpleDB_SNS_SQS_Demo4.apk

As you can see, with each successive change in configuration, we remove more from the final .apk size. Prior to ProGuard, the smallest app we could achieve would have been #3, but in this example, ProGuard takes off an additional 35 percent from our smallest app – a significant savings. The savings for any individual app will vary depending on services and operations used in the app as well as other third-party libraries included by the app, so your results may vary.

Creating a ProGuard Config

ProGuard is integrated into the Eclipse IDE by means of the ADT plug-in. If enabled for a project, Eclipse will run ProGuard for any project when exporting the app as an .apk file.

A default, recommended config ships with the toolkit and can be included in your Android app. You can augment its settings or even override them in a project customization file. For any new Android project, this file is created for you and is named proguard-project.txt. By default, the file contains no active rules or configuration settings.

If you are adding Progaurd to an existing app, you can place your proguard-project.txt anywhere in the project folder, but typically you’d place it at the root of your project, alongside the AndroidManifest.xml and project.properties files.

Regardless of whether you create the file yourself or use the default config when you create a new project, you still need to enable ProGuard for your app. If you refer to the Android Tools Project Site,note that you do this by adding a line similar to the following in your project.properties file:

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

The variable ${sdk.dir} expands to the path of the Android SDK you specified when setting up your Eclipse IDE for Android development.

If you did not create your proguard-project.txt file at the root of your project directory, you need to supply the path to the file as well.

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:<path_to_proguard>/proguard-project.txt

Modifying proguard-project.txt for the AWS SDK for Android

When exporting your app using the AWS SDK for Android with the ADT-supplied, empty proguard-project.txt file, you may notice that Eclipse reports errors. In order to correct these errors, add the following lines to the end of your proguard-project.txt file:

-keep class org.apache.commons.logging.**               { *; }
-keep class com.amazonaws.services.sqs.QueueUrlHandler  { *; }
-keep class com.amazonaws.javax.xml.transform.sax.*     { public *; }
-keep class com.amazonaws.javax.xml.stream.**           { *; }
-keep class com.amazonaws.services.**.model.*Exception* { *; }
-keep class org.codehaus.**                             { *; }
-keepattributes Signature,*Annotation*

-dontwarn javax.xml.stream.events.**
-dontwarn org.codehaus.jackson.**
-dontwarn org.apache.commons.logging.impl.**
-dontwarn org.apache.http.conn.scheme.**

It is important to note that this configuration applies only to the AWS SDK for Android. If you are using additional third-party libraries, consult any documentation from those libraries to see if they require any additional configuration or directives to enable their use with ProGuard.