Android Obfuscation and obfuscation filtering

Source: Internet
Author: User

Code obfuscation in Android is typically proguard. It has many other useful functions besides confusing the code. The main record here is confusing related implementations.

The role of 1.ProGuard

Delete useless code, compress and optimize class file, reduce the size of apk;

Improve the reverse threshold to protect code security;

Use of 2.ProGuard

Eclipse

Before Android2.3, the obfuscation code would need to be manually added Proguard to achieve code confusion; After 2.3, Google added it to the SDK (Sdk\tools\groguard). When a project is created, Project.properties,proguard-project.txt (formerly Proguard.cfg) is automatically generated at the root of the project, and the project can be confused directly using Proguard in the Eclipse project. Enabling obfuscation requires only two simple steps:

Old version

1) Add "proguard.config=proguard.cfg" to Project.Properties

2) Adding rules for obfuscation and filtering in proguard.cfg

New version

1) Release the "#proguard. Config=${sdk.dir}/tools/proguard/proguard-android.txt in the Project.Properties file: Proguard-project.txt "in the comments

2) Adding rules for obfuscation and filtering in Proguard-project.txt

Android Studio

1) Referencing the Proguard configuration file

Android {    buildtypes {        release {            true            proguardfiles getdefaultproguardfile ( ' Proguard-android.txt '), ' some-other-rules.txt '            //proguardfile ' Some-other-rules.txt '  Configure a single file so         }    }}

2) Add Filter rule in "Proguard-android.txt"

3.ProGuard Filter Rule Configuration

-include {filename} reads configuration parameters from a given file
-basedirectory {directoryname} specifies the base directory for later relative file names
-injars {Class_path} specifies the application jar,war,ear and directory to be processed
-outjars {Class_path} Specifies the name of the Jar,war,ear and directory to output after processing
-libraryjars {classpath} specifies the library files required for the application Jar,war,ear and directories to be processed
-dontskipnonpubliclibraryclasses specifies that non-public library classes are not ignored.
-dontskipnonpubliclibraryclassmembers specifies that the members of the Library class that the package is visible are not ignored.

Retention options (Configure what is not processed)

-keep {Modifier} {class_specification} protects the specified class file and members of the class
-keepclassmembers {modifier} {class_specification} Protects members of the specified class, and if this class is protected they will be better protected
-keepclasseswithmembers {class_specification} protects the specified class and members of the class, but the condition is that all specified classes and class members are to be present.
-keepnames {class_specification} protects the names of the specified classes and members of the class (if they are not removed in the compression step)
-keepclassmembernames {class_specification} protects the names of the members of the specified class (if they are not removed in the compression step)
-keepclasseswithmembernames {class_specification} protects the name of a member of the specified class and class if all the specified class members are present (after the compression step)
-printseeds {filename} lists the members of the class and class-keep options, standard output to the given file

Compression

-dontshrink does not compress the input class file
-printusage {filename}
-whyareyoukeeping {class_specification}

Optimization

-dontoptimize does not optimize the input class file
-assumenosideeffects {class_specification} Optimizations assume the specified method without any side effects
-allowaccessmodification allows access to and modification of members of classes and classes with modifiers when optimized

Confuse

-dontobfuscate does not confuse the input class file
-obfuscationdictionary {filename} uses the keyword in the given file as the name of the method to be confused
-overloadaggressively obfuscation when applying intrusive overloads
-useuniqueclassmembernames determining the member names of a unified obfuscation class to increase confusion
-flattenpackagehierarchy {package_name} repack all renamed packages and place them in a given single package
-repackageclass {package_name} repack all renamed class files in a given single package
-dontusemixedcaseclassnames confusion does not produce a variety of class names
-keepattributes {attribute_name,...} protects the given optional attributes, such as linenumbertable, Localvariabletable, SourceFile, Deprecated, Synthetic, Signature, and innerclasses.
-renamesourcefileattribute {String} sets the given string constant in the source file

4. Places that cannot be confused

If the confusion is too serious, it may cause the program can not be used normally, so some things can not be confused (will be referenced outside the place), the following lists a few can not be confused:

Android components registered in the Androidmanifest;

Custom view that is used in XML;

Android-generated resource index file R.java;

Classes that are called by reflection, methods, etc., cannot be confused;

It is not possible to confuse the native method of communication with so through JNI;

The method of communicating with JavaScript through WebView cannot be confused;

Some other static constants, etc.;

  

Android Obfuscation and obfuscation filtering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.