Add Proguard to your Android Game

Installing ProGuard

Adding ProGuard to your game adds a number of potential benefits. If you're at a high Dex count, it may bring down your Dex count low enough so that you won't need to enable Multidex. it can also shrink your APK's size in the process as well. As an added bonus, It will also obfuscate your code which adds security since it will be harder for other people to read.

This article will teach you how to use ProGuard. For more details about what ProGuard is and how it works, you can refer to this article.

We integrated ProGuard with our in-house game and you can see the code for that integration here: https://github.com/skillz/3d-cave-runner/tree/master-v3. You can also check out this direct link to our Proguard rules file. You can use this tool to help you find out what your game’s current Dex count is. With this tool, we learned that our previous Dex count was at 115,712 and it became 59,092 after integrating ProGuard.

Here are the steps you can take to use ProGuard with your game:

  1. Create a ‘proguard-rules.pro’ file in your game’s root directory beside build.gradle as shown on the screenshot below.


  2. In your build.gradle file, ensure that minifyEnabled is added and is set to true. Also, add proguardFiles 'proguard-rules.pro' under 'release' of 'buildTypes' as shown on the screenshot below: 

    Note: You can also set 'shrinkResources' to true to help lower the Dex count even more. Shrinking Resources will only work when 'minifyEnabled' is set to true.

  3. In your proguard-rules.pro file, you will be able to specify which files ProGuard should keep and which ones should not throw a warning. You can refer to this file to see how we did it. Here is the official Link to the ProGuard Manual for your reference.

  4. Make sure that every time you edit your 'proguard-rules.pro' file that you do a clean before you make another build to test out if your game still works after ProGuard. This deletes the previous APK file you have created and it also makes it easier for you to check the Dex count each time you make a build.

 

Troubleshooting ProGuard

If you run into any additional issues such running out of VM memory and GC overhead, you can resolve this by creating a file next to your build.gradle called ‘gradle.properties’. This file contains the content below:

 
org.gradle.daemon=true
GRADLE_OPTS="-Xmx4096m -Xms4096m -XX:MaxPermSize=2048m"
org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.configureondemand=true

 

This new file allows you to configure the Gradle build environment. You can learn more about ‘gradle.properties’ in their official document.

You might also need to specify the maximum memory allocation in your build.gradle file so that you can limit the memory that gradle uses. You can do this by adding the following inside the android block of your build.gradle file:

 
android {
   -----
   dexOptions {
       javaMaxHeapSize "2g"   // Change this value depending on your needs
   }
   -----
}

If, after enabling ProGuard and your game's Dex count is still above the 64K reference limit, you may need to look into Multidex which you can learn more about in this article.