ProGuard and the newer R8 are optimizers and obfuscators for Java and Android applications. You can use these tools with your libGDX application to make it harder for 3rd parties to decompile your app, reduce your apps size and even increase the runtime speed by ahead-of-time optimizations like inlining.

    The following configuration file will make your libGDX app work with ProGuard/R8:

    1. # To enable ProGuard in your project, edit project.properties
    2. # to define the proguard.config property as described in that file.
    3. #
    4. # Add project specific ProGuard rules here.
    5. # By default, the flags in this file are appended to flags specified
    6. # in ${sdk.dir}/tools/proguard/proguard-android.txt
    7. # You can edit the include path and order by changing the ProGuard
    8. # include property in project.properties.
    9. #
    10. # For more details, see
    11. # http://developer.android.com/guide/developing/tools/proguard.html
    12. # Add any project specific keep options here:
    13. # If your project uses WebView with JS, uncomment the following
    14. # and specify the fully qualified class name to the JavaScript interface
    15. # class:
    16. #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
    17. # public *;
    18. #}
    19. -verbose
    20. -dontwarn com.badlogic.gdx.backends.android.AndroidFragmentApplication
    21. -dontwarn com.badlogic.gdx.utils.GdxBuild
    22. -dontwarn com.badlogic.gdx.physics.box2d.utils.Box2DBuild
    23. -dontwarn com.badlogic.gdx.jnigen.BuildTarget*
    24. -dontwarn com.badlogic.gdx.graphics.g2d.freetype.FreetypeBuild
    25. # Required if using Gdx-Controllers extension
    26. -keep class com.badlogic.gdx.controllers.android.AndroidControllers
    27. # Required if using Box2D extension
    28. -keepclassmembers class com.badlogic.gdx.physics.box2d.World {
    29. boolean contactFilter(long, long);
    30. void beginContact(long);
    31. void endContact(long);
    32. void preSolve(long, long);
    33. void postSolve(long, long);
    34. boolean reportFixture(long);
    35. float reportRayFixture(long, float, float, float, float, float);
    36. }

    Note that you will also have to keep any classes that you access via reflection yourself! Please refer to the ProGuard/R8 documentation for more details.

    To apply ProGuard/R8 to your Android project on Release builds, you need to add the following config to build.gradle file (the one in the android/ folder of your project, not the root build.gradle)

    1. buildTypes {
    2. release {
    3. minifyEnabled true
    4. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    5. }
    6. }