Skip to content

Android Code Style

Chingiz edited this page May 25, 2023 · 13 revisions

1.1 Project structure

New projects should follow the Android Gradle project structure that is defined on the Android Gradle plugin user guide.

1.2 File naming

1.2.1 Class files

Class names are written in UpperCamelCase.

For classes that extend an Android component, the name of the class should end with the name of the component; for example: SignInActivity, SignInFragment, ImageUploaderService, ChangePasswordDialog.

1.2.2 Resources files

Resources file names are written in lowercase_underscore.

1.2.2.1 Drawable files

Naming conventions for drawables:

Asset Type Prefix Example
Button btn_ btn_send_pressed.9.png
Dialog dialog_ dialog_top.9.png
Divider divider_ divider_horizontal.9.png
Background bg_ bg_rounded.xml
Icon ic_ ic_star.png
Menu menu_ menu_submenu_bg.9.png
Notification notification_ notification_bg.9.png
Tabs tab_ tab_pressed.9.png

Naming conventions for icons (taken from Android iconography guidelines):

Asset Type Prefix Example
Icons ic_ ic_star.png
Launcher icons ic_launcher ic_launcher_calendar.png
Menu icons and Action Bar icons ic_menu ic_menu_archive.png
Status bar icons ic_stat_notify ic_stat_notify_msg.png
Tab icons ic_tab ic_tab_recent.png
Dialog icons ic_dialog ic_dialog_info.png

Naming conventions for selector states:

State Suffix Example
Normal _normal btn_order_normal.9.png
Pressed _pressed btn_order_pressed.9.png
Focused _focused btn_order_focused.9.png
Disabled _disabled btn_order_disabled.9.png
Selected _selected btn_order_selected.9.png

1.2.2.2 Layout files

Layout files should match the name of the Android components that they are intended for but move the top-level component name to the beginning. For example, if we are creating a layout for the SignInActivity, the name of the layout file should be activity_sign_in.xml.

Component Class Name Layout Name
Activity UserProfileActivity activity_user_profile.xml
Fragment SignUpFragment fragment_sign_up.xml
Dialog ChangePasswordDialog dialog_change_password.xml
AdapterView item ProfileAdapter item_profile.xml
Partial layout --- partial_stats_bar.xml
View SquareImageView widget_square_image_view.xml

A slightly different case is when we are creating a layout that is going to be inflated by an Adapter, e.g to populate a RecyclerView. In this case, the name of the layout should start with item_.

Note that there are cases where these rules will not be possible to apply. For example, when creating layout files that are intended to be part of other layouts. In this case, you should use the prefix partial_.

1.2.2.3 Menu files

Similar to layout files, menu files should match the name of the component. For example, if we are defining a menu file that is going to be used in the UserActivity, then the name of the file should be activity_user.xml

A good practice is to not include the word menu as part of the name because these files are already located in the menu directory.

1.2.2.4 Values files

Resource files in the values folder should be plural, e.g. strings.xml, styles.xml, colors.xml, dimens.xml, attrs.xml

2 Code guidelines

2.1 Kotlin language rules

2.1.1 Don't ignore exceptions

You must never do the following:

fun setServerPort(value: String) {
    try {
        serverPort = Integer.parseInt(value);
    } catch (e: NumberFormatException) { }
}

While you may think that your code will never encounter this error condition or that it is not important to handle it, ignoring exceptions like above creates mines in your code for someone else to trip over someday. You must handle every Exception in your code in some principled way. The specific handling varies depending on the case. - (Android code style guidelines)

At least add timber logs:

fun setServerPort(value: String) {
    try {
        serverPort = Integer.parseInt(value);
    } catch (e: NumberFormatException) {
        Timber.e(e, "Error parsing integer")
    }
    // or
    kotlin.runCatching {
        serverPort = Integer.parseInt(value)
    }
	.onFailure { e ->
	    Timber.e(e, "Error parsing integer")
        }
}

See alternatives here.

2.1.2 Catch internal exception

You should not do this:

try {
    someComplicatedIOFunction();
} catch (e: Throwable) {
    Timber.e(e, "Error occurred while making request")
    handleError(e);
}

See the reason why and some alternatives here

2.1.3 Don't use finalizers

We don't use finalizers. There are no guarantees as to when a finalizer will be called, or even that it will be called at all. In most cases, you can do what you need from a finalizer with good exception handling. If you absolutely need it, define a close() method (or the like) and document exactly when that method needs to be called. See InputStream for an example. In this case it is appropriate but not required to print a short log message from the finalizer, as long as it is not expected to flood the logs. - (Android code style guidelines)

2.1.4 Fully qualified imports

This is bad: import foo.*;

This is good: import foo.Bar;

See more info here

2.2 Kotlin style rules

2.2.1 Fields definition and naming

Fields should be defined at the top of the file and they should follow the naming rules listed below.

  • Other fields start with a lower case letter.
  • Static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES.
  • Companion object should be placed at the top of the class
  • Some common static variables should be placed above the class
  • Public static variables should be placed in companion object

Example:

private const val ARG_VALUE = "ARG_VALUE" // good
const val ARG_VALUE_2 = "ARG_VALUE_2" // don't do this!
class MyClass {

    companion object {
        const val DELAY_IN_MS = 500L
	const val ARG_VALUE_2 = "ARG_VALUE_2" // good
    }

    // do smth
}

2.2.3 Treat acronyms as words

Good Bad
XmlHttpRequest XMLHTTPRequest
getCustomerId getCustomerID
url: String URL: String
id: Long ID: Long

2.2.4 Use spaces for indentation

Use 4 space indents for blocks:

if (x == 1) {
    x++;
}

2.2.5 Use standard brace style

Braces go on the same line as the code before them.

class MyClass {
    fun function(): Int {
        if (something) {
            // ...
        } else if (somethingElse) {
            // ...
        } else {
            // ...
        }
    }
}

Braces around the statements are required unless the condition and the body fit on one line.

If the condition and the body fit on one line and that line is shorter than the max line length, then braces are not required, e.g.

// good
if (condition) body() else elseBody()

This is bad:

if (condition)
    body(); 
else
    elseBody()

if (condition) body()
else elseBody()

if (condition) {
    body()
} else elseBody()

This rule is also applied to when branches. All the branches should be written in one style. If one branch needs brackets, then brackets should be added to other ones too.

// good
when {
    conditionOne -> branchBody()
    conditionTwo -> branchBodyTwo()
}
// good
when {
    conditionOne -> {
        branchBody()
    }
    conditionTwo -> {
        branchBodyTwo()
    }
}

// bad
when {
    conditionOne -> {
	branchBody()
    }
    conditionTwo -> branchBodyTwo()
}

2.2.6 Annotations

2.2.6.1 Annotations practices

According to the Android code style guide, the standard practices for some of the predefined annotations in Java are:

  • @Suppress: The @Suppress annotation should only be used under circumstances where it is impossible to eliminate a warning. If a warning passes this "impossible to eliminate" test, the @SuppressWarnings annotation must be used, to ensure that all warnings reflect actual problems in the code.

The best practice here is also to leave some comments on why you use this annotation. More information about annotation guidelines can be found here.

2.2.6.2 Annotations style

Classes, Methods and Constructors

When annotations are applied to a class, method, or constructor, they are listed after the documentation block and should appear as one annotation per line .

/* This is the documentation block about the class */
@AnnotationA
@AnnotationB
class MyAnnotatedClass { }

Fields

Annotations applying to fields should be listed on the different line, unless the line reaches the maximum line length.

@Mock 
private val dataManager: DataManager

2.2.7 Limit variable scope

The scope of local variables should be kept to a minimum (Effective Java Item 29). By doing so, you increase the readability and maintainability of your code and reduce the likelihood of error. Each variable should be declared in the innermost block that encloses all uses of the variable.

Local variables should be declared at the point they are first used. Nearly every local variable declaration should contain an initializer. If you don't yet have enough information to initialize a variable sensibly, you should postpone the declaration until you do. - (Android code style guidelines)

2.2.8 Order import statements

If you are using an IDE such as Android Studio, you don't have to worry about this because your IDE is already obeying these rules. If not, have a look below.

The ordering of import statements is:

  1. Android imports
  2. Imports from third parties (com, junit, net, org)
  3. java and javax
  4. Same project imports

2.2.9 Logging guidelines

