The learning Path of Android development--a common tool for performance optimization

Source: Internet
Author: User

?? Android performance optimization related to the development tools are many, here are the following six tools to do a simple introduction, mainly with the Android developer options, analysis of the specific time-consuming trace view, Layout complexity tool hierarchy view, application startup time, Memory, CPU, network analysis, static code checking tool lint, and program stability Monkey. Let's start learning the tools below.

One, Android developer options

?? Andorid development tools, the first is the mobile phone comes with the developer options, as for the phone how to start the developer option, then please Baidu or Google itself, and then simple familiar with the following various options:

?? 1, take the bug report (for vulnerability Reporting): Click this option will get the current device log information files, packaging and ready to send the person we want to send. This will take 1, 2 minutes, and then you'll see a ready-to-go message, and then you can click Send.?? 2. Desktop Backup password: You can use ADB to back up and store apps and their associated data to your computer. This option enforces the need for a password backup and storage.?? 3, stay awake (keep awake): Select this check box will force the screen to wake up, as long as the connection on the USB cable or charging will not go to sleep, when the need to debug the time to not always go to unlock, quite convenient. 4. Select runtime (Choose Runtime): You can choose Dalvik or art. Art is still experimental until the Android L finally release. 5, enable Bluetooth HCI snoop log (Bluetooth HCI search log): Sometimes, developers need to obtain and analyze the Bluetooth HCI package, enable this will be on the device storage (path:/SDCARD/BTSNOOP_ Hco.log) to save the information for the package. Then you can use software like Wireshark and so on.?? 6. Process stats (program status): Any information you need to know about all the programs running on your phone. Includes RAM usage, run time, etc..?? 7, USB debugging (USB debugging): The use of USB debugging, the use of DDMS and adb command time also need this enable, do not check this can not debug our application. 8, Revoke USB debugging authorizations (recall USB error-removing authorization): When the first USB cable connected to the computer, the need to authenticate and establish a key, this operation to remove the authentication, forced to re-come again. 9. Power Menu bug reports (include vulnerability report in power menus): Include the option for vulnerability reporting in the Power menu. 10, allow mock locations (allows virtual location): Allow manual location information, for applications that need to simulate the location is very convenient, you can do some bad things, you know. 11. Select Debug App (choose the application to be debugged): This setting lets you choose an application to be debugged. 12. Wait for debugger (waiting for the debugger): Waiting for the debugger?? 13, show touches (display touch position): Show the location of the touch, is generally a small circle. 14, PointerLocation (show pointer position): Displays the coordinates of the current position. 15. Show Surface updates (display screen update): Flashes when the screen is updated. 16. Show layout bounds (display the boundary of the layouts): Show clipping boundary, edge. 17, Force RTL layout direction (forced RTL layouts): interface layout from right to left. 18, Windows Animation Scale (window animation ratio): The proportion of window animation, the smaller the number, the faster. 19, Transition Animation Scale (transition animation ratio): The proportion of transition animation, ibid.?? 20, simulate secondary displays (analog two-level display): Allows developers to simulate the performance of different sizes. 21. Force GPU rendering (forced GPU rendering): Force the app to use hardware 2D rendering unless the application itself does not need.?? 22. Show GPU View updates (display GPU views update): Any hardware GPU that draws a view will have a red overlay. 23. Show hardware layer updates (Show hardware layer update): This setting will tell you when the hardware layer is being updated. 24. Debug GPU Overdraw (debug GPU over-rendering): Excessive rendering occurs at any time when the application requests the system to draw some views on top of other views. 25, Force 4x MSAA (start 4x MSAA): This setting enforces multi-sample antialiasing. MSAA is the pixel that looks for the edge of the object, and then zooms the picture to the current display. Will reduce the performance to improve the visual beauty.?? 26, Strict mode enabled (Strict): The application on the main thread to perform a long operation screen will blink. 27, Show CPU usage (show CPU usage): Display the current CPU information in the upper right corner of the screen. 28. Profile GPU Rendering (GPU display configuration file): This setting can be displayed on the screen or written to a file. 29, enable OpenGL traces (start OpenGL tracking): This setting is to observe OpenGL errors, and to save the log to the file. 30. Don ' t keep activities (do not keep active): Clean up each activity immediately after the user leaves. 31. Background process limit (restrict background process): An option that allows the number of processes currently running in the backgroundChoose.?? 32, Show all Anrs (Display all ANR): When the application has a bug back to show the app unresponsive dialog box. Second, the application of specific time-consuming tool trace view

