Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,71 @@
# android-xml-formatter

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.
[![GitHub check runs](https://img.shields.io/github/check-runs/ByteHamster/android-xml-formatter/main)](https://github.com/ByteHamster/android-xml-formatter/actions/workflows/checks.yml?query=branch%3Amain)
[![License: GPL v3](https://img.shields.io/github/license/ByteHamster/android-xml-formatter)](https://www.gnu.org/licenses/gpl-3.0)
[![GitHub Release](https://img.shields.io/github/v/release/ByteHamster/android-xml-formatter)](https://github.com/ByteHamster/android-xml-formatter/releases)

To view available command line options, execute `java -jar android-xml-formatter.jar --help`.
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.

Can be used as a style check on a CI server by executing and then printing the diff.
## Requirements

- Java 8 or higher
- Maven 3.6 or higher

## Building

- `mvn clean package` to create an executable JAR file with all dependencies
at `target/android-xml-formatter-1.0-SNAPSHOT-jar-with-dependencies.jar`.
- `mvn verify` runs all tests and code format checks.

## Usage

To view available command line options:

```bash
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 |

### Example

Format an XML file:

```bash
java -jar target/android-xml-formatter-1.0-SNAPSHOT-jar-with-dependencies.jar path/to/layout.xml
```

Format multiple files:

```bash
java -jar target/android-xml-formatter-1.0-SNAPSHOT-jar-with-dependencies.jar file1.xml file2.xml file3.xml
```

## Contributing

This project uses [Spotless Maven Plugin](https://github.com/diffplug/spotless/tree/main/plugin-maven)
with Eclipse JDT formatter for Java code formatting, configured to match the
[AOSP (Android Open Source Project) code style](https://source.android.com/docs/setup/contribute/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`.

## CI Integration

This project can be used as a style check on a CI server by executing the formatter and then printing the diff:

```bash
java -jar android-xml-formatter.jar *.xml
git diff --exit-code
```

## License

See [LICENSE](LICENSE) file for details.
28 changes: 27 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@

<build>
<plugins>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.43.0</version>
<configuration>
<java>
<!-- Eclipse JDT formatter with AOSP code style -->
<eclipse>
<file>${project.basedir}/settings/eclipse-aosp-style.xml</file>
</eclipse>
<!-- Import ordering matching AOSP style -->
<importOrder>
<order>android,androidx,com.android,dalvik,libcore,com,org,java,javax,</order>
</importOrder>
<removeUnusedImports/>
</java>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
Expand All @@ -59,4 +85,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Loading