Use the logging methods provided by the Timber class to print out error messages or other information that may be useful for developers to identify issues:

  • Timber.tag(tag: String).v(message: String) (verbose)
  • Timber.tag(tag: String).d(message: String) (debug)
  • Timber.tag(tag: String).i(message: String) (information)
  • Timber.tag(tag: String).w(message: String) (warning)
  • Timber.tag(tag: String).e(message: String) (error)

As a general rule, we use the class name as tag and we define it as a static final field at the top of the file. For example:

class MyClass {
    private const val TAG = MyClass::class.simpleName

    fun myMethod() {
        Timber.tag(TAG).e("My error message")
    }
}

VERBOSE and DEBUG logs must be disabled on release builds. It is also recommended to disable INFORMATION, WARNING and ERROR logs but you may want to keep them enabled if you think they may be useful to identify issues on release builds. If you decide to leave them enabled, you have to make sure that they are not leaking private information such as email addresses, user ids, etc.

To only show logs on debug builds:

if (BuildConfig.DEBUG) Timber.tag(TAG).d("The value of x is " + x);

Logging errors:

    try {
        //do smth
    } catch(e: Throwable) {
        Timber.e(e, "Error doing smth")
    }

2.2.10 Class member ordering

There is no single correct solution for this but using a logical and consistent order will improve code learnability and readability. It is recommendable to use the following order:

  1. Constants
  2. Injected objects
  3. ViewBinding classes
  4. Fields
  5. Constructors
  6. Override methods and callbacks (public or private)
  7. Public methods
  8. Private methods
  9. Inner classes or interfaces

Lifecycle methods from Android SDK should be placed close to each other without private methods or fields in between. In other cases private methods should be ordered in such a way so that they read like a story that doesn't cause the reader to need to jump around the code too much. It's better to place private methods to the nearest place where they are used. This is a more sensible way to organize code rather than by access modifier.

private const val ARG_NAME = "ARG_NAME"

class MyFragment : Fragment() {
  companion object { 
      fun create(name: String): MyFragment = MyFragment().withArgs(ARG_NAME, name)
  }

  // public
  var somePublicVal: String = "example"
  // injected fields
  private val presenter: MyPresenter by inject()
  // view binding
  private val binding: FragmentBinding by viewBinding()
  // other fields
  private val adapter: MyListAdapter = MyListAdapter()

  // lifecycle methods
  override fun onCreate() { 
   // ...
  }

  override fun onPause() { 
   // ...
  }

  override fun onDestroy() { 
   // ...
  }

Example:

class MainActivity : Activity {

    companion object {
        private const val TAG = MainActivity::class.simpleName
    }

    private val manager: SomeManager by inject()
    
    private val binding: FragmentViewBinding by viewBinding()
    
    private val title: String

    override fun onCreate() {
        ...
    }
    
    override fun onStop() {
        ...
    }

    override fun showData(data: SomeData) {
    	...
    }

    private void setUpView() {
        ...
    }

    inner class AnInnerClass {

    }
}

If your class is extending an Android component such as an Activity or a Fragment, it is a good practice to order the override methods so that they match the component's lifecycle. For example, if you have an Activity that implements onCreate(), onDestroy(), onPause() and onResume(), then the correct order is:

class MainActivity : {

    //Order matches Activity lifecycle
    override fun onCreate() {}

    override fun onResume() {}

    override fun onPause() {}

