Skip to content

Code Style

Spotless

We use Spotless to maintain the Java/Kotlin coding style in the codebase. Run the following command to check the codebase:

./gradlew spotlessCheck

and run the following command to apply fixes to the violations:

./gradlew spotlessApply

License Headers

Spotless maintains the license headers for Kotlin files. Use addlicense to maintain license headers in other files:

addlicense -c "Google LLC" -l apache .

Kotlin style

The codebase follows google-java-format instead of the Kotlin coding conventions because google-java-format is strict and deterministic, and therefore removes formatting as a concern for developers altogether.

If you would like Android Studio to help format your code, follow these steps to set up your Android Studio:

  1. Install and configure the ktfmt plugin in Android Studio by following these steps:
    1. Go to Android Studio's Settings (or Preferences), select the Plugins category, click the Marketplace tab, search for the ktfmt plugin, and click the Install button
    2. In Android Studio's Settings (or Preferences), go to Editorktfmt Settings, tick Enable ktfmt, change the Code style to Google (Internal), and click OK
  2. Indent 2 spaces. In Android Studio's Settings (or Preferences), go to EditorCode StyleKotlinTabs and Indents, set Tab size, Indent and Continuation indent to 2, and click OK.
  3. Use single name import sorted lexigraphically. In Android Studio's Settings (or Preferences), go to EditorCode StyleKotlinImports, in Top-level Symbols and Java statics and Enum Members sections select Use single name import option, remove all the rules in Packages to Use Imports with '*' and Import Layout sections and click OK.

Now you can go to CodeReformat code, or press Ctrl+Alt+L (⌘+⌥+L for Mac) to automatically format code in Android Studio.

Note that you don't have to do any of these. You could rely on spotless to format any code you want to push. For details see below.

XML style

We use prettier's XML plugin to format the XML code. At the moment we have not discovered an Android Studio style configuration that would produce the same result. As a result, please run ./gradlew spotlessApply to format the XML files.