layoutopt

layoutopt is a command-line tool that helps you optimize the layouts and layout hierarchies of your applications. You can run it against your layout files or resource directories to quickly check for inefficiencies or other types of problems that could be affecting the performance of your application.

To run the tool, open a terminal and launch layoutopt <resources> from your SDK tools/ directory. In the command, supply a list of uncompiled resource xml files or directories that you want to analyze.

When run, the tool loads the specified XML files and analyzes their layout structures and hierarchies according to a set of predefined rules. If it detects issues, it outputs information about the issues, giving filename, line numbers, description of issue, and for some types of issues a suggested resolution.

Here's an example of the output:

$ layoutopt samples/
samples/compound.xml
   7:23 The root-level <FrameLayout/> can be replaced with <merge/>
   11:21 This LinearLayout layout or its FrameLayout parent is useless
samples/simple.xml
   7:7 The root-level <FrameLayout/> can be replaced with <merge/>
samples/too_deep.xml
   -1:-1 This layout has too many nested layouts: 13 levels, it should have <= 10!
   20:81 This LinearLayout layout or its LinearLayout parent is useless
   24:79 This LinearLayout layout or its LinearLayout parent is useless
   28:77 This LinearLayout layout or its LinearLayout parent is useless
   32:75 This LinearLayout layout or its LinearLayout parent is useless
   36:73 This LinearLayout layout or its LinearLayout parent is useless
   40:71 This LinearLayout layout or its LinearLayout parent is useless
   44:69 This LinearLayout layout or its LinearLayout parent is useless
   48:67 This LinearLayout layout or its LinearLayout parent is useless
   52:65 This LinearLayout layout or its LinearLayout parent is useless
   56:63 This LinearLayout layout or its LinearLayout parent is useless
samples/too_many.xml
   7:413 The root-level <FrameLayout/> can be replaced with <merge/>
   -1:-1 This layout has too many views: 81 views, it should have <= 80!
samples/useless.xml
   7:19 The root-level <FrameLayout/> can be replaced with <merge/>
   11:17 This LinearLayout layout or its FrameLayout parent is useless

The layoutopt tool is available in SDK Tools, Revision 3 or later. If you do not have SDK Tools r3 or later installed in your SDK, you can download it from the Android SDK repository site using the Android SDK and AVD Manager. For information, see Adding SDK Components.

Usage

To run layoutopt against a given list of layout resources:

layoutopt <list of xml files or directories>

For example:

$ layoutopt res/layout-land
$ layoutopt res/layout/main.xml res/layout-land/main.xml
↑ Go to top