proguard ❤ gradle

Andrew Chen
靠der Cowder
Published in
1 min readMay 6, 2016

Preface

Refer Shrinking | Android Developers, you can enable proguard by the following configuration:

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

We can see getDefaultProguardFile(‘proguard-android.txt’). Actually, it’s from $ANDROID_HOME/tools/proguard/proguard-android.txt :

  • /opt/android-sdk-linux/tools/proguard/proguard-android.txt
  • or /opt/android-studio/sdk/tools/proguard/proguard-android.txt

And proguard-project.txt or proguard-rules.pro is generated by Android Studio, it’s copied from

  • /opt/android-sdk-linux/tools/proguard/proguard-project.txt
  • or /opt/android-studio/sdk/tools/proguard/proguard-project.txt

Usually, we have a few proguard configs for corresponding libraries, e.g.:

Normally, we will configure such like that:

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro', 'rxjava.pro', 'crashlytics-2.pro', 'google-analytics.pro'

KISS ❤ *.pro

gradle provides possible for easy to use with fileTree()

fileTree(dir: ‘.’, include: ‘*.pro’)

As Application ❤ *.pro

As application you can use the following instead:

proguardFiles getDefaultProguardFile(‘proguard-android.txt'), fileTree(dir: ‘.’, include: ‘*.pro’)

As Library ❤ *.pro

As library, you can use consumerProguardFiles to pack *.pro into aar:

consumerProguardFiles fileTree(dir: '.', include: '*.pro')

Folder structure:

.
├── build.gradle
├── proguard-rule.pro
├── rxjava.pro
├── crashlytics-2.pro
├── google-analytics.pro
└── src

Who ❤ *.pro

--

--