Shrink your code. Code shrinking with R8 is enabled by default when you set the minifyEnabled property to true . Code shrinking (also known as tree shaking), is the process of removing code that R8 determines is not required at runtime.
R8 is having a faster processing time than Proguard which reduces build time. R8 gives better output results than Proguard. R8 reduces the app size by 10 % whereas Proguard reduces app size by 8.5 %. The android app having a Gradle plugin above 3.4.
Code Obfuscation is the process of modifying an executable so that it is no longer useful to a hacker but remains fully functional. While the process may modify actual method instructions or metadata, it does not alter the output of the program. On some platforms (such as Java, Android, iOS and .
minifyEnabled true
is just a shortcut for:
postprocessing {
removeUnusedCode true
obfuscate true
optimizeCode true
}
So, if you want to minify without obfuscating, replace minifyEnabled true
with:
postprocessing {
removeUnusedCode true
obfuscate false // <--
optimizeCode true
}
Additionally, the compiler will complain if you have shrinkResources true
. The equivalent postprocessing field is removeUnusedResources true
, i.e:
postprocessing {
removeUnusedCode true
removeUnusedResources true // <--
obfuscate false
optimizeCode true
}
Contrary to other answers, useProguard false
does not disable obfuscation; it changes the obfuscation engine from ProGuard to R8.
Yes, you can use ProGuard to minify debug builds.
The key is to use -dontobfuscate
option in ProGuard configuration for debug build.
Use this setting in build.gradle
:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro',
'proguard-rules-debug.pro'
}
}
Write your release ProGuard configuration to proguard-rules.pro
.
Use the same configuration for release and debug. This way you ensure that no necessary code is stripped away. And debug minification doesn't break the build.
Add extra ProGuard config file proguard-rules-debug.pro
for debug build. It should contain rules used only for debug. In this case add only:
-dontobfuscate
Tomik's answer is technically correct, but it doesn't support using Instant Run for your builds. As pointed out in the official guide on code-shrinking:
Enable code shrinking with Instant Run If code shrinking is important to you while incrementally building your app, try the experimental code shrinker that's built into the Android plugin for Gradle. This shrinker supports Instant Run, unlike ProGuard.
You can configure the Android plugin shrinker using the same configuration files as ProGuard. However, the Android plugin shrinker does not obfuscate or optimize your code—it only removes unused code. So you should use it for your debug builds only, and enable ProGuard for your release builds so your release APK's code is obfuscated and optimized.
So the proper solution would be to setup your debug build like this:
android {
buildTypes {
debug {
minifyEnabled true
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
This way the code in your debug build doesn't get optimized nor obfuscated, but will get shrunk. This also applies when using Instant Run.
A simple solution is to add minifyEnabled true
and useProguard false
inside the build configuration. But code shrinking is not recommended for debug builds from official docs Be aware that code shrinking slows down the build time, so you should avoid using it on your debug build if possible.
Reference https://developer.android.com/studio/build/shrink-code.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With