← Back to Blog Tech

The libGDX ProGuard/R8 Survival Guide (With Complete Kotlin 3D Config)

When you ship a libGDX game on Android, file size matters. A default build pulls in the native C++ engines, the Java framework, and the Kotlin stdlib. Turning on R8 (Android's current ProGuard replacement) trims unused code and resources.

But libGDX depends on JNI hooks and runtime reflection to draw 3D, animate models, and load assets. An unconfigured R8 run deletes or renames parts of the framework it thinks are unused. You get a build that compiles clean, then dies on the device with a ClassNotFoundException or NoSuchMethodError.

Here's a setup that shrinks the build without breaking 3D rendering or the Scene2D UI.


1. Configuring build.gradle

First tell Gradle to run minification and resource shrinking on the release build. Open android/build.gradle (or build.gradle.kts if you use the Kotlin DSL) and find the buildTypes block.

Turn both flags on:

groovy

android {
    // ...
    buildTypes {
        release {
            minifyEnabled true      // Activates R8 code shrinking and obfuscation
            shrinkResources true    // Strips unused layout elements, icons, and XMLs
            
            // Re-use your debug signing configuration temporarily for local testing
            signingConfig signingConfigs.debug 
            
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


A note on debug builds

Leave these flags false in the debug { ... } block. Minification slows down every local build, and keeping it off gives you fast deploys over USB and readable stack traces (something like Player.kt:50 rather than a.b.c(Unknown Source)) when something throws.


2. The full proguard-rules.pro

Open proguard-rules.pro in your android/ module and replace its contents with this tested setup. It covers Kotlin metadata, libGDX native bindings, 3D shaders/attributes, and Scene2D UI.

pro

# ====================================================================
# 1. GENERAL & COMPILER CONFIGURATION
# ====================================================================
-verbose
-dontwarn android.support.**
-dontwarn com.badlogic.gdx.backends.android.AndroidFragmentApplication
-keepattributes LineNumberTable,SourceFile,RuntimeVisibleAnnotations,AnnotationDefault
-renamesourcefileattribute SourceFile

# ====================================================================
# 2. KOTLIN STANDARD LIBRARY RULES
# ====================================================================
# Essential to prevent the optimizer from stripping language metadata
-keep class kotlin.Metadata { *; }
-keep class kotlin.reflect.** { *; }
-keepclassmembers class **$WhenMappings {
    ;
}

# ====================================================================
# 3. LIBGDX CORE & NATIVE JNI HOOKS
# ====================================================================
# Protects C++ bindings (Audio, Box2D, Bullet, and Core Graphics)
-keepclasseswithmembernames class * {
    native ;
}
-keep class com.badlogic.gdx.backends.android.** { *; }
-keep interface com.badlogic.gdx.utils.Disposable { *; }

# ====================================================================
# 4. 3D GRAPHICS ENGINE PRESERVATION
# ====================================================================
# Keeps 3D model loaders, environment states, and dynamic shaders intact
-keep class com.badlogic.gdx.graphics.g3d.** { *; }
-keep class com.badlogic.gdx.graphics.g3d.attributes.** { *; }
-keep class com.badlogic.gdx.graphics.g3d.environment.** { *; }
-keep class com.badlogic.gdx.graphics.g3d.utils.** { *; }

# ====================================================================
# 5. 2D GUI & SCENE2D UI CORE
# ====================================================================
# Prevents obfuscating UI skins, bitmap fonts, and viewport layouts
-keep class com.badlogic.gdx.scenes.scene2d.** { *; }
-keep class com.badlogic.gdx.graphics.g2d.BitmapFont { *; }
-keep class com.badlogic.gdx.graphics.Color { *; }
-keep class com.badlogic.gdx.scenes.scene2d.ui.Skin$** { *; }

# ====================================================================
# 6. OFFICIAL EXTENSIONS
# ====================================================================
# Box2D Physics Engine JNI Callbacks
-keep class com.badlogic.gdx.physics.box2d.** { *; }
-keepclassmembers class com.badlogic.gdx.physics.box2d.World {
   boolean contactFilter(long, long);
   boolean getUseDefaultContactFilter();
   void    beginContact(long);
   void    endContact(long);
   void    preSolve(long, long);
   void    postSolve(long, long);
   boolean reportFixture(long);
   float   reportRayFixture(long, float, float, float, float, float);
}
-dontwarn com.badlogic.gdx.physics.box2d.gwt.**

# ====================================================================
# 7. YOUR LOCAL INDIE GAME LOGIC
# ====================================================================
# IMPORTANT: Adjust these package naming paths to match your project structure.
# This prevents R8 from breaking your launchers and reflection-heavy data models.

# Keep your explicit launcher entry point
-keep public class io....android.AndroidLauncher { *; }
-keep public class io....Main { *; }

# Safety Net: Keep your own code packages unmodified for initial stability
-keep class io....** { *; }

# If you use libGDX Json serialization to handle save game states, 
# uncomment the rule below to protect your state fields from renaming:
# -keep class io....models.** { *; }



3. Building and checking

To build and test the optimized output from the command line, use your IDE's terminal.

To build a release APK:

bash

./gradlew android:assembleRelease


To build a production App Bundle (.aab):

bash

./gradlew android:bundleRelease


What it does to size

A normal debug APK often lands around 25MB–28MB, mostly because it carries uncompressed native libraries (.so files for arm64-v8a, armeabi-v7a, x86, and x86_64).

After minifying and packaging as an .aab, output drops to about 9.3MB. R8 cuts the Java/Kotlin weight, and the App Bundle format adds structural compression. When a player downloads from the store, the server strips out the architectures they don't need, so the actual download is even smaller.

Before shipping, install the release on a real device, check the 3D renders and Scene2D scaling, and watch logcat for runtime errors.