Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prepare a proguard file and whats included in it?

I am working on an app in Android Studio and I want to add proguard to my app. But I dont know what to do? Also I'd like to learn its context. Can anyone show me something? Thanks.

like image 361
nuhkoca Avatar asked Jul 05 '16 22:07

nuhkoca


2 Answers

In your gradle file set true to minifyEnabled

You can define if proguard is enabled in debug, release or both

buildTypes {
        release {
            minifyEnabled true
            proguardFiles 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            proguardFiles 'proguard-rules.pro'
        }
    }

You can also set the proguardFiles to config him, check this site to see the docs about it, look this example:

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/balysv/Documents/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

-optimizationpasses 5
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose

If you want use a custom dictionary for code obfuscation, set this config with your dictionary file:

-obfuscationdictionary proguard-dic.txt
-classobfuscationdictionary proguard-dic.txt
-packageobfuscationdictionary proguard-dic.txt

The dictionary file is a simple text file with the labels you want to use to obfuscate your code, 1 label per line.

like image 156
Ismael Di Vita Avatar answered Oct 13 '22 20:10

Ismael Di Vita


To make your APK file as small as possible, you should enable shrinking to remove unused code and resources in your release build.

Code shrinking is available with ProGuard, which detects and removes unused classes, fields, methods, and attributes from your packaged app, including those from included code libraries (making it a valuable tool for working around the 64k reference limit).

ProGuard also optimizes the bytecode, removes unused code instructions, and obfuscates the remaining classes, fields, and methods with short names. The obfuscated code makes your APK difficult to reverse engineer, which is especially valuable when your app uses security-sensitive features, such as licensing verification.

For example, the following snippet from a build.gradle file enables code shrinking for the release build:

android {
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile(‘proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}

Example of use from Proguard, from Android Studio

like image 25
Anantha Raju C Avatar answered Oct 13 '22 20:10

Anantha Raju C