    override fun onDestroy() {}

}

2.2.11 Parameter ordering in methods

When programming for Android, it is quite common to define methods that take a Context. If you are writing a method like this, then the Context must be the first parameter.

The opposite case are callback interfaces that should always be the last parameter.

Examples:

// Context always goes first
fun loadUser(context: Context, userId: Int) : User

// Callbacks always go last
fun loadUserAsync(context: Context, userId: Int, callback: () -> Unit)

2.2.13 String constants, naming, and values

Many elements of the Android SDK such as SharedPreferences, Bundle, or Intent use a key-value pair approach so it's very likely that even for a small app you end up having to write a lot of String constants.

When using one of these components, you must define the keys as a const val fields and they should be prefixed as indicated below.

Element Field Name Prefix
SharedPreferences KEY_
Bundle ARG_
Fragment Arguments ARG_
Intent Extra ARG_
Request Key KEY_REQUEST
Intent Action ACTION_

Note that the arguments of a Fragment - Fragment.arguments - are also a Bundle. However, because this is a quite common use of Bundles, we define a different prefix for them.

Example:

// Note the value of the field is the same as the name to avoid duplication issues
const val ARG_EMAIL = "ARG_EMAIL"
const val ARG_AGE = "ARG_AGE"
const val ARG_USER_ID = "ARG_USER_ID"
const val ARG_SURNAME = "ARG_SURNAME"

// Intent-related items use full package name as value
const val ACTION_OPEN_USER = "com.myapp.action.ACTION_OPEN_USER"

2.2.14 Arguments in Fragments and Activities

When data is passed into a Fragment via an Intent or a Bundle, the keys for the different values must follow the rules described in the section above.

When a Fragment expects arguments, it should provide a public static method that facilitates the creation of the relevant Intent or Fragment.

For Fragments it is named create() and handles the creation of the Fragment with the right arguments:

companion object {
    fun create(user: User): UserFragment =
       UserFragment().withArgs(ARG_USER to user)
}

Note 1: These methods should go at the top of the class before onCreate().

Note 2: If we provide the methods described above, the keys for extras and arguments should be private because there is not need for them to be exposed outside the class.

2.2.15 Line length limit

Code lines should not exceed 120 characters. If the line is longer than this limit there are usually two options to reduce its length:

  • Extract a local variable or method (preferable).
  • Apply line-wrapping to divide a single line into multiple ones.

There are two exceptions where it is possible to have lines longer than 120:

  • Lines that are not possible to split, e.g. long URLs in comments.
  • package and import statements.

2.2.15.1 Line-wrapping strategies

There isn't an exact formula that explains how to line-wrap and quite often different solutions are valid. However there are a few rules that can be applied to common cases.

Break at operators

When the line is broken at an operator, the break comes before the operator. For example:

val longName = anotherVeryLongVariable + anEvenLongerOne - thisRidiculousLongOne
        + theFinalOne;

Assignment Operator Exception

An exception to the break at operators rule is the assignment operator =, where the line break should happen after the operator.

val longName =
        anotherVeryLongVariable + anEvenLongerOne - thisRidiculousLongOne + theFinalOne;

Method chain case

When multiple methods are chained in the same line - for example when using Builders - every call to a method should go in its own line, breaking the line before the .

Picasso.with(context).load("http://ribot.co.uk/images/sexyjoe.jpg").into(imageView);
Picasso.with(context)
        .load("http://ribot.co.uk/images/sexyjoe.jpg")
        .into(imageView);

Long parameters case

When a method has many parameters or its parameters are very long, we should break the line after every comma , and use named parameters

loadPicture(context, "http://ribot.co.uk/images/sexyjoe.jpg", profileImageView, listener, "Title of the picture");
loadPicture(
    context = context,
    url = "http://ribot.co.uk/images/sexyjoe.jpg",
    imageView = profileImageView,
    listener = listener,
    title = "Title of the picture"
 )

2.2.16 When and If usages

The general rule of the thumb here is to understand what how many expressions you need to check and of what type are they. If you have <= 2 boolean checks, then you can use if. If you have > 2 boolean checks, please use when for better readablity.

// good
if (value.isNotEmpty()) {
    doStuff()
} else {
    doElseStuff()
}
// good
if (value.isEmpty()) {
    doStuff()
} else if (value.size == 5) {
    doElseStuff()
}

// bad
if (value.isEmpty()) {
    doStuff()
} else if (value.size == 5) {
    doElseStuff()
} else {
    doElseElseStuff()
}

// good
when {
    value.isEmpty() -> //...
    value.size == 5 -> //...
    else -> // ...
}

When working with Enums or sealed instances, when is always preferred with >= 2 checks

// good
if (token == Token.Active) {
    doStuff()
}

// bad
if (token == Token.Active) {
    doStuff()
} else if (token == Token.Disabled) {
    doElseStuff()
}

// good
when (token) {
    is Token.Active -> //...
    is Token.Disabled -> //...
}

2.2.17 Expression body usage

