Detailed explanation of code confusion examples in Android App

Receive a new task to code confuse the existing project. I have some understanding of confusion before, but it is not detailed and complete enough. I know that it is difficult to confuse some things. Fortunately, however, the current project is not too complex (for confusion), which is completed ~ ~ summarized in advance.

Part I

Introduce the following operation process (eclipse):

1. Open the obfuscator: find the project under the project root directory Properties file, delete "#" before "#proguard.config = ${SDK. Dir} / tools / Proguard / Proguard Android. TXT: Proguard project. TXT";

2. Modify the obfuscation configuration file: find the project - project. Directory under the project root directory Txt file, modify the code, this part is the most critical;

3. Save relevant files for future use in case of errors: mainly the exported APK file, the file in the Proguard directory under the project root directory (mainly mapping. Txt) and the project source code;

4. Error handling during project operation: locate the error location according to the error information and the mapping saved in step 3.

After knowing this, we expand it. Open eclipse and create a new project. By default, you will create a Proguard project Txt and project properties。 Write our code, and then put Proguard project Txt "# Proguard. Config = ${SDK. Dir} / tools / Proguard / Proguard Android. TXT: Proguard project. TXT" delete the "#" before the line, and finally export to confuse the code, even if we don't write a Proguard project Txt. Here is my test code:

Package and decompile. Finally, we get the following code:

Analyzing the above code, we will find that the custom method names are replaced with short letters without special meaning, while the oncreate () method of activity remains unchanged; The last testerror () method was eliminated because we didn't call it. These are the default obfuscation strategies. See here, feel confused or small case ha ~

Continue, we will open the unregistered testerror() and report an error when it is packaged and run. The error information is as follows:

Because this example is relatively simple, it is easy to see where the problem is, but it can still be used to explain the problem we want to express: how to restore the error information of the confused code. To achieve this goal, we need three files: Android SDK windows toolsproguard  in etrace. bat、mapping. Txt and the above error message (log. Txt). Then execute the following command (window system):

retrace. bat mapping. txt log. txt

From the figure above, we can clearly see that the B () method in the error log is the setname () method in our actual code.

It should be noted here that each export of APK will generate a corresponding mapping file in the Proguard folder in the project directory, so we need to save the corresponding mapping file for each APK. So far, the whole process of confusion has been introduced.

reference resources:

Official documents: http://developer.android.com/tools/help/proguard.html

Translation of official documents: http://www.cnblogs.com/over140/archive/2011/04/22/2024528.html (I wanted to turn it over myself, but I found that uncle farmer had translated it a long time ago. I quote it directly and thank you for it.)

Part II

The first part talks about how to operate. You can basically master it by referring to the official documents. The remaining and most difficult is Proguard project Txt file preparation. For this part, there are two processing strategies: write it yourself and use it written by others. Let's talk about how to use the third-party libraries written by others. Whether open source or closed source, we can find the configuration of confused code in his user guide in case of special circumstances. For example, we quoted the famous guillep pulltorefresh, and we can find the following code in his file:

With this part of the code, we can directly copy and insert it into our project. This method is still copy. Let's take a small example to see how to write your own code and whether control is confused. Still use the example in the first part, we are in the project guard project Txt file (empty before) add the following lines ("#" in proguard-project.txt represents comments):

Then we export APK and decompile to get the following code:

Compared with the previous ones, we find that the getString method is not confused. Yes, it's Proguard project Txt means to keep the getString () method of mainactivity from being confused. You can also try the effect of the two annotated lines in the above confused code.

At this point, we have begun to cover the core part of Proguard. The rest is to study Proguard's documents, master his grammar and use it. I wanted to find a complete translation document of Proguard, but I haven't found one for a long time. Moreover, there are very few scattered translations. Recently, time is very tight and the ability is limited. It's also very difficult to translate several commonly used commands, so the idea of careful reading can only be postponed temporarily. Here is a brief introduction to the keep command:

-keep [,modifier,...] class_ specification

Specify the classes or class members (properties and methods) that are reserved as pointcuts in your code. For example, to maintain an application, you can specify the main class and its main method. In order to handle a library, you need to specify the elements accessed by its public.

In addition, there is a simple overview of keep and the specifications in the syntax. The class specification will tell you how to represent construction methods, properties and methods, the difference between * and "* *", and so on. For example, * means to match any class name but does not include the package separator, while * * means to match any class name and includes any number of package separators. Therefore, the code we commented above means as follows: the first line: keep com ttdevs. All methods of all classes under Proguard and classes under sub packages are not confused, and the second line remains com ttdevs. All methods and member variables of all classes under Proguard and classes under sub packages are not confused// There are many details of todo, such as - libraryjars, - dontwarn, - keepatributes, etc. These will be continued

Through this article, I hope to help readers confuse the code. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>