Android Source Analysis Tool

Source: Internet
Author: User
<span id="Label3"></p><p><span style="COLOR: #333333"><span style="COLOR: #333333">title:</span></span><span style="COLOR: #000000"><span style="COLOR: #000000">"original" Android Source code analysis tools and methods <span> </span></span></span><span style="COLOR: #666666"><span style="COLOR: #666666"><br><span style="COLOR: #333333">author:</span> <span> </span> <span style="COLOR: #000000">Mindmac</span><br><span style="COLOR: #333333">time:</span> <span> </span>2014-01-02,09:32:35<br><span style="COLOR: #333333">links:</span> <span> </span>http://bbs.pediy.com/showthread.php?t=183278<br></span></span><br>In the analysis of the Android source code, if there are powerful tools to assist, will achieve a multiplier effect. This paper<br>Introduced some of the tools and methods used in the analysis of Android source code, hoping to help students in Need.<br><br><strong><strong><span style="FONT-SIZE: 18px">Eclipse</span></strong></strong><br><br>Eclipse is typically used during Android app development, but Google also launched a 2013<br>IDE Tools for Android developed by Android Studio. With Eclipse, you can better analyze the Android source code,<br>About the download and compilation of Android source code in this omission, there are many related tutorials online, of course, It is best to refer to Google<br>Official documentation, http://source.android.com/source/downloading.html provides downloads from the source code to the compiled shipping<br>All steps of the LINE. After compiling the Android source code, you can import the source code into Eclipse to facilitate subsequent analysis, with<br>In-depth understanding of Android volume II (p7-p8) is as Follows:<br> </p> <ul> <ul> <li><p>The Android source directory under The/development/ide/eclipse/.classpath Copy to the Android root directory;<br>In addition to the Eclipse folder in The/development/ide directory, there are intellij,emacs and Xcode folders,<br>The corresponding IDE configuration files are provided under these folders;</p></li> <li><p>/development/ide/eclipse/.classpath file for eclipse, file content is the source directory of the various modules<br>The path, as shown in basic content 1. Can be modified according to the needs of their analysis. classpath content, example<br>If you only need to analyze the framework-related modules, you can put some source directory routes that do not belong to the framework<br>Note or delete the path from the file;</p></li> </ul> </ul> <p><p><br><strong>Figure 1.classpath File Basic content</strong><br></p></p> <ul> <ul> <li><p>In Eclipse, click File-New Java project, and in the popup window, fill in the project Name and remove<br>Use the default location to select the location as the root directory of the Android source code, as shown in 2. Finish<br>Tighten click the Finish button, due to the many Android source files, the import process may continue for a long time (depending on<br>Machine performance).</p></li> </ul> </ul> <p><p><br><strong>Figure 2 Importing Android source in Eclipse</strong><br><br>A few things to note:</p></p> <ul> <ul> <li><p>There will be some points in the. classpath file pointing to/out/target/... The path, while the Out directory is on the Android<br>Generated after the source is compiled, so you need to complete the code before importing the Android source into Eclipse<br>The compilation of Android source code;</p></li> <li><p>Before importing the source code, Please remove the Eclipse's automatic compilation options (many Android source files, compiled over time<br>, remove the tick of Build Project automatically under the menu bar Project.</p></li> </ul> </ul> <p><br>After completing the import of Android source code, you can use Eclipse for source Analysis. In general, I'm analyzing A.<br>Ndroid source, mainly with the help of Eclipse automatic positioning Function. Taking the analysis of Activitymanagerservice as an example,<br>The service is created by the system_server thread in Systemserver.java (/frameworks/base/services/java/c<br>Om/android/server/systemserver.java) The main function entrance called the System.currenttimemillis ()<br>function, if you want to analyze the specific implementation of the function, you can hold down the CTRL key and move the mouse to the<br>Use the Currenttimemillis function, click the left mouse button, you can automatically navigate to the System.currenttimemi<br>The source code of the Llis function (depending on the setting of the shortcut key, the method of automatic positioning is different). With the automatic<br>Location feature, You do not need to search for the source files Yourself.<br><br><strong><strong><span style="FONT-SIZE: 18px">Android SDK Search</span></strong></strong><br><br>In the case of Android development or source code analysis, it is common to see Google's official Android API<br>Looking into the details of the API implementation, the best way is to look at the source Code. For online API documentation, if<br>When viewing, you can also view the source code that is of course the Best. Under Chrome, you can install Android<br>SDK Search Extender to do this, the extension is shown below in the Chrome Web Store. An<br>After the extension is loaded, after searching the Android API again, A view source is added after the corresponding search results<br>Link. For example, search Activity class, as shown in result 4. Click on the View Source link to open Activity<br>Class source code, 5, from the Web page can be seen in fact is linked to Google's Android source Git<br><br><strong><strong>Figure 3 Android SDK Search</strong></strong><br><br><br><strong><strong>Figure 4 Effects after adding the Android SDK Search Extender</strong></strong><br><br><br><strong><strong>Figure 5 View source Open Activity Source</strong></strong><br><br>Library. of course, This browser under the analysis of source code is a lot of inconvenience, far from the Eclipse and other IDE down the Convenience.<br>however, We can quickly locate the target code in the local source according to the path given on the webpage, and the two in Figure 5<br>The path of the red box after stitching The/frameworks/base/core/java/android/app/activity.java is the local source code<br>path, so you can use an IDE like Eclipse to analyze the source Code.<br>For this scenario, you can also associate the Android source code directly in Eclipse (primarily the Framework source).<br>For example, in an Android project, you want to see the Activity source code, as shown in the Following:<br></p> <p><p>Code:</p></p> <pre><pre>1. Public class Mainactivity extends Activity {2. @Override3. protected void onCreate (Bundle Savedinstancestate) {4. supe R.oncreate (savedinstancestate); 5. Setcontentview (r.layout.activity_main); 6. ...... 7.}8. }</pre></pre> <p>You can use the Quick Locate feature provided by Eclipse to view the source code of the Activity class, but it will show the source not when clicked<br>found, shown in 6. For Android4.0 later versions, when downloading the SDK, you can choose to download the source code at the same time.<br>7, after the download is complete in the Android SDK directory will have the sources directory, the corresponding version of the Android<br>Source code, such as android-17. Click Attach Source, in the dialog box that appears, select External location, Point<br>Click External folder, Select the downloaded source directory, such as .../sources/android-17, 8, tap OK<br>You can see the source information of the Activity class.<br><br><strong><strong>Figure 6 Source not Found Error</strong></strong><br><br><br><strong><strong>Figure 7 Download the corresponding source code for Android</strong></strong><br><br><br><strong><strong>Figure 8 Eclipse affiliate Android source code</strong></strong><br><br>Since only the corresponding source directory exists for Android 4.0, the previous version of the Android4.0<br>Version is unable to download the SDK way to get the source code, of course, There are some 4.0 of the source voltage before<br>Shrink packs can be used. Another more convenient way is to install the Eclipse plugin for Android Sources (https://code.goo<br>gle.com/p/adt-addons/), you can Install New software in Eclipse and add a location address of http:<br>adt-addons.googlecode.com/svn/trunk/source/com.android.ide.eclipse.source.update/. Because Plug-ins<br>larger, about 240M or so, may download the installation time Longer. When the installation is complete, it is available for most Android versions<br>To view the source Code. Android Sources now supports the following versions:<br></p> <ul> <ul> <li><p>14-android 4.0.1</p></li> <li><p>10-android 2.3.4</p></li> <li><p>9-android 2.3</p></li> <li><p>8-android 2.2</p></li> <li><p>7-android 2.1</p></li> <li><p>6-android 2.0.1</p></li> <li><p>4-android 1.6</p></li> <li><p>3-android 1.5</p></li> </ul> </ul> <p><br><strong><strong><span style="FONT-SIZE: 18px">Androidxref</span></strong></strong><br><br>The use of Eclipse and local source code makes it easier to analyze Android sources, but for Native<br>Can not be used by the rapid location of Eclipse to view the corresponding Native source, and Android source code in the Native<br>Library loading is generally not done by calling System.loadlibrary in the Static block to load the library file and completing the Native function<br>registered, so it is difficult to locate this type of Code. One way to do this is to use tools such as UltraEdit to<br>Search for specific strings, such as function names, that are commonly found in/frameworks/base/core/jni for JNI functions<br>Directory. In the case of a large number of files, using this method to search is still relatively busy, so the introduction of the next<br>Androidxref.<br>Androidxref (http://androidxref.com/) Project provides cross-index of Android source code, can quickly search<br>Specific conditions of the Android source code, background is based on the Opengrok engine, Opengrok mainly used in the source of the search<br>cross-index, and Navigation. ANDROIDXREF provides a complete index of the Android source code, including the kernel source Code.<br>The available source versions are shown on the right side of the Androidxref home page, as shown in 9, click on the corresponding source version link<br>You can enter the corresponding source code branch, 10 is Shown.<br><br><br><strong><strong>Figure 9 Androidxref Home</strong></strong><br><br><br><strong><strong>Figure ten Androidxref KITKAT4.4-R1 branch</strong></strong><br><br>Figure 10 on the right provides a search in a specific directory, for example, when analyzing the Framework, you can select frameworks<br>To reduce the scope of the search and to more accurately locate the required source Code. On the left is a number of search<br>The meaning of the cable is as follows (under the specified Project (s)):</p> <ul> <ul> <li><p>Full Search: complete text searches that match all words, strings, identifiers, and numbers, such as<br>Frameworks the "activity" is searched under full search, all included activity characters are displayed (ignoring Case)<br>results, even the comments are displayed, as shown in 11. Clicking on a green background link will open the containing<br>The folder where the activity string file is located; clicking on the left file name, such as the Widgetadder.java link, opens<br>Activity string file; Clicking the search results link on the right side of each row will also open the corresponding file content;</p></li> </ul> </ul> <p><p><br><strong>Figure one Full search</strong><br></p></p> <ul> <ul> <li><p>Definition: search symbol definition related code, such as search startactivityifneeded function definition, display<br>As shown in result 12, the green font on the right indicates that it is in the Activity class, as you can see from the search Results.<br>Method</p></li> </ul> </ul> <p><p><br><strong>Figure Definition Search</strong><br></p></p> <ol> <ol> <li><p>Symbol: search for symbols, such as the ability to search for member variables in a class, Figure 13 shows the search by symbol<br>The result of window_hierarchy_tag;</p></li> </ol> </ol> <p><p><br><strong>Figure Symbol Search</strong><br></p></p> <ul> <ul> <li><p>File Path: searches for files in the source file name that contain the given string, for example, to search for filenames containing Activity<br>SOURCE files, You can fill in the file Path with Activity to search, as shown in the results;</p></li> </ul> </ul> <p><p><br><strong>Figure File Path Search</strong><br></p></p> <ul> <ul> <li><p>History: searching for a given string in the history log comments in the Git repository is not a useful<br>Don't be big, just search for information from Android source Submitter's Comments. For example, search for the god string, Explicit<br>Shown in result 15, but clicking on the source file link will only show the source content and will not be appended to the history<br>Log Comments content;</p></li> </ul> </ul> <p><p><br><strong>Figure History Search</strong><br><br>of course, the above mentioned search methods can be used in combination, for example, to search in the Activity.java file<br>The definition of the finish function, you can fill in the definition "finish", in the File Path to fill in "activity.java", search<br>As shown in result 16.<br><br><strong>Figure 16 Combination Search</strong><br>Because Androidxref is using the Opengrok engine, It also supports some other ways of searching:</p></p> <ul> The <ul> <li> <p> +  indicates that the string containing this string is,- . For example, in  Full Search  search for source files that contain  activity <br> strings but do not contain  service  strings, you can fill in + activity   -"service" (minus the double-citation <br>), but for an indivisible word and a space between the two, double quotation marks are not allowed, and this rule applies <br> to all search rules, such as  Full Search  "final  string ", Double quotation marks remove the representation or relationship, this <br> is the same as the  Google  's search rule); </p> </li> <li> <p> Boolean operation, You can use  and (&&)," + ", OR (| |), not (!) and "-" (and , etc. must all be capitalized), such as search <br>, including both  final  and  String  source files, can  Full Search  Enter "final"  AND  "String"; </p> </li> <li> <p> Use wildcards, "?" Represents a character, "*" represents more than one character ("?"). And "*" are not available at the beginning of the string); </p> </li> <li> <p> Fuzzy query, You can use "~" to search for content that contains a source code similar to the spelling of the provided string; </p> </li> <li> <p> Escape character,opengrok  Special characters used in the + - && | include |  !  ( )  { } [ ] ^  " ~ * ? : \  therefore, If you need to search for content that contains these special characters, you can use \ to escape, such as search (2), <br> can be such \ (1\+1\) \:2. </p></li> </ul> </ul> <p><p><br>For more detailed usage rules, refer to androidxref help (http://androidxref.com/4.4_r1/help.jsp).<br><br>Personally think Androidxref is suitable for searching the path of the source files, as for the actual analysis of the source code to borrow<br>It is easier to help with Ides such as Eclipse.<br><br><strong><span style="FONT-SIZE: 18px">Summarize</span></strong><br><br>This article mainly summarizes my personal in the Android source code analysis process to use some of the tools and basic methods, purely<br>opinion, when the right, if there is a better way to welcome the exchange of learning!<br><br><br>Note: this post by the Snow Forum volunteers Pestone Re-layout of the pdf, if and the original text, the original author attachment as the subject<br><br><br><br>Some of the tools used in the Android source analysis process and the summary of the basic methods, purely opinion, of course, there are more and better ways to welcome the exchange!<br><br>Download point here: Android Source code analysis tools and METHODS. pdf* reprint please specify from the Snow forum<span> </span> @pediy.com<br></p></p> <p><p><br></p></p> <p><p></p></p><p><p>Android Source Analysis Tool</p></p></span>

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.