  1. If function or method return type is Unit - expression body is not allowed and block body must be used.
// not allowed
fun methodExample(params: List<Any>) = require(params.isNotEmpty()) { "Params should not be empty" }

// good
fun methodExample(params: List<Any>) { 
    require(params.isNotEmpty()) { "Params should not be empty" }
}
  1. When using expression body, adding explicit return type to function or method signature is mandatory.
// not allowed
fun check(params: List<Any>) = 
    params.filterInstance<String>
        .map { it.toUpperCase }

// good
fun castToUpperCase(args: List<Any>): List<String> = 
    params.filterInstance<String>
        .map { it.toUpperCase }
  1. Function or method expression body formatting choice is up to a developer.

2.2.18 Scope functions usage

with - is allowed to use when there are >= 2 calls on a passed instance. Used for grouping function calls on an object.

with (binding) {
    textView.setText(text)
    imageView.isVisible = false
}

apply - used for object configuration.

val intent = Intent().apply {
    action = intentAction
    data = Uri.parse(intentData)
}

let - used for executing a lambda on non-null objects

val greeting = Person().name?.let {
    "Hi, $it!"
}

2.2.19 Lambda expressions

If lambda with one parameter doesn't have lambdas inside, it's allowed to use implicit it. If lambda with one parameter does have lambdas inside with their body, naming each it is mandatory.

// good
tokenValue?.let {
    activate(it)
}

// bad
tokenValue?.let {
    activate(it, onResult = { 
        view?.showResult(it)
    })
}
// good
tokenValue?.let { token ->
    activate(token, onResult = { tokenActivationResult ->
        view?.showResult(tokenActivationResult)
    })
}

2.3 XML style rules

2.3.1 Use self closing tags

When an XML element doesn't have any contents, you must use self closing tags.

This is good:

<TextView
	android:id="@+id/textViewProfile"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content" />

This is bad :

<!-- Don\'t do this! -->
<TextView
    android:id="@+id/textViewProfile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</TextView>

2.3.2 Resources naming

Resource IDs and names are written in lowercase_underscore.

2.3.2.1 ID naming

IDs should be prefixed with the name of the element in camel case. In general it should be cameCase. For example:

Element Prefix
TextView textView
ImageView imageView
Button button
include layout
Menu menu

Image view example:

<ImageView
    android:id="@+id/imageViewProfile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/textViewProfile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<include
    android:id="@+id/includeToolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/layout_toolbar" />
<ConstraintLayout
    android:id="@+id/containerRoot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Menu example:

<menu>
	<item
        android:id="@+id/menuDone"
        android:title="Done" />
</menu>

2.3.2.2 Strings

String names start with a prefix that identifies the section they belong to. For example auth_email_hint or auth_name_hint. If a string doesn't belong to any section, then you should follow the rules below:

Prefix Description
error_ An error message
common_msg_ A regular information message
common_title_ A title, i.e. a dialog title
common_action_ An action such as "Save" or "Create"

2.3.2.3 Styles and Themes

Unlike the rest of resources, style names are written in UpperCamelCase.

2.3.3 Attributes ordering

As a general rule you should try to group similar attributes together. A good way of ordering the most common attributes is:

  1. View Id
  2. Style
  3. Layout width and layout height
  4. Other layout attributes, sorted alphabetically
  5. Remaining attributes, sorted alphabetically

2.4 Tests style rules

2.4.1 Unit tests

Test classes should match the name of the class the tests are targeting, followed by Test. For example, if we create a test class that contains tests for the DatabaseHelper, we should name it DatabaseHelperTest.

Test methods are annotated with @Test and should generally start with the name of the method that is being tested, followed by a precondition and/or expected behaviour.

  • Template:
  @Test 
  fun methodNamePreconditionExpectedBehaviour()
  • Example:
  @Test 
  fun signInWithEmptyEmailFails()

Precondition and/or expected behaviour may not always be required if the test is clear enough without them.

Sometimes a class may contain a large amount of methods, that at the same time require several tests for each method. In this case, it's recommendable to split up the test class into multiple ones. For example, if the DataManager contains a lot of methods we may want to divide it into DataManagerSignInTest, DataManagerLoadUsersTest, etc. Generally you will be able to see what tests belong together because they have common test fixtures.

Clone this wiki locally