The Android SDK includes a free tool called ProGuard and can be used to minify the size of the APK file and to remove debugging code. This is only needed if you are using Java to build your app.
CAUTION: If you built your app using HTML5, CSS3 and JavaScript (e.g., jQueryMobile), you don't need to do this step.
Shrinker - detects and removes unused classes, fields, methods and attributes. WHY: To create more compact code, faster network transfer and loading.
Optimizer - optimizes bytecode and removes unused instructions. WHY: To list dead code so it can be removed from the source code.
Obfuscator - renames the remaining classes, fields and methods using short meaningless names WHY: To make programs and libraries harder to reverse-engineer.
Preverifier - preverifies the processed code for Java 6 or higher or for Java Micro Edition. WHY: To retarget and to preverify existing class to take full advantage of faster class loading.
ProGuard's main advantages compared to other Java obfuscators is:
It is a compact template-based configuration. A few intuitive command line options or a simple configuration file are usually sufficient. The user manual explains all available options and shows examples of this powerful configuration style.
It is fast. It only takes seconds to process programs and libraries of several megabytes. The results section presents actual figures for a number of applications.
It is a command-line tool with an optional graphical user interface. It also comes with plugins for Ant, for Gradle, and for the JME Wireless Toolkit.
Minify APK
Navigate to your current APK file to see what its current file size is (e.g., 1.05M). NOTE: Even though an APK file may be small, every byte matters when dealing with mobile devices.
Launch Android Studio and open the file named build.gradle. NOTE: Notice it has an attribute called minifyEnabled false set by default.
Set the minifyEnabled to true (minifyEnabled true). WHY: This tells ProGuard to reduce the size of the APK file by:
-
looking for libraries that are not used and remove them.
- renaming methods, packages and classes to reduce the size of their identifiers.
-
obscuring the code making it harder to read or reverse engineer.
CAUTION: This may be a problem if your app is using advanced Java techniques (e.g., indirect call to classes) that are not named in your code.
(OPTIONAL) Move the original apk files to a new location so that you don't overwrite them
In Android Studio, re-sync the app.
Check the new size of the APK.
Test APK to ensure that it works the same as it did before you minified it.
Remove Debugging Code
The Android development guidelines recommend that you "stripped out" certain classes that are only used for debugging. While you could do this manually, you can use ProGuard to remove debugging code from your app before you package it for distribution.
Let us say that you're using logcat in your app to trace certain actions in the app.
Go to MainActivity class.
Type a call to the Log class at the end of the class (Log.d(" MainActivity", "Logging message")) before the closing curly brace. NOTE: This call would be included in the final code. You could remove it manually but you may want to leave it for later development.
In Android Studio, open the gradle.build file. NOTE: Notice that there's a call to proguardFiles that refers to two files. The first one (proguard-android.txt) points to a file that is part of your SDK installation that is shared among all projects. The other file (proguard-rules.pro) is unique to your app.
Go back to your Project window and select the app module to see the proguard-rules.pro file. NOTE: Notice that it's at the module root and not the project root.
Go to the ProGuard website, click on Manual, Examples and then click on Removing logging code link.
Select the code sample and copy it.
Go back to Android Studio and paste the code at the end of the proguard-rules file. WHY: To make any call to the Log class method be stripped from the APK file.