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:
- Install and configure the ktfmt plugin in Android Studio by following these steps:
- Go to Android Studio's
Settings
(orPreferences
), select thePlugins
category, click theMarketplace
tab, search for thektfmt
plugin, and click theInstall
button - In Android Studio's
Settings
(orPreferences
), go toEditor
→ktfmt Settings
, tickEnable ktfmt
, change theCode style
toGoogle (Internal)
, and clickOK
- Go to Android Studio's
- Indent 2 spaces. In Android Studio's
Settings
(orPreferences
), go toEditor
→Code Style
→Kotlin
→Tabs and Indents
, setTab size
,Indent
andContinuation indent
to2
, and clickOK
. - Use single name import sorted lexigraphically. In Android Studio's
Settings
(orPreferences
), go toEditor
→Code Style
→Kotlin
→Imports
, inTop-level Symbols
andJava statics and Enum Members
sections selectUse single name import
option, remove all the rules inPackages to Use Imports with '*'
andImport Layout
sections and clickOK
.
Now you can go to Code
→ Reformat 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.