Developer and Text Tools

How to Format Java Code in Google or AOSP Style Without Installing Anything

A practical guide to Google and AOSP Java style: indentation, braces, import order and unused imports, and how to review formatting changes as a diff.

Saizul Amin
Saizul Amin7 Nov 2026 · 4 min read
How to Format Java Code in Google or AOSP Style Without Installing Anything

Most Java teams pick a style guide once and then spend years arguing about it in code review. The two guides you meet most often are the Google Java Style Guide and the code style of the Android Open Source Project. They agree on far more than they disagree, but the differences are exactly the ones a reviewer notices first: how far a block is indented, how a long call continues on the next line, and how the import list is grouped. This guide explains those rules and shows how to apply them to a file in a few minutes with the Java Code Formatter, which runs in the browser and shows a diff before it changes anything.

Indentation and continuation lines

Google style indents each block by two spaces. When a statement is too long for one line and continues on the next, the continuation is indented by four spaces, twice the block indent, so a reader can tell a wrapped expression from a new statement. AOSP doubles both numbers: four spaces per block and eight for a continuation. Neither guide allows tabs.

A useful habit when you switch a project between the two is to change only the indentation in one commit. Mixed commits that move braces, reorder imports and change logic at the same time are hard to review, and a pure re-indentation commit can be skipped by blame tools.

Braces and else

Both guides use the "Egyptian" or K&R brace style: the opening brace goes at the end of the line that starts the block, and the closing brace starts its own line. An else, catch or finally follows the closing brace on the same line, as in "} else {". Both also require braces around the body of every if, for, while and do statement, even when the body is a single line. The formatter lists every braceless body it finds so you can add the braces yourself, because adding them automatically would change the shape of your code more than a formatter should.

Import order

Google style keeps it simple. All static imports go in one block, then one blank line, then every other import in a single block, each sorted in ASCII order. There are no further groups and no wildcard imports.

AOSP adds groups. Static imports still come first. After them come Android imports, then third-party packages such as com, junit, net and org, and finally the standard java and javax packages, with a blank line between groups. Inside each group the order is alphabetical.

Unused imports are a separate question. google-java-format removes them, and so does the tool when the option is on: an import is dropped when its class name appears nowhere else in the file, Javadoc links included. Static imports and wildcards are left alone because their use is harder to prove.

A short walk-through

Open the page and paste a class, or open several .java files at once. The Google preset is selected by default. The result panel immediately shows the number of lines that would change, the errors and warnings, and a side-by-side diff with the original on the left and the formatted code on the right. Tick "Only changed lines with context" to hide unchanged code in a long file.

If the diff looks right, press Apply to this file. The source box now holds the formatted code, and Ctrl+Z brings the original back. With several files open, Apply to all files formats the whole batch, and Download all as ZIP gives you the results with the same file names. Download .patch writes a unified diff you can apply with git apply.

Reading the issues tab

Formatting cannot fix everything, so the issues tab lists the rest. A missing brace is the most common: the checker matches every bracket and tells you the line and column where the unclosed one was opened, which is usually closer to the real mistake than a compiler error at the end of the file. A public class in a file with a different name is flagged before javac rejects it. Lines over the 100-column limit are listed with their length, because the built-in engine keeps your line breaks rather than rewrapping them.

When to use the official formatter

The built-in engine is deliberately conservative. It re-indents and re-spaces lines, moves braces and sorts imports, but it never joins or splits lines. If you want long expressions wrapped automatically, press the button for clang-format or for Prettier with its Java plugin. The page shows the download size first, loads the pinned version from jsDelivr, and runs it locally. Your code is not uploaded in either case.

Strings and comments stay as they are

A formatter that touches string contents is a formatter you cannot trust. The tool reads strings, char literals, text blocks and comments as whole tokens and copies them unchanged, so a brace inside a string or a commented-out line of code never affects the indentation. That is also why formatting the same file twice gives the same result, which makes the tool safe to run on every file in a commit.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages