12

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

pz64_
  • 2,212
  • 2
  • 20
  • 43
HelloCW
  • 843
  • 22
  • 125
  • 310

3 Answers3

6

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.

Brian Yencho
  • 2,748
  • 1
  • 18
  • 19
5

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.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • Thanks! If I use e Alpha/Beta testing tracks in Play to test, in fact I have to upload my app first. :) – HelloCW Mar 23 '21 at 01:43
  • Is there a way to test without upload my app to Google Play? – HelloCW Mar 23 '21 at 01:44
  • @HelloCW Yep, use bundletool as described in the first paragraph/links of my answer. You'll [generate APKs from the AAB](https://developer.android.com/studio/command-line/bundletool#generate_apks) and then [deploy it to your device](https://developer.android.com/studio/command-line/bundletool#deploy_with_bundletool). – Ryan M Mar 23 '21 at 01:45
  • If this is the only way, I think I can use Android Studio to generate APK file instead of AAB file, and test the APK file in my real mobile phone, right? – HelloCW Mar 23 '21 at 01:50
  • Thanks! Would you please to see my added content in the question? – HelloCW Mar 23 '21 at 02:52
0

I think the easiest way to test it is to build your app in the release version and install it on your device. This is the simplest way.

Eyosiyas
  • 1,422
  • 1
  • 9
  • 25