Skip to content

Proguard

Lopez Mikhael edited this page Jun 7, 2020 · 3 revisions

The size of Android applications has been increasing for years.

To make your app as small as possible, you should enable shrinking option in your release build to remove unused code and resources. When enabling shrinking option, you also benefit from obfuscation which shortens the names of your app’s classes and members, and optimization which applies more aggressive strategies to further reduce the size of your app.

ProGuard (Shrink, Optimize and Obfuscate)

Enable D8 shrinker on gradle.properties

android.enableD8=true

All proguard rules:

Defined on dependencies.gradle:

// PROGUARD
proguardFolder              = '../proguard/'
modelRules                  = proguardFolder + 'model-rules.pro'
supportRules                = proguardFolder + 'support-rules.pro'
apacheRules                 = proguardFolder + 'apache-rules.pro'
gsonRules                   = proguardFolder + 'gson-rules.pro'
okhttp3Rules                = proguardFolder + 'okhttp3-rules.pro'
retrofit2Rules              = proguardFolder + 'retrofit2-rules.pro'
stethoRules                 = proguardFolder + 'stetho-rules.pro'

Add on Data module:

release {
    minifyEnabled true
    consumerProguardFiles modelRules, supportRules, apacheRules, gsonRules, okhttp3Rules, retrofit2Rules, stethoRules
}

Enable on Presentation module:

release {
    signingConfig signingConfigs.release
    shrinkResources true
    minifyEnabled true
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')
}

⚠️ Don't forget to disable proguard when you run Android tests, that's why I setup a new buildType debugTest like this:

debugTest {
    initWith(debug)
    minifyEnabled false
}
testBuildType 'debugTest'
Clone this wiki locally