?? Trace view is used to find out where the program runs, and it shows two panels, one timeline panel, describing the time each thread and method starts and ends. The other is the profile panel, which provides a summary of what has happened inside all of the methods.
?? Create a new project Performancetool, then create a new layout main_activity_layout.xml, a button to load a Web page, and another page is a webview.

?? Then write the code, first of all Mainactivity.java:
 PackageCom.jared.performancetool;ImportAndroid.databinding.DataBindingUtil;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;Importcom.jared.performancetool.databinding.MainBinding; Public  class mainactivity extends appcompatactivity {Mainbinding binding;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); binding = Databindingutil.setcontentview ( This, R.layout.activity_main);    Initview (); }Private void Initview() {Binding.btnLoad.setOnClickListener (NewView.onclicklistener () {@Override             Public void OnClick(View view) {Webviewactivity.launch (mainactivity). This);    }        }); }}

?? Then the Webviewactivity.java:

 PackageCom.jared.performancetool;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.databinding.DataBindingUtil;ImportAndroid.os.Bundle;Importandroid.support.annotation.Nullable;Importandroid.support.v7.app.AppCompatActivity;Importcom.jared.performancetool.databinding.WebViewBinding;/** * Created by Jared on 2017/1/10. * * Public  class webviewactivity extends appcompatactivity {Webviewbinding binding;@Override    protected void onCreate(@Nullable Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); binding = Databindingutil.setcontentview ( This, r.layout.activity_webview_layout);    Initview (); }Private void Initview() {BINDING.WEBVIEW.LOADURL ("Http://www.hoolay.cn/ihoolay"); } Public Static void Launch(Context context) {Intent Intent =NewIntent (context, webviewactivity.class);    Context.startactivity (Intent); }}
1. View Form

?? There are two ways to use TraceView, one is to start and stop trace directly through the DDMS tool, using the following methods:
?? Then run the program and run it successfully, click on the menu bar of tools, Android, Android Device Monitor:

?? After entering monitor, click Ddms:?? This time on the left panel of Monitor will appear?? Click the package name that corresponds to the trace, and then click the button on the left of the stop with a little red dot. Click OK after the execution of the trace, then click on the app to load the page button, after loading, again click on the stop to the left of the button to become a small black point. Then trace completed, you can view the following two panels. is the timeline panel, the left side is each line is displayed a thread, right corresponding to its timeline. It can be seen that the main thread is doing things. Is the profile panel, the first is the name on the left, such as the ordinal number 86, where there are parents and children, where parents is called the method, where Initview is called in OnCreate, Children is the method called internally by this method, and Loadurl is called here. Exactly match the actual code in the project. Then look at the right row of incl CPU time, EXCL CPU time, incl real time, Excl real time, Call+recur calls/total, Cpu time/call, Real time/call. Incl CPU time: How long a method consumes the CPU, including internal calls to other methods to consume CPU time.? EXCL CPU Time: How long a method consumes the CPU, and does not include the CPU time that is consumed by the internal call to other methods. Incl Real time: A method that is running at the actual times (in MS), containing the actual time taken by other methods inside the call. EXCL Real Time: The real time (in MS) that a method runs, and does not contain the actual times that internal calls to other methods occupy. Call+recur Calls/total: The number of times a method is called and the percentage of the total number of calls that a recursive call takes. CPU Time/call: The proportion of time and frequency that a method consumes the CPU. Cpu Time/call: A method occupies a percentage of the CPI real time and the number of calls. 2. Code Form

?? The other is implemented by injecting code:
?? Add two lines of code to Webviewactivity's OnCreate code as follows:

@OverrideprotectedvoidonCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    binding = DataBindingUtil.setContentView(this, R.layout.activity_webview_layout);    Debug.startMethodTracing("perform");    initView();    Debug.stopMethodTracing();}

?? Do not forget to add the SD card read and Write permissions, and then run the app, click the Load Page button, will be in the/sdcard directory production of perform.trace files, through the ADB pull can be the file guide.

?  ~ adb pull /sdcard/perform.trace Downloads/

?? This is exported to the downloads directory and then selected by Androidmonitor's File->open file to open:

?? Then you can view the analysis. The advantage of the code is that we want to test it more precisely, but the price is to write code compile, export, the step is relatively cumbersome. and the direct use of the tool is only a approximate range, but convenient and fast. One of these can be used as appropriate. Third, layout complexity tool hierarchy View

?? The previous article, "The layout of Android performance optimization," has been talked about the simple use of hierarchy view. Here's how to use it. Hierarchy view is mainly to detect the layout complexity, the layout of the various views of the time-consuming situation of a tool, it needs to be on the simulator, the real machine seems to have a way to solve, but very lazy not to toss, mainly learning tools mainly.
?? Click menu Tools, Android, Android Device Monitor:

?? Then go to Android Device Monitor and select Hierarchy View?? When selected, a hierarchy tree appears. Then we click on the button to load the page, you can see the corresponding loading time, respectively, measure,layout and draw time-consuming. We can optimize our interface based on its time-consuming. Iv. application Start-up time

?? Generally we will be more concerned about an app startup time, if the boot is too slow, the user may not want to play, here is a way to test the application start time:
?? The command line is as follows:

start -W packagename/activity

?? Here is an example, for example, the Performacetool app in the above project, then first open the terminal, connect the USB cable, and then the terminal input:

?  com.jared.performancetool/.MainActivity

?? Wait a moment, the app will start and output the following information:

Starting:Intent {act=android. Intent. Action. MAINCat=[android. Intent. Category. LAUNCHER] Cmp=com. Jared. Performancetool/. Mainactivity}Status:OkActivity: com. Jared. Performancetool/. MainactivityThistime: 158TotalTime: 158WaitTime: 172Complete

?? As shown above you can see the time of 158ms.

V. Memory, CPU, Network

?? Most of the time we need to understand the memory usage of the app, how CPU is occupied, and how fast the network is. In order to improve performance, we always want to reduce the memory to a minimum, the CPU usage is near possible small, the network is suitable. Of course, these can be implemented with the command line, after all, Androd based on the Linux operating system, but that is very cumbersome and not very intuitive, it is fortunate that Android studio comes with features.
?? Android Monitor has Logcat and monitors on Android, and monitors shows the MEMORY,CPU and the network. As shown in the following:

?? This shows how much memory is used, how much CPU is consumed, and how fast the network is. is a click on the Load page when the distribution of the situation. It can help us to view and optimize our program, which makes it very intuitive. Vi. Static code checking tool lint

?? Lint mainly provides a set of static code analysis tools, it can help us to check the problems existing in the project, let us more normative development of the app. Run the menu Analyze->inspect Code.

?? At a moment's rest the system will be analyzed as follows:?? The lint is divided into many children. Internationalization internationalization issues, hard-coded text is required to add @string resources, security issues, usability usability issues, unused resources, resources, and so on. Specific can be based on the analysis of the results of a step-by-step improvement. Vii. Program Stability: Monkey

?? Through the monkey to the program before submitting the test to do self-testing, you can detect the obvious cause of instability in the problem, the execution of monkey only need a line of command, before submitting the test run can avoid the application just submitted to be called back.

?  com.jared.performancetool500

?? -P indicates the package name, and-V indicates that feedback level 500 is 500 pseudo-random events
?? If the program crashes or receives any out-of-control anomalies during the stress test, it will stop automatically.

Reference:
Https://developer.android.com/studio/profile/traceview.html#traceviewLayout
http://www.jianshu.com/p/07b551ee260b

The learning Path of Android development--a common tool for performance optimization

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.