Code compression confusion and Packaging Optimization for Android ProGuard

Posted by blindtoad on Sat, 06 Jul 2019 19:02:04 +0200

Preface

This is an actual Demo exercise for sharing only. Please give it to Star if you like.
demo link: https://github.com/AnyLifeZLB/AndroidAppFrameWork

Why start ProGuard (all trials are in AS environment)

Code compression is provided through ProGuard, which detects and removes unused classes, fields, methods, and attributes from encapsulation applications. Include unused items in the built-in code base (which makes it a useful tool for resolving 64k reference limitations in a workaround).ProGuard You can also optimize byte codes, remove unused code instructions, and confuse the remaining classes, fields, and methods with short names.Confused code This makes it difficult for your APK to be reverse engineered, which is particularly useful when applying security-sensitive features such as license validation.
In short: Proguard can confuse code, reduce Apk size, and optimize

Enable Compressed Code Step [1]

First of all, you should be in the project's Gradle Definitions are as follows in
android {
    buildTypes {
        release {
            minifyEnabled true
            zipAlignEnabled true  // Zipalign optimization
            shrinkResources true // Remove useless resource files, must be used with minifyEnabled

            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}

>Note that code compression slows down the build, so you should avoid using it in debug builds whenever possible.However, it's important that you do >The final APK for testing enables code compression because errors may be introduced if you do not fully customize the code to be retained >Note: Android Studio will disable ProGuard when using Instant Run.

Enable Compressed Code Step [2]

After defining the release in the gradle file, we'll see a line of code like this >proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'

  • proguard-rules.pro is a confusion profile in your project. You can configure your confusion rules based on your project, of course This is templatable, see here for a configuration example Template
  • proguard-android.txt is the default configuration for the development environment. You can view its contents and you can also use it Proguard-android-optimize.txt; I recommend using proguard-android-optimize.txt, which is a more rigorous confusion optimization. The risk is that errors may cause some phones to fail!The difference between the two click here

Simple template

I recommend configuring proguard-android-optimize.txt in gradle.Risk?Risks are everywhere. Come on.
A simple template reference is as follows: Click to see a simple confusion template with a real Demo to practice with

Special description

Add the following configuration after using proguard-android-optimize, otherwise use Retrofit 2(Base on OKHTTP) for Http requests
There are times when methods cannot be passed through internally, so you can try removing them

  -keepclasseswithmembers class * {
      @retrofit2.http.* <methods>;
  }

Differences between proguard-android-optimize.txt and proguard-android.txt

The simple difference is that proguard-android-optimize.txt turns on Optimization (no configuration-dontoptimize) and Optiizations filtering algorithm configured:

These two files can be found in the development environment directory, you can look at them yourself

------------------------proguard-android.txt---------------------------------
# This does not require translation, it is simple to configure dontoptimize (do not turn on optimization)
# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).

# Do not turn on Optimization do not optimize
-dontoptimize
# Do not turn on pre-validation, the mobile end does not need to be turned on.
-dontpreverify

# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

------------------------proguard-android-optimize.txt---------------------------

# Optimizations: If you don't want to optimize, use the
# proguard-android.txt configuration file instead of this one, which
# turns off the optimization flags.  Adding optimization introduces
# certain risks, since for example not all optimizations performed by
# ProGuard works on all versions of Dalvik.  The following flags turn
# off various optimizations known to have issues, but the list may not
# be complete or up to date. (The "arithmetic" optimization can be
# used if you are only targeting Android 2.0 or later.)  Make sure you
# test thoroughly if you go this route.

#Translate: If you do not need optimization, use proguard-android.txt configuration (optimization is turned off by configuration)
#Warning: There is a risk associated with adding optimizations, and not all ProGuard optimizations apply to all versions of Delvik.
#The following configurations are a series of practices that can significantly reduce risk, but not everything, so please test them in detail.
#Otherwise, don't blame me me for the problem.(Ah, the minimum API we can adapt is 15)

-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
-optimizationpasses 5
-allowaccessmodification
-dontpreverify

Topics: Android Gradle github Retrofit