Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test if ProGuard works correctly before I publish an app to Google Play?

I have read the article. The author think every Android app should use code shrinking.

I use the following code to shrink and obfuscate code, proguard-rules.pro is original and blank, and proguard-android-optimize.txt is original. it's default made by Android Studio.

You know that some project can work well in Android Studio but failed after publish to Google Play, you can see the article.

When an app run in Android Studio, I think ProGuard doesn't work and it doesn't shrink and obfuscate code, so the app works well in Android Studio.

When I generate .aab file for publish in Android Studio, the ProGuard will shrink and obfuscate code, but it maybe cause runtime error due to incorrectly shrink and obfuscate operation.

How can I test if ProGuard works correctly before I publish an app to Google Play ?

buildTypes {
    release {         
        minifyEnabled false
        shrinkResources false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }

    debug {
    }
}

Add content:

To Ryan M: Thanks!

It seems that I can test whether ProGuard works correctly in Android Studio by the article.

You can see Code A and Image A.

Is that right?

Code A

debugMini {
    initWith debug
    minifyEnabled true
    shrinkResources true
    proguardFiles getDefaultProguardFile('proguard-android.txt'),
            'proguard-rules.pro'
    matchingFallbacks = ['debug']
}

Image A

enter image description here

like image 428
HelloCW Avatar asked Mar 23 '21 01:03

HelloCW


People also ask

How do I get ProGuard mapping?

It should be located at build/outputs/proguard/release/mapping. txt in your application module's directory. In the latest version of ProGuard and Android Studio, the file is located at build/outputs/mapping/release/mapping.

What is ProGuard rule?

What is ProGuard? ProGuard is a free java tool in Android, which helps us to do the following, Shrink(Minify) the code: Remove unused code in the project. Obfuscate the code: Rename the names of class, fields, etc. Optimize the code: Do things like inlining the functions.


2 Answers

Install and run the minified release version of your app (see here or here for info on installing AAB files) that you upload to Google Play, not the debug version.

If you're just hitting "Run" in Studio, you're installing the debug version that doesn't (by default) have Proguard or other minification run on it. If you instead use the minified release version before uploading it to Google Play, you'll get the same behavior you will after uploading: Google Play isn't running any "extra" Proguard tasks on it after you upload.

You can also use the Alpha/Beta testing tracks in Play to test the full Play experience without publishing to a wider audience or fiddling with bundletool.

like image 54
Ryan M Avatar answered Oct 04 '22 21:10

Ryan M


There are several ways to confirm you code is being properly minimized. One is to check the youappmodule/build/outputs/mapping/release/mapping.txt file. This will include lines like

com.example.somepackage.SomeClass -> a.b.c.d:

If you see that, you know which classes are being properly obfuscated. You can also find out which classes have been removed by making sure there is no such entry for that class.

Another good way is to inspect the output APK in Android Studio. In Android Studio 4.1 you can do this by going to Build > Analyze APK and then selecting your APK that should have had Proguard run with it. You can then inspect the classes.dex file and check its contents. You can see which classes have been obfuscated and removed by directly traversing the file structure.

like image 29
Brian Yencho Avatar answered Oct 04 '22 23:10

Brian Yencho