Optimize, shrink and obfuscate your android app

Jay Patel
4 min readNov 26, 2020

--

Image Reference

When we study computer science, we read an exciting topic at the beginning of the Data Structure and Algorithms, i.e., Space and Time Complexity. Space Complexity is how much amount of memory space it requires to run a computational problem. It’s just the same when we talk about the apps or any software. We all know that software size is an essential factor in user engagement. Therefore, App size matters. I will show an app for the reference. This app is a recharge and bill payments app. For a rough idea, It has nearly 75 screens with 80 API requests with a mixup of POST and GET API Calls.

The App Size is 10.4 MB without shrinking tools.

In android, you have ways to optimize and shrink your apk size. Here, how it works:

Analyze your APK timely:

You can navigate to:

Project> Build> Outputs> apk> debug

to find your build apk. You can analyze all your dex files and resources there. Check the resources for any large files and PNGs. Try using vector assets such as SVGs compared to PNGs. Vector Images save nearly 60 to 90% of the size and remain crisp and clear at any possible resolution. Try using Vector Assets in Android Studio for the conversion of the SVGs in the desired XML.

Using R8 and Proguards:

Proguard conversion to dex file

R8 is now the default in the Android Gradle plugin when you enable minification in the ‘build.gradle’ file of your project:

R8 conversion to dex file

Note: This code will do the shrinking in the debug-build; make sure to false it, as the error found in the logcat may bother you due to renaming of the class and variable name.

The app has been reduced by 4Mb in size which is a reduction of the app size by nearly 40%.

You can use Proguards in place of the default R8 plugin by adding these two lines in the build.gradle file

android.enableR8=false
android.enableR8.libraries=false

Both these tools help to shrink and optimize the code.

They help in changing the class names, which also helps in protecting the code from reverse engineering. It also reduces the dex size as it shortens the term of classes and members; it also inspects and rewrites the code to minimize the code. If it detects an if/else condition else has never been called, it removes the else part of the code for further reduction.

As the official documentation says, R8 is enabled by default, and to do aggressive optimization you have to include this line in gradle.properties:

android.enableR8.fullMode=true

Remove unused alternative resources:

You can also remove the alternative code resources which are not used by the app code. For Example, if your app is only in English and Hindi language then you can restrict with the help of resConfigs. The gradle shrinker will remove all the other languages to minimize the size.

App size is reduced to 6.3 MB after using resConfigs = true

App Bundles

App Bundles help a lot for shrinking the app and optimization, Bundles have the whole package of every aspect, screen ratios, and resources. Whenever we upload an app bundle to the Google Play Console, it automatically selects the Apk size according to the screen ratio and a screen density of the phone. It also removes the user less demanded feature to reduce app size.

Summary

  1. Analyze your app and optimize accordingly, See the resources and change the PNGs to SVGs.
  2. Using R8 and Proguard shrinking.
  3. Removing unused imports.
  4. Use of App Bundles

References

  1. Proguard and R8: Optimizers
  2. Enable Shrinking, obfuscation, and optimization.
  3. The Smaller, the Better: Reducing Android App Size

Thanks for reading this. Happy Shrinking! :)

--

--

Jay Patel

Software Engineer @QBurst || IITP '25 Cloud Computing