Efficient reading of Android source code
MindMac
In the analysis of Android source code, if there is a powerful tool assistance, it will achieve twice the result with half the effort. This article introduces some tools and methods used in the analysis of Android source code, hoping to help students in need.
Eclipse
In the process of Android application development, eclipse is generally used. Of course, Google also launched Android studio, an IDE tool developed for Android in 2013. With the help of eclipse, you can better analyze the Android source code. The download and compilation of Android source code are omitted here. There are many related tutorials on the Internet. Of course, it's better to refer to the official Google Documents. Http://source.android.com/source/downloading.html provides all the steps from source code download to compilation and operation. After compiling the Android source code, you can import the source code into eclipse for subsequent analysis. The specific steps are as follows (please refer to p7-p8, in-depth understanding of Android Volume II):
- Copy / development / IDE / eclipse /. Classpath under the Android source directory to the Android source root directory; / development / IDE, in addition to the eclipse folder, there are IntelliJ, Emacs and Xcode folders, which provide the configuration files of the corresponding ide;
Copy / development / IDE / eclipse /. Classpath under the Android source directory to the Android source root directory; / development / IDE, in addition to the eclipse folder, there are IntelliJ, Emacs and Xcode folders, which provide the configuration files of the corresponding ide;
- /The development / IDE / eclipse /. Classpath file is used by eclipse. The file content is the path of each module in the source directory. The basic content is shown in Figure 1. You can modify the content of. Classpath according to your analysis needs. For example, if you only need to analyze framework related modules, you can comment or delete some source directory paths that do not belong to the framework from the file;
/The development / IDE / eclipse /. Classpath file is used by eclipse. The file content is the path of each module in the source directory. The basic content is shown in Figure 1. You can modify the content of. Classpath according to your analysis needs. For example, if you only need to analyze framework related modules, you can comment or delete some source directory paths that do not belong to the framework from the file;
Figure 1. Basic contents of classpath file
- Click file - > New - > java project in eclipse, fill in the project name in the pop-up window and remove the check of use default location. Select location as the root directory of Android source code, as shown in Figure 2. Click finish after completion. Due to the large number of Android source files, the import process may last for a long time (depending on the performance of the machine).
Click file - > New - > java project in eclipse, fill in the project name in the pop-up window and remove the check of use default location. Select location as the root directory of Android source code, as shown in Figure 2. Click finish after completion. Due to the large number of Android source files, the import process may last for a long time (depending on the performance of the machine).
Figure 2. Importing Android source code into eclipse
Several points need to be noted:
- There will be some paths to / out / target /... In the. Classpath file, and the out directory is generated after compiling the Android source code, so before importing the Android source code into eclipse, you need to finish compiling the Android source code;
There will be some paths to / out / target /... In the. Classpath file, and the out directory is generated after compiling the Android source code, so before importing the Android source code into eclipse, you need to finish compiling the Android source code;
- Before importing the source code, please cancel the automatic compilation option of eclipse (there are many Android source files, so it's time-consuming to compile them once), and remove the check of build project automatically under project in the menu bar.
Before importing the source code, please cancel the automatic compilation option of eclipse (there are many Android source files, so it's time-consuming to compile them once), and remove the check of build project automatically under project in the menu bar.
After importing the Android source code, you can use eclipse to analyze the source code. Generally, when I analyze Android source code, I mainly use eclipse's automatic positioning function. Take the analysis of activitymanagerservice as an example. The service is created by the system server thread. The system.currenttimemillis() function is called at the main function entrance of systemserver.java (/ frameworks / base / services / Java / COM / Android / server / systemserver. Java). If you want to analyze the specific implementation process of the function, you can hold down the CTRL key and move the mouse Move to the called currenttimemillis function, and click the left mouse button to automatically locate to the source code of the system.currenttimemillis function (the automatic positioning method may vary depending on the shortcut key settings). With the auto location function, you don't need to search and find the source files by yourself.
Android SDK Search
During Android development or source code analysis, you will generally check the official Android API provided by Google. If you want to know more about the details of the API implementation, of course, the best way is to look at the source code. For online API documents, if you can also view the source code when viewing, it is certainly the best. Under Chrome browser, you can install the Android SDK search extension to achieve this goal. The screenshot of the extension in chrome online app store is shown below. After the extension is installed and Android API is searched again, a view source link will be added after the corresponding search results. For example, search the activity class, and the result is shown in Figure 4. Click the view source link to open the source code of the activity class, as shown in Figure 5. It can be seen from the web page that it is actually the GIT Figure 3 Android SDK search linked to Google's Android source code
Figure 4 Effect after adding the Android SDK search extension
Figure 5 activity source opened by view source
Library. Of course, there are many inconveniences to analyze the source code in the browser, far from the convenience of IDE such as eclipse. However, we can quickly locate the target code in the local source code according to the path given on the web page, and the / frameworks / base / core / Java / Android / APP / activity.java obtained by splicing the paths of the two red boxes in Figure 5 is the path corresponding to the local source code, so that we can use ide such as eclipse to analyze the source code. For the above situations, you can also directly associate Android source code (mainly the source code of framework) in eclipse. For example, in an Android project, you want to view the activity source code as follows:
Code:
You can use the quick positioning function provided by eclipse to view the source code of the activity class, but after clicking, the source is not found, as shown in Figure 6. For versions later than Android 4.0, when downloading the SDK, you can choose to download the source code at the same time, as shown in Figure 7. After downloading, there will be a sources directory under the Android SDK directory, under which there will be the corresponding version of Android source code, such as android-17. Click attach source, in the dialog box that appears, select external location, click external folder, and select the downloaded source directory, such as... / sources / android-17, as shown in Figure 8. Click OK to see the source information of activity class. Figure 6 source not found error
Figure 7 download the source code corresponding to Android
Figure 8 eclipse associated Android source code
Since only Android 4.0 and later versions have corresponding source directories, the source code for Android 4.0 and earlier versions cannot be obtained by downloading the SDK. Of course, there are some source compression packages before Android 4.0 that can be used on the Internet. Another more convenient way is to install the Eclipse Plug-in of Android sources (https://code.google.com/p/adt-addons/), install new software in eclipse, and add the location address as http://adt-addons.google code.com/svn/trunk/source/com.android.ide.eclipse.source.update/. Due to the large plug-in, about 240m, it may take a long time to download and install. After installation, you can view the source code for most Android versions. Android sources now supports the following versions:
- 14 - Android 4.0.1
14 - Android 4.0.1
- 10 - Android 2.3.4
10 - Android 2.3.4
- 9 - Android 2.3
9 - Android 2.3
- 8 - Android 2.2
8 - Android 2.2
- 7 - Android 2.1
7 - Android 2.1
- 6 - Android 2.0.1
6 - Android 2.0.1
- 4 - Android 1.6
4 - Android 1.6
- 3 - Android 1.5
3 - Android 1.5
AndroidXRef
With the help of eclipse and local source code, it is easy to analyze Android source code, but the call to native code can't be viewed through eclipse's quick positioning, and the loading of native library in Android source code is generally not done by calling system.loadlibrary in static block to load the library file and complete native Function is registered, so it is difficult to locate such code. One way to do this is to use tools like UltraEdit to search for specific strings in files, such as function names. For JNI functions, they usually exist in / frameworks / base / core / JNI directory. In the case of a large number of files, it's still busy to use this method to search, so here's an introduction to Android XRef. The Android XRef (http://android.com/) project provides the cross index of Android source code, which can quickly search for Android source code that meets specific conditions. The background is based on the opengrok engine, which is mainly used to search, cross index and navigate in the source code. Android XRef provides a complete index of Android source code, including kernel source code. The available source code version is displayed on the right side of the Android XRef homepage, as shown in Figure 9. Click the corresponding source code version link to enter the corresponding source code branch, as shown in Figure 10.
Figure 9 Android XRef home page
Figure 10 androidxref kitkat4.4-r1 branch
The right side of Figure 10 provides searching in a specific directory. For example, when analyzing the framework, you can select the framework directory, which can reduce the search scope and more accurately locate the required source code. On the left side are some search criteria, with the following meanings (under the specified project (s)):
- Full search: for full-text search, all words, strings, identifiers and numbers will be matched. For example, if you search "activity" through full search under frameworks, all results containing activity characters (ignoring case) will be displayed. Even comments will be displayed, as shown in Figure 11. Click the link with a green background to open the folder containing the activity string file; click the file name on the left such as widgetadder.java to open the file containing the activity string; click the search result link on each line on the right to open the corresponding file content;
Full search: for full-text search, all words, strings, identifiers and numbers will be matched. For example, if you search "activity" through full search under frameworks, all results containing activity characters (ignoring case) will be displayed. Even comments will be displayed, as shown in Figure 11. Click the link with a green background to open the folder containing the activity string file; click the file name on the left such as widgetadder.java to open the file containing the activity string; click the search result link on each line on the right to open the corresponding file content;
Figure 11 full search
- Definition: search symbol definition related codes, such as the definition of the startactivityifneeded function. The display results are shown in Figure 12. From the search results, we can see that the green font on the right indicates the method in the activity class;
Definition: search symbol definition related codes, such as the definition of the startactivityifneeded function. The display results are shown in Figure 12. From the search results, we can see that the green font on the right indicates the method in the activity class;
Figure 12 definition search
- Symbol: search symbols, for example, member variables in a class can be searched. Figure 13 shows the result of searching window? Hierarchy? Tag through symbol;
Symbol: search symbols, for example, member variables in a class can be searched. Figure 13 shows the result of searching window? Hierarchy? Tag through symbol;
Figure 13 Symbol Search
- File path: search for the file with the given string in the source file name. For example, if you want to search for the source file with activity in the file name, you can fill in activity in the file path to search, and the result is as shown in;
File path: search for the file with the given string in the source file name. For example, if you want to search for the source file with activity in the file name, you can fill in activity in the file path to search, and the result is as shown in;
Figure 14 file path search
- History: search for a given string in history log comments in Git library. This should not be very useful, just search for information from comments of Android source submitter. For example, search the "God" string, and the display result is as shown in Figure 15. However, click the source file link, only the source content will be displayed, and no history comments will be attached;
History: search for a given string in history log comments in Git library. This should not be very useful, just search for information from comments of Android source submitter. For example, search the "God" string, and the display result is as shown in Figure 15. However, click the source file link, only the source content will be displayed, and no history comments will be attached;
Figure 15 History Search
Of course, the above mentioned search methods can be used in combination. For example, if you want to search the definition of the finish function in the activity.java file, you can fill in "finish" in the definition and "activity. Java" in the file path. The search results are shown in Figure 16. Figure 16 combined search because Android XRef uses opengrok engine, it also supports some other search methods:
- +Indicates contains this string, - indicates contains this string. For example, to search for a source file that contains an activity string but does not contain a service string in full search, you can fill in + "activity" - "service" (it is also OK to remove the double quotes, but for indivisible words and spaces between them, the double quotes cannot be removed. This rule is applicable to all search rules, such as full search "final string", Double quotes remove the relation of or, which is the same as Google's search rule);
+Indicates contains this string, - indicates contains this string. For example, to search for a source file that contains an activity string but does not contain a service string in full search, you can fill in + "activity" - "service" (it is also OK to remove the double quotes, but for indivisible words and spaces between them, the double quotes cannot be removed. This rule is applicable to all search rules, such as full search "final string", Double quotes remove the relation of or, which is the same as Google's search rule);
- Boolean operations, such as and (& &), + ", or (|), not (!), and" - "(and) must be capitalized. For example, search for source files containing both final and string, and fill in" final "and" string "in full search;
Boolean operations, such as and (& &), + ", or (|), not (!), and" - "(and) must be capitalized. For example, search for source files containing both final and string, and fill in" final "and" string "in full search;
- Use wildcard characters, "?" for one character, "*" for more than one character ("?" and "*" are not available for the beginning of a string);
Use wildcard characters, "?" for one character, "*" for more than one character ("?" and "*" are not available for the beginning of a string);
- For fuzzy query, you can use "~" to search for source files that are similar to the provided string spelling;
For fuzzy query, you can use "~" to search for source files that are similar to the provided string spelling;
- Escape characters. The special characters used in opengrok include + - & & |!) {} [] ^ "~ *?: \, so if you need to search for these special characters, you can use \. For example, search (1 + 1): 2, you can use \ (1 \ + 1 \) \: 2.
Escape characters. The special characters used in opengrok include + - & & |!) {} [] ^ "~ *?: \, so if you need to search for these special characters, you can use \. For example, search (1 + 1): 2, you can use \ (1 \ + 1 \) \: 2.
For more detailed rules, please refer to the help of Android XRef (http://android.com/4.4_r1/help.jsp).
I think Android XRef is suitable for searching and locating the path of source files. As for the actual analysis of source code, of course, it will be more convenient with the help of IDE such as eclipse.
summary
This article mainly summarizes some tools and basic methods that I used in the process of Android source code analysis. It's just a one-of-a-kind advice. If there is a better way, welcome to exchange and learn!
Note: this post will be rearranged and typeset by pestone, the volunteer of the snow watching forum. If there is any discrepancy between this post and the original text, the original author's attachment shall prevail
-----Kanxue college, wechat ID: ikanxue-----