Code obfuscation and packaging in Android Studio

Keywords: Android Gradle Fragment SDK

[+]

After two days of fumbling, I learned about the process of code obfuscation and package publishing in Android Studio, which is recorded here.

Code obfuscation:

The role of code obfuscation is not explained much. The whole process is roughly as follows:

  • Add the following code to the build.gradle file under app (minify Enabled means confusion, default is false, remember to set it to true here):
     
    Among them, proguard- Android The. txt file is the default file under the local sdk/tools/proguard folder; the prguard-rules.pro file is used to write obfuscated code;

  • The next step is to write obfuscation code in the prguard-rules.pro file:

    For an understanding of the code, you can refer to this article: Code obfuscation in detail

After the obfuscation code is written, the obfuscation is completed, and then the packaging is completed. However, when confusing, there may be various problems, mainly the obfuscation code. Because some classes involved do not need to be obfuscated, and the obfuscation will cause the error to run. So when writing the obfuscation code, we must be careful. In fact, we should pay attention to these points generally:

  • Codes that do not need to import third-party libraries, such as the annotated part of the figure above: - libraryjars libs/BaiduLBS_Android.jar, etc., need to import third-party libraries into obfuscated files to prevent errors in reading package contents when confusing, but if you do so, the following errors will be reported:
     
    Yes, he said the bag had been assigned twice.

    The reason is that third-party class libraries have been specified in the build.gradle file, and if they are specified here again, they will be repeated, so there should be no code to import third-party packages.

  • Filter out third-party packages, that is, you don't need to confuse third-party packages, such as the common android-support-v4.jar:

-ignorewarnings
-dontwarn android.support.v4.**    
-keep class android.support.v4.** { *; }  
-keep interface android.support.v4.app.** { *; }  
-keep public class * extends android.support.v4.**  
-keep public class * extends android.app.Fragment
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Here's a third-party package I filtered out of a project:

-keep class org.apache.**{*;}//filter commons-httpclient-3.1.jar
-keep class com.fasterxml.jackson.**{*;}//filter jackson-core-2.1.4.jar etc.
-keep class com.lidroid.xutils.**{*;}//filter xUtils-2.6.14.jar
--keep class com.baidu.** {*;}//filter BaiduLBS_Android.jar
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

Often confusing code about third-party packages is prompted on their github home page or in simple. For example, confusion with xUtils suggests not confusing Annotation, i.e. `keep class * extends java.lang.annotation.Annotation {*;}
There is also Baidu Map, which has been described in its official documents, requiring the following obfuscation codes: Baidu Map, Baidu Map, Baidu Map, Baidu Map, Baidu Map, Baidu Map, Baidu Map, Baidu Map

-dontwarn com.baidu.**
-keep class com.baidu.**{*;}  
-keep class vi.com.**{*;} 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
  • Next, we need to pay attention to some fixed format code, such as common activities and so on, do not confuse, beans do not confuse and so on. Look at the code:
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Fragment 
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

The main idea here is to keep some Android components as they are without confusion.

Summarize the confusion

Written in the prguard-rules.pro file is actually confusing rules, specifying which things need not be confused. Some important classes need to be confused in the code written by myself. The essence of confusion is to simplify the class names and replace the easy-to-understand class names such as DataUtil with simple words such as a,b,c.

So, if you understand this, you will understand how to write this confusing file. The general idea is: not confusing third-party libraries, not confusing system components, and generally not confusing model classes such as beans, because they are useless to others, after all, open source...

Here's the code for the entire obfuscation file:

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\sdt1\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

#Confused configuration settings - --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-optimizationpasses 5                                                       #Specify code compression level
-dontusemixedcaseclassnames                                                 #Confusion does not produce a variety of class names
-dontskipnonpubliclibraryclasses                                            #Specifies that non-public class libraries are not ignored
-dontpreverify                                                              #No pre-validation. If pre-validation is required, it is-dontoptimize.
-ignorewarnings                                                             #Shielding warning
-verbose                                                                    #Logging in confusion
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*    #optimization

#Import third-party packages, but use input jar file is specified twice error in the current version, so comment it out
#-libraryjars libs/android.support.v4.jar
#-libraryjars libs/BaiduLBS_Android.jar
#-libraryjars libs/commons-httpclient-3.1.jar
#-libraryjars libs/jackson-annotations-2.1.4.jar
#-libraryjars libs/jackson-core-2.1.4.jar
#-libraryjars libs/jackson-databind-2.1.4.jar
#-libraryjars libs/xUtils-2.6.14.jar

#There is no need to confuse third-party class libraries - ------------------------------------------------------------------------------------------------------------------------------------------------------------------
-dontwarn android.support.v4.**                                             #Remove warning
-keep class android.support.v4.** { *; }                                    #Filtering android.support.v4
-keep interface android.support.v4.app.** { *; }
-keep public class * extends android.support.v4.**
-keep public class * extends android.app.Fragment

-keep class org.apache.**{*;}                                               #Filter commons-httpclient-3.1.jar

-keep class com.fasterxml.jackson.**{*;}                                    #Filter jackson-core-2.1.4.jar, etc.

-dontwarn com.lidroid.xutils.**                                             #Remove warning
-keep class com.lidroid.xutils.**{*;}                                       #Filtering xUtils-2.6.14.jar
-keep class * extends java.lang.annotation.Annotation{*;}                   #This is the filtered comment mentioned in the xUtils document

-dontwarn com.baidu.**                                                      #Remove warning
-dontwarn com.baidu.mapapi.**
-keep class com.baidu.** {*;}                                               #Filter BaiduLBS_Android.jar
-keep class vi.com.gdi.bgl.android.**{*;}
-keep class com.baidu.platform.**{*;}
-keep class com.baidu.location.**{*;}
-keep class com.baidu.vi.**{*;}

#No need to confuse system components, etc. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keep class com.classtc.test.entity.**{*;}                                   #Filter out the entity classes you write


#--------------------------------- Protects the members of the specified classes and classes, provided that all the specified classes and class members exist - --------------------------------------------------------------------------------------------------
-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

Package release

When the obfuscation code is written, it is packaged and released. Packing should pay attention to that if there are omissions in the obfuscation code, compilation and packaging may make errors. Like the error above, the third-party package is designated repeatedly during the obfuscation, which leads to compilation and packaging errors.  
The following is the specific process of packaging:

  • As shown in the figure, find Build -> Generate Signed APK in Android Studio.  

  • Select Create new in the window that appears (if you have created it before, you can select it directly, and then enter the password below):

  • Next, write the key information window:

  • After filling in the key information, go back to the previous window, click Next, then click Finish to complete, and then wait for compilation to complete, there will be an additional app-release.apk file under the folder, which is the official version of the installation package.

At this point, packaging is completed, of course, if you need to release to major software markets, you may also need to channel packaging, this will be done later and come back to the summary.

Posted by MARIOPARTY53 on Sun, 31 Mar 2019 20:39:30 -0700