Formats XML files according to Android Studio's default formatting rules. By default, also re-orders the attributes to specify the android:id, android:layout_width and android:layout_height first. This can be turned off with a command line option.
- Java 8 or higher
- Maven 3.6 or higher
mvn clean packageto create an executable JAR file with all dependencies attarget/android-xml-formatter-1.0-SNAPSHOT-jar-with-dependencies.jar.mvn verifyruns all tests and code format checks.
To view available command line options:
java -jar target/android-xml-formatter-1.0-SNAPSHOT-jar-with-dependencies.jar --help| Option | Description |
|---|---|
--indention <n> |
Set indentation spaces (default: 4) |
--attribute-indention <n> |
Set attribute indentation spaces (default: 4) |
--attribute-order <list> |
Comma-separated attribute order (default: id,layout_width,layout_height) |
--attribute-sort |
Sort attributes alphabetically |
--namespace-order <list> |
Comma-separated namespace order (default: android) |
--namespace-sort |
Sort namespaces alphabetically |
Format an XML file:
java -jar target/android-xml-formatter-1.0-SNAPSHOT-jar-with-dependencies.jar path/to/layout.xmlFormat multiple files:
java -jar target/android-xml-formatter-1.0-SNAPSHOT-jar-with-dependencies.jar file1.xml file2.xml file3.xmlThis project uses Spotless Maven Plugin with Eclipse JDT formatter for Java code formatting, configured to match the AOSP (Android Open Source Project) code style.
To verify that all Java code follows the formatting rules, run mvn spotless:check.
To automatically format all Java code, run mvn spotless:apply.
This project can be used as a style check on a CI server by executing the formatter and then printing the diff:
java -jar android-xml-formatter.jar *.xml
git diff --exit-codeSee LICENSE file for details.