Android SDK tool: layoutopt for Layout Optimization

Source: Internet
Author: User

BKJIA: creating good-looking Android la S is not a big challenge. After you spend hours adjusting them to adapt to multiple devices, you usually don't want to adjust them any more, however, the bulky nested layout is often very inefficient. Fortunately, there is a tool in the Android SDK that helps you optimize the layout to reduce memory consumption and improve the application running performance.

 
Layoutoptimization

Optimization requires some skills. Code with good performance is important, but the cost of writing good code is usually high, you may not rush to optimize code that runs only once or temporarily. If your application is unresponsive and expensive, or slows down other applications in the system, the user will respond, and your application downloads will be affected.

Optimizing your layout as early as possible during development is a simple way to save costs and improve performance. The Android SDK provides a tool that automatically analyzes your layout, discover unnecessary layout elements to reduce layout complexity.

Step 1: Prepare

If you want to use the optimization tool provided by the Android SDK, you need to work in the command line of the development system. If you are not familiar with the command line tool, you have to work hard.

We strongly recommend that you add the path of the Android tool to the environment variable of the operating system, so that you can directly name the tool and run it, otherwise, enter the complete file path at the end of the command prompt. Now there are two tool directories in Android SDK:/tools and/platform-tools, this article mainly uses the layoutopt tool in the/tools directory. In addition, I want to say that the ADB tool is located in the/platform-tools directory.

Run layoutopt

Running the layoutopt tool is quite simple. You only need to keep up with the directory where the layout file or layout file is located as a parameter. Note that you must include the complete path of the layout file or directory, even if you are currently in this directory. Let's take a simple example:
 

 
 
  1. D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt D:\d\tools\eclipse\article_ws\Nothing\res\layout\main.xml  
  2. D:\d\tools\eclipse\article_ws\Nothing\res\layout\main.xml  
  3. D:\d\tools\eclipse\article_ws\Nothing\res\layout> 

Note: In the preceding example, the complete path of the file is included. If the complete path is not specified, NO content is output. For example:
 

 
 
  1. D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt main.xml  
  2. D:\d\tools\eclipse\article_ws\Nothing\res\layout> 

Therefore, if you do not see anything, it is likely that the file is not parsed, that is, the file may not be found.

Layoutopt output

Layoutopt's output result is just a suggestion. You can choose to adopt these suggestions in your application. Here are some examples of layoutopt's output suggestions.

Useless Layout

During the layout design, we will move various components frequently, and some components may not be used any more, such:
 

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout 
  3.     xmlns:android="http://schemas.android.com/apk/res/android" 
  4.     android:layout_width="match_parent" 
  5.     android:layout_height="match_parent" 
  6.     android:orientation="horizontal"> 
  7.     <LinearLayout 
  8.         android:id="@+id/linearLayout1" 
  9.         android:layout_height="wrap_content" 
  10.         android:layout_width="wrap_content" 
  11.         android:orientation="vertical"> 
  12.         <TextView 
  13.             android:id="@+id/textView1" 
  14.             android:layout_width="wrap_content" 
  15.             android:text="TextView" 
  16.             android:layout_height="wrap_content"></TextView> 
  17.     </LinearLayout> 
  18. </LinearLayout> 

The tool will soon tell us that the LinearLayout in LinearLayout is redundant:
 

 
 
  1. 11:17 This LinearLayout layout or its LinearLayout parent is useless  

The two numbers at the top of each row in the output result indicate the suggested row number.

Root replacement

Layoutopt output is sometimes contradictory, for example:
 

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <FrameLayout 
  3.     xmlns:android="http://schemas.android.com/apk/res/android" 
  4.     android:layout_width="match_parent" 
  5.     android:layout_height="match_parent"> 
  6.     <LinearLayout 
  7.         android:id="@+id/linearLayout1" 
  8.         android:layout_height="wrap_content" 
  9.         android:layout_width="wrap_content" 
  10.         android:orientation="vertical"> 
  11.         <TextView 
  12.             android:id="@+id/textView1" 
  13.             android:layout_width="wrap_content" 
  14.             android:text="TextView" 
  15.             android:layout_height="wrap_content"></TextView> 
  16.         <TextView 
  17.             android:text="TextView" 
  18.             android:id="@+id/textView2" 
  19.             android:layout_width="wrap_content" 
  20.             android:layout_height="wrap_content"></TextView> 
  21.     </LinearLayout> 
  22. </FrameLayout> 

This layout will return the following output:
 

 
 
  1. 5:22 The root-level <FrameLayout/> can be replaced with <merge/> 
  2. 10:21 This LinearLayout layout or its FrameLayout parent is useless  

Although the suggestions in the first line are feasible, they are not necessary. We want the two textviews to be placed vertically, so LinearLayout should be retained, while the suggestions in the second line can be adopted to delete useless FrameLayout.

Interestingly, this tool is not all-powerful. For example, in the above example, if we add a background attribute to FrameLayout and then run the tool, the first suggestion will certainly disappear, however, the second suggestion will still show that the tool knows that we cannot control the background through merging, but after checking the LinearLayout, it seems that we forgot to add a property not provided by LinearLayout to FrameLayout.

Too many views

Each view consumes memory. If too many views are arranged in one layout, the layout occupies too much memory. If a layout contains more than 80 views, layoutopt may give the following suggestions:
 

 
 
  1. -1:-1 This layout has too many views: 83 views, it should have <= 80!  
  2. -1:-1 This layout has too many views: 82 views, it should have <= 80!  
  3. -1:-1 This layout has too many views: 81 views, it should have <= 80!  

The suggestion above is that the number of views cannot exceed 80. Of course, the latest device may support so many views. However, if the performance is poor, you 'd better adopt this suggestion.

Too many nesting

Layoutopt and the Android team recommend that the layout be within 10 levels. The layout should not exceed 10 levels even for the largest tablet screen, relativeLayout may be a solution, but its usage is more complex. It is better to make it easier after the Graphical Layout resource tool in Eclipse is updated.

The following figure shows the output content of layoutopt when there are too many layoutopt la s:
 

 
 
  1. -1:-1 This layout has too many nested layouts: 12 levels, it should have <= 10!  
  2. 305:318 This LinearLayout layout or its RelativeLayout parent is possibly useless  
  3. 307:314 This LinearLayout layout or its FrameLayout parent is possibly useless  
  4. 310:312 This LinearLayout layout or its LinearLayout parent is possibly useless  

Nested layout warnings are usually accompanied by warnings of useless la S, which help to identify which la s can be removed and avoid all la s being redesigned.

Summary

Layoutopt is a quick and easy-to-use layout analysis tool to find out inefficient and useless la s. What you need to do is to determine whether to adopt the optimization suggestions provided by layoutopt, although the adoption of recommendations to make changes will not immediately significantly improve performance, there is no reason to slow down the entire application with complicated la S, and the later maintenance is difficult. The simple layout not only simplifies the development cycle, but also reduces the test and maintenance workload. Therefore, during application development, you should optimize your layout as soon as possible, and do not wait until the user feedback is returned for modification.

Original article name: Android SDK Tools: Layout Optimization Author: Shane Conder and Lauren Darcey Original article

BKJIA translations are not reprinted without authorization. For reprinted by the media partners, please indicate the source, author, and BKJIA translations and translators !]

Related Article

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.