Skip to content

This repository contains an opinionated set of Detekt Rules for Android projects

Notifications You must be signed in to change notification settings

wesjon/detekt-rules-android

Repository files navigation

Pre Merge Checks

Detekt rules for Android

This repository contains an opinionated set of Detekt Rules for Android projects

Getting started

1. Add this library declaration into your detekt dependencies

detekt {
    // ...
}

dependencies {
    detektPlugins "br.com.wesjon:detekt-android-rules:0.1" // Add it here
    detektPlugins "io.gitlab.arturbosch.detekt:detekt-formatting:$detekt_version"
}

2. Include a new block in your detekt.yml file

You may want to activate or deactivate rules according to your project needs.

android-rules:
  TestNameShouldFollowNamingConvention:
    active: true
    namingConvention: 'backtick' // REQUIRED: backtick, snake_case or camelCase
  ViewModelExposesState:
    active: true
    customViewModel: 'BaseViewModel,CustomViewModel' // OPTIONAL: If your application uses base/custom ViewModels

Rules

TestNameShouldFollowNamingConvention

Checks whether test names follow the name convention. This rule only applies if method is annotated with @Test

NON-compliant code

CONFIG: backtick

@Test
fun additionIsCorrect(){
    // ...
}

Compliant code

// config: backtick
@Test
fun `addition is correct`(){
    // ...
}

// config: snake_case
@Test
fun addition_is_correct(){
    // ...
}

// config: camelCase
@Test
fun additionIsCorrect(){
    // ...
}

ViewModelExposesState

MVVM has popularized the use of ViewModels in Android apps. Exposing a MutableLiveData to the View layer is an antipattern and should be avoided when using ViewModels.

Non-compliant code

class LoginViewModel: ViewModel() {
     val loginViewState = MutableLiveData<LoginViewState>()
}

Compliant code

class LoginViewModel: ViewModel() {
     private val loginViewState = MutableLiveData<LoginViewState>()
     val loginViewState: LiveData<LoginViewState> = _loginViewState
}

About

This repository contains an opinionated set of Detekt Rules for Android projects

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages