Aug 5, 2011

Android In-Depth : How layoutopt works ?

Hello All,

In my previous post, I have explain you how to use layoutopt. It is common question how layoutopt works.
As you all aware that Android OS is open source. so I find the source code of layoutopt. layout is developed in core java. It parse the XML file and verify set of rules.  Apache Groovy library to implemented for rule engine.

I have also attached source code with this article.

Layoutopt apply following rules on every Layout.

  1. IncorrectHeightInScrollView.rule
  2. IncorrectWidthInHorizontalScrollView.rule
  3. InefficientWeight.rule
  4. InefficientWeight.rule
  5. NestedScrollingWidgets.rule
  6. TooManyChildren.rule
  7. TooManyLevels.rule
  8. TooManyViews.rule
  9. UseCompoundDrawables.rule
  10. UselessLayout.rule
  11. UselessView.rule

Let's see what's inside the rule file

// Rule: UselessView
// Description: Checks whether a container view can be removed.
// Conditions:
// - The node is a container view (LinearLayout, etc.)
// - The node has no id
// - The node has no background
// - The node has no children

if (node.isContainer() && node.'*'.size() == 0 && !node.'@android:id' &&
        !node.'@android:background') {
    analysis << "This ${} view is useless (no children, no background, no id)"

Note: Click on link redirect to, Click on File->Download Original to download code