Add confusion and precautions before Android application release

Code confusion

Java code is confused with Native code, resource file drawable, xml, etc.

1. Functions: code compression, code obfuscation, resource compression and other optimization processes;

Code compression: obfuscation process removes unused classes, class members, methods and properties from the main project and dependency library, which helps avoid the bottleneck of 64K method number

Obfuscation: obfuscate the class name, attribute name, and method name into illegible letters, such as a,b,c

Resource compression: remove unused resources from projects and dependent libraries.

2. Obfuscation rules: proguard-android.txt obfuscation default rules and custom obfuscation rules in;

Note: JavaBean classes, that is, entity classes are not confused, custom controls are not involved in confusion, enumerations are not confused, and classes in third-party libraries are not confused.

3. Mudle. Gradle write obfuscation configuration;

buildTypes {
    release {
        minifyEnabled true//Open confusion
        shrinkResources true//Turn on resource compression
        zipAlignEnabled true//Reduce the memory consumption of the application at run time.
        proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

4. The obfuscated output file is in the module/build/outputs/mapping folder:

dump.txt describes the internal structure of all class files in apk file;

mapping.txt mapping between the original classes, methods, and field names and obfuscated code;

Classes and members of seeds.txt that are not confused;

Code removed from apk by usage.txt.

5. Examples of custom obfuscation rules in

#------Basic command area------
# Code obfuscation compression ratio, between 0 and 7, default to 5, generally not modified
-optimizationpasses 5
# Do not use case mixing when mixing. The mixed class name is lowercase
# Specifies whether to confuse third-party jar s by not ignoring classes of non-public Libraries
# Specifies not to ignore class members of non-public Libraries
# Preverify is one of the four steps of proguard without pre verification. Android does not need preverify. Removing this step can speed up confusion
# Whether to record the log during obfuscation, including the mapping relationship of class name - > obfuscated class name
#Obfuscation mapping file output
-printmapping proguardMapping.txt
#Keep annotations unambiguous
-keepattributes *Annotation*,InnerClasses
# Avoid confusing generics
-keepattributes Signature
# Keep the source file name and line number when throwing an exception
-keepattributes SourceFile,LineNumberTable
# Specify the algorithm to be used for obfuscation, and the following parameter is a filter. This filter is the algorithm recommended by Google, which is generally unchanged
-optimizations !code/simplification/cast,!field/*,!class/merging/*

# ------Default reserve------
#Keep the four major components we use, custom Application and so on, and these classes will not be confused
-keep public class * extends
-keep public class * extends
-keep public class * extends
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends
-keep public class * extends android.preference.Preference
-keep public class * extends android.view.View
-keep public class

# Keep all classes and their inner classes under support
-keep class** {*;}
# Keep the
-keep public class * extends**
-keep public class * extends**
-keep public class * extends**
#Fragment does not need to be registered in AndroidManifest.xml. It needs additional protection
-keep public class * extends**
-keep public class * extends

#If a v4 or v7 package is referenced, the warning can be ignored because is not available

# Keep native methods from being confused
-keepclasseswithmembernames class * {
    native <methods>;

# The method parameter retained in the Activity is the view method, so the onClick we write in the layout will not be affected
-keepclassmembers class * extends {
   public void *(android.view.View);

# Keep our custom controls (inherited from View) unambiguous
-keep public class * extends android.view.View{
    *** get*();
    void set*(***);
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);

# Keep custom control classes unambiguous
-keepclasseswithmembers class * {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);

# Keep the Parcelable serialization class from being confused
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;

#Keep Serializable
-keepnames class * implements

# Keep Serializable serialized classes unambiguous
-keepclassmembers class * implements {
    static final long serialVersionUID;
    private static final[] serialPersistentFields;
    !static !transient <fields>;
    !private <fields>;
    !private <methods>;
    private void writeObject(;
    private void readObject(;
    java.lang.Object writeReplace();
    java.lang.Object readResolve();

#Keep enumerating enum classes from being confused. If there is an error, it is recommended to directly use the above - keepclassmembers class * implements
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);

#Keep entity classes from being confused
-keep class com.mys.example.common.entity.**{*;}

#Filter R file confusion:
-keep class **.R$* {

#------Third party framework to avoid confusion------
# butterknife
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewInjector{ *; }#7 below
-keep class **$$ViewBinder { *; }#More than 7
-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
-dontwarn com.squareup.okhttp3.**
-keep class com.squareup.okhttp3.** { *;}
-dontwarn okio.**
-keep class okio.**{*;}
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
   **[] $VALUES;
   public *;
-dontwarn com.yanzhenjie.permission.**
-keep class com.yanzhenjie.permission.** { *; }
-keepclassmembers class ** {
    @com.yanzhenjie.permission.PermissionYes <methods>;
-keepclassmembers class ** {
    @com.yanzhenjie.permission.PermissionNo <methods>;
-dontwarn com.squareup.leakcanary.**
-keep class com.squareup.leakcanary.** { *; }
-dontwarn com.github.moduth.**
-keep class com.github.moduth.** { *; }
#Bugly log Sdk
-dontwarn com.tencent.bugly.**
-keep class com.tencent.bugly.**{*;}
-dontwarn com.scwang.smartrefresh.**
-keep class com.scwang.smartrefresh.** { *; }
-dontwarn cn.qqtheme.framework.**
-keep class cn.qqtheme.framework.** { *; }
-dontwarn com.github.CymChad.**
-keep class com.github.CymChad.** { *; }
-keep class** { *; }
-keep class** { *; }
-dontwarn com.pnikosis.**
-keep class com.pnikosis.** { *; }
-dontwarn com.github.zackratos.ultimatebar.**
-keep class com.github.zackratos.ultimatebar.** { *; }
# Gson related confusion configuration
-keepattributes Signature
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class** { *; }
-keep class** { *; }
-keep class * implements
-keep class * implements
-keep class * implements
# Annotation class to avoid confusion
-keep class * extends java.lang.annotation.Annotation { *; }
-keep interface * extends java.lang.annotation.Annotation { *; }
#Add according to the actual situation of the project
-keep class { *; }


Keywords: Android Java Google github

Added by satya61229 on Thu, 02 Jan 2020 20:27:13 +0200