If a source file contains only a single top-level class, the file name should reflect the case-sensitive name plus the .kt extension. Otherwise, if a source file contains multiple top-level declarations, choose a name that describes the contents of the file, apply PascalCase, and append the .kt extension.
It is possible to get Android Studio to adhere to these style guidelines, via a rather complex sequence of menus. To make it easier, we’ve provided a coding style that can be imported into Android Studio.
The file can be found here.
To install the file, open Android Studio Settings and go to Editor > Code Style > Kotlin, then click the gear menu and choose Import Scheme….
From now on, projects you create should follow the correct style guidelines.
Aside from the line terminator sequence, the ASCII horizontal space character (0x20) is the only whitespace character that appears anywhere in a source file. This implies that:
All other whitespace characters in string and character literals are escaped.
Tab characters are not used for indentation.
Special escape sequences
For any character that has a special escape sequence (\b, \n, \r, \t, \’, \”, \, and \$), that sequence is used rather than the corresponding Unicode (e.g., \u000a) escape.
Non-ASCII characters
For the remaining non-ASCII characters, either the actual Unicode character (e.g., ∞) or the equivalent Unicode escape (e.g., \u221e) is used. The choice depends only on which makes the code easier to read and understand. Unicode escapes are discouraged for printable characters at any location and are strongly discouraged outside of string literals and comments.
The Kotlin Style Guide for Android covers important updates, such as:
1. Naming
When the source file contains a single top-level element (e.g. class) then the file name should reflect the case-sensitive name with a .kt extension. For m
2. Special Characters
Kotlin supports special characters, numbers, and Booleans that can be represented as primitive values during runtime. We can easily call properties and member functions on any variables.
Whitespace Characters
The ASCII horizontal space character (0x20) is the whitespace characters that appear in a source file. It means other whitespace characters in literals and strings are escaped. The tab characters are not used for indentation. The special escape sequence for any character has the sequence \b, \n, \r, \t, \’, \”, \\, and \$ rather than the corresponding Unicode \u221e.
Non-ASCII Characters
The Unicode characters (e.g. ∞) or the Unicode escape (e.g. \u221e) are used for the Non-ASCII characters. The perfect use of both characters depends upon the code, which is easier to read and understand. Unicode escapes discourage the printable characters as well as string literals and comments.
3. Formatting
Kotlin offers support for the use of UTF-8 character encoding in the source file. Soft tabs are used, tab characters which are converted to spaces. Every major element in the Kotlin language is defined and separated from the single blank line. Single line lambdas /class/objects/enums use curly braces on the same lines, such as: