Overview

AAML - Another Android Markup Language

AAML is a tiny XML compiler (generator).

AAML is designed to help Android developers who avoid using IDEs to keep their code clean and maintainable. So, instead of editing manualy XMLs for layouts and other resources one can describe XML structure with AAML, and convert it to XML when building the project.

Quick sample

Here's an example from developer.android.com just to make you see how it looks like:

AAML:

:LinearLayout layout fill, padding 0 16dp, orientation vertical
    :EditText
        layout fill wrap
        hint @string/to
    :EditText
        layout fill wrap
        hint @string/subject
    :EditText
        layout fill 0dp 1
        gravity top
        hint @string/message
    :Button#btn_send
        layout 100dp wrap
        gravity right
        text @string/send

Resulting XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="0"
    android:paddingBottom="0"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical">
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/to" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/subject" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="@string/message" />
    <Button
        android:id="@+id/btn_send"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="@string/send" />
</LinearLayout>

Installation

AAML compiler is written in Python, so python3 is required.

To install AAML:

  1. Fetch the sources and put aaml binary somewhere in your $PATH.
  2. Create custom_rules.xml in the root of your Android project with the following content:

    <?xml version="1.0" encoding="UTF-8"?>
    <project>
     <target name="-pre-build">
         <exec executable="/home/serge/src/trikita/aaml/aaml">
             <arg value="aaml"/>
             <arg value="res"/>
         </exec>
     </target>
    </project>
    
  3. Create aaml directory and the subdirectories you need (same as in res/), e.g. (layout, values, ...)

  4. Start editing AAML files, e.g. aaml/layout/act_main.aaml
  5. Run ant debug or ant release to build the whole project. This should create (or overwrite) the res/layout/act_main.xml layout file.

Syntax

AAML elements start with a colon and indentation level defines how elements will be nested:

AAML:

:A
    :B
        :C
        :D
    :E

XML:

<A>
    <B>
        <C />
        <D />
    </B>
    <E />
</A>

To specify the attributes list write after the tag name:

AAML:

:A key1 value1, key2 value2

XML:

<A key1="value1" key2="value2" />

Comma is inserted automatically after the end of line, so the following element definitions are equal:

:A key1 value1, key2 value2

:A key1 value1
    key2 value2

:A
    key1 value1
    key2 value2

If you need to put a space or command inside the attribute value you can use quotes:

AAML:

:A foo "bar baz"

XML:

<A foo="bar baz" />

Macros and aliases

To provide some flexibility AAML has a simple macro expansion mechanism. Macro definition start with a def keyword (sorry, you can't create XML tags with the name def.

There are two kinds of macros. The first one is used to give aliases (or variables):

AAML:

:def red #ff0000
:def my_color red

:A color my_color

XML:

<A color="#ff0000" />

This kind of macros has no "parameters" and its body is just one word - a value. To make more powerful macros you can use parametric macros. One ugly trick is specifying the number of macro parameters in its name.

AAML:

:def size:2
    layout_width $1
    layout_height $2

:A size 100dp 50dp

XML:

<A layout_width="100dp" layout_height="50dp" />

Parametric macros can be overloaded, e.g. the same name can be bound to macros with different number of parameters, e.g.

AAML:

:def size:2 layout_width $1, layout_height $2
:def size:1 size $1 $1

:A size wrap_content

XML:

<A layout_width="wrap_content" layout_height="wrap_content" />

Other info

AAML is licensed under MIT license. It still is alpha, so bugs are expected and issue reports are welcome.

By the way, if you use vim - there is aaml.vim syntax file you can use to highlight AAML syntax.