androidgradlefunctional-programmingandroid-gradle-pluginandroid-proguard

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.


Solution

  • 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 configure it, 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.