Minify android app but do not obfuscate it

user4918296 picture user4918296 · Mar 7, 2016 · Viewed 10.5k times · Source

When I do not minify my app I reach the maximum method count and building the dex file fails. This can be avoided by enabling minify in build.gradle. The downside, however, is that now the code gets obfuscated. This is OK for the Release build but it is problematic for a Debug build.

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

Answer

Andrew Sun picture Andrew Sun · Apr 21, 2019
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.