Search code examples
androidproguardobfuscationandroid-obfuscation

Android Proguard obfuscation configuration problem


I am having issues with creating a obfuscated/shrinked/optimized apk for my Android(Java) app. As I am new to proguard ,I am trying first to get the apk done with the basic config, I have not added anything yet to the proguard-rules.pro and all I have done so far is to set minifyEnabled to true like so:

    buildTypes {
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

So when I run the app on the emulator device, the build process is successfull, the apk gets really small, it does not start properly on the device but I guess this is expected as I have not added any rules to the proguard-rules.pro file. There is an issue, though, when I try to manually build an APK. I get the following error:

Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in missing_rules.txt.

So I add the rule to the proguard-rules.pro which now looks like this:

-dontwarn org.kxml2.io.KXmlParser,org.kxml2.io.KXmlSerializer

But when I try to build it again, I get the same error with the exact same rule in the missing_rules.txt.

What is more now, I found the apk built for the emulator device and although it is much smaller now, when I open it with the APK analyzer, I could still find my own classes in plain text alongside their methods and all inside. I thought the obfuscation is enabled by default? What could be the reason for both issues? Is there a comprehensive guide somewhere on how to get configuration properly done? I searched quite a lot on the web, but the sources are pretty outdated and could not find a working solution so far. I am using Android Studio 2023.3.1 and AGP 8.2.2 and Gradle 8.2


Solution

  • The problem with the missing classes and missing_rules.txt was fixed after I updated the androidx.test.runner and androidx.test.rules libraries to 1.5.2 and 1.5.0 respectivelly.

    The issue with the code not being obfuscated was that I had to use the "Generate Signed Bundle/APK" action. So far I was trying only with the "Build Bundles/APKs". Basically when I create a "release" APK then it is obfuscated and all.