Android Safeguard — ProGuard

Rizvan Hawaldar
2 min readNov 8, 2017

Never release an application without ProGuard. It’s freely available , comes bundled with the Android SDK. ProGuard not only minifies your code, but it obfuscates your code making it harder for reverse-engineers to understand, replicate and manipulate it.

Proguard majorly helps in optimisation ,obfuscation, Minification and repackaging. Proguard is used for code shrinking , detects and removes unused classes, fields, methods, and attributes from your packaged app, including those from included code libraries. ProGuard also optimizes the bytecode, removes unused code instructions, and obfuscates the remaining classes, fields, and methods with short names.

To enable code shrinking with ProGuard, add minifyEnabled true to the appropriate build type in your build.gradle file. Android studio disables Proguard when using Instant run .

build.gradle

buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile(‘proguard-android.txt’)
}
}

getDefaultProguardFile(‘proguard-android.txt’) method obtains the default Proguard settings from the Android SDK tools/proguard folder. Android Studio adds the proguard-rules.pro file at the root of the module, which helps to add custom Proguard rules.

Android plugin shrinker supports Instant Run unlike ProGuard which makes build time slow , Android plugin shrinker does not obfuscate or optimize your code as it only removes unused code. You can use Android plugin shrinker in debug mode set useProguard to false .

build.gradle

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

ProGuard configuration for libraries

If you’re building an AAR library to be used in other projects that needs ProGuard rules to work, you should use the consumerProguardFiles option to package a ProGuard configuration file with the AAR. This way, anyone using your library will not have to worry about adding rules manually when enabling the minifier.

android {
defaultConfig {
consumerProguardFiles “proguard-rules.txt”
}
}

Drawbacks

  • App crash due to configured rules poorly managed.
  • Stack traces are difficult to understand because of obfuscation.

Learned something? Clap your 👏 to say “thanks!” and help others find this article.

--

--

Rizvan Hawaldar

Android dev turned Full stack dev, building ai apps in free time, gaining expertise in Kotlin (MPM/ServerSide/Android). http://github.com/llRizvanll