article

Amelia@Amazon avatar image
Amelia@Amazon posted

How to use proguard within your Android apps with new Android Dev Tools

Issue

As Android developers look for ways to protect their property, and be able to avoid the 65,000 method limit issues that are normally associated with the Dalvik Runtime, ProGuard is a

great first line of defense for both of these. However, by default it is not enabled in Android Studio as of version 1.0 of their Gradle plugin, nor are the configuration files setup by default.

Resolution

To configure ProGuard, you will need to configure the minification options within your project's build.gradle file. (please see example below)

As of Android Studio 1.1, ProGuard configuration files are no longer applied to the test APK. If ProGuard is enabled, it is only to generate the mapping file that will be used when running your ProGuard rules on the release APK.

For more information on how ProGuard works with the Android Gradle Plugin, please look at Google's documentation for their Gradle Plugin.

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard

If you are still using a non-production version of Android studio, one thing to note is when you do migrate to version 1.0, the ProGuard enablement is handled differently. For more details on that, please refer to the Android documentation for migrating to Android Studio 1.0 and above:

http://tools.android.com/tech-docs/new-build-system/migrating-to-1-0-0

For information on what options you should use in a ProGuard configuration for general cases with Android, refer to the ProGuard manual,

https://stuff.mit.edu/afs/sipb/project/android/sdk/android-sdk-linux/tools/proguard/docs/index.html#manual/introduction.html

Also, of note, you may want to look into resource shrinking to help reduce the size of your APK at release as well, in the Android plugin for Gradle, this requires ProGuard to be enabled first, and google has that documented at:

http://tools.android.com/tech-docs/new-build-system/resource-shrinking

Sample Code

 // build.gradle file 
android {
    ...
        buildTypes {
            release {
                minifyEnabled true // set to true or false to enable or disable Proguard
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
} 

Keywords: Proguard, Security, Android Studio[/b]

KB_0035

androidlollipop
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Article

Contributors

brizzlebrazzle contributed to this article