Android application development code confusion

  • 2020-06-03 08:20:19
  • OfStack

Confusion (ProGuard)

Obfuscators compress, optimize, and obfuscate code by removing unused code and renaming classes, fields, and methods with obscure names. The result is a.apk file that is smaller than the.project file, which is harder to reverse. Therefore, obfuscators are an important protection when your application is security-sensitive, such as when you authorize an application.

The obfuscator is integrated into the android build system, so you don't have to call it manually. The obfuscator only executes when building an application in publish mode, so you don't have to deal with obfuscated code when building an application in debug mode. Making the obfuscator run is optional, but is recommended.

1. Change the project. properties


  # This file is automatically generated by Android Tools.
  # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
  #
  # This file must be checked in Version Control Systems.
  #
  # To customize properties used by the Ant build system edit
  # "ant.properties", and override values to adapt the script to your
  # project structure.
  #
  # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
  #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

  # Project target.
  target=android-19

Remove the stare in front of proguard.config

2. Change the proguard - project. txt


  # To enable ProGuard in your project, edit project.properties
  # to define the proguard.config property as described in that file.
  #
  # Add project specific ProGuard rules here.
  # By default, the flags in this file are appended to flags specified
  # in ${sdk.dir}/tools/proguard/proguard-android.txt
  # You can edit the include path and order by changing the ProGuard
  # include property in project.properties.
  #
  # 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 *;
  #}

Suppose that the third party's 'jar' package is used in the program, after the confusion leads to an error, then we need to carry out the corresponding configuration in proguard-ES31en.txt, so that it does not confuse the corresponding jar package when confused. Explain the relevant configuration in the configuration file as follows:


```java
  -keep public class * extends android.app.Activity [Class that does not confuse the class name, keeping its original class name and package name] 

  -keep public abstract interface com.asqw.android.Listener{
  public protected <methods>;  【 all public protected Method name is not confused.] 
  }
  -keep public class com.asqw.android{
  public void Start(java.lang.String);  [Do not confuse the method] 
  }
  -keepclasseswithmembernames class * {  For all classes native Method names are not confused] 
  native <methods>;
  }
  -keepclasseswithmembers class * {  [Do not confuse the method names of all the specified methods of the class] 
  public <init>(android.content.Context, android.util.AttributeSet);
  }
  -keepclassmembers class * extends android.app.Activity { [Do not confuse the method names of all the specified methods of the class] 
  public void *(android.view.View);
  }
  -keepclassmembers enum * { On enumerated types enum The following methods of all classes of the specified method name are not confused] 
  public static **[] values();
  public static ** valueOf(java.lang.String);
  }
  -keep class * implements android.os.Parcelable { Yes, it did Parcelable The class name of all classes of the interface is not confused, and its member variable is Parcelable$Creator Variable names of member variables of type are not confused. 
  public static final android.os.Parcelable$Creator *;
  }
  -keepclasseswithmembers class org.jboss.netty.util.internal.LinkedTransferQueue { [Do not confuse the variable name of the specified variable of the specified class] 
    volatile transient org.jboss.netty.util.internal.LinkedTransferQueue$Node head;
    volatile transient org.jboss.netty.util.internal.LinkedTransferQueue$Node tail;
    volatile transient int sweepVotes;

  }
  -keep public class com.unionpay.** {*; } 【 to com.unionpay All classes under the package are not confused, that is, class names are not confused, method names and variable names are not confused.] 
```    

The four components are still there. Since the four components are configured in the manifest file, it is assumed that the confusion cannot be found according to the configuration of the manifest file.

Assumptions about 1 in some of your own code to provide out let others through reflection to invoke methods, we don't want to part of the code are confused, or we use the third party provided jar package, as a result of the third party jar package 1 is has been confused, if we get confused will be an error, so we have to ensure that content without confusion, here we just need to change the file, then add 1 behind the words, he wouldn't confuse our content is given


-keepattributes *Annotation*     
-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.app.backup.BackupAgent
-keep public class * extends android.preference.Preference
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Fragment
-keep public class com.android.vending.licensing.ILicensingService
-keep class com.itheima.mobilesafe.engine.AppInfoProvider
-keep class net.youmi.android.** {
*;
}


Related articles: