Minimize Android app but not obfuscate it

When I do not reduce my application, I get the maximum number of tags, and the creation of the dex file fails. This can be avoided by including minify in build.gradle . However, the drawback is that now the code is getting confusing. This is fine for the Release build, but for the Debug build it is problematic.

Is there a way to tell gradle to minimize Debug build but not obfuscate it?

+10
source share
4 answers

Yes, you can use ProGuard to minimize debug builds.

The key is to use the -dontobfuscate in the ProGuard configuration to build debugging.

Use this parameter 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 version of ProGuard configuration in proguard-rules.pro .

Use the same configuration for release and debugging. This way you guarantee that the code you need will not be deleted. And minimizing debugging doesn't break the build.

Add the optional ProGuard configuration file proguard-rules-debug.pro to build debugging. It should contain rules used only for debugging. In this case, add only:

 -dontobfuscate 
+20
source

A simple solution is to add minifyEnabled true and useProguard false to the build configuration. But code compression is not recommended for debug builds from white papers. Keep in mind that code compression slows down build time, so avoid using it in a debug build if possible. Link https://developer.android.com/studio/build/shrink-code.html

+3
source

Tomik's answer is technically correct, but it does not support using Instant Run for your collections. As stated in the official code abbreviation guide :

Enable code compression using Instant Run. If code compression is important to you when building the application gradually, try an experimental one that is built into the Android plugin for Gradle. This Unlike ProGuard, shrinkage supports Instant Run.

You can customize the Android plugin utilizer using the same as the ProGuard files. However, the plugin for Android plugins does not obfuscate or optimize your code - it only removes unused code. Therefore, you should only use it for your debug builds and enable ProGuard for your version so that your release APK is confused and optimized.

So, the correct solution would be to configure the debug build as follows:

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

Thus, the code in your debug assembly is not optimized or confused, but will decrease. This also applies when using Instant Run .

+3
source
 minifyEnabled true 

this is just a shortcut to:

 postprocessing { removeUnusedCode true obfuscate true optimizeCode true } 

So, if you want to minimize without obfuscation, replace minifyEnabled true with:

 postprocessing { removeUnusedCode true obfuscate false // <-- optimizeCode true } 

In addition, the compiler will complain if you have shrinkResources true . The equivalent post-processing field is removeUnusedResources true , i.e.

 postprocessing { removeUnusedCode true removeUnusedResources true // <-- obfuscate false optimizeCode true } 

Unlike other answers, useProguard false does not disable obfuscation ; this changes the entanglement mechanism from ProGuard to R8.

0
source

All Articles