Search code examples
androidandroid-studiokotlinpluginscode-duplication

Finding duplicated code (Kotlin) in Android Studio


Is there a way to find duplicated (Copy/pasted code, repeated code) Kotlin code in Android Studio? I have Android Studio 4.0 but can't find this "Duplicated code fragment" option.

Are there any Android Studio plugins that do this job for Kotlin?


Solution

  • You can install the sonarLint plugin.

    It has many rules which are executed on source code to find possible issues like bugs or code smells.

    example 1

    This is done with the rules provided by default.

    Other example of repetition:

    example 2

    Just need to install the sonarLint plugin in Preferences/Plugins:

    sonarLint

    A more advanced tool you could check is Gradle CPD plugin. This is a Gradle plugin to find duplicate code using PMDs copy/paste detection (= CPD). You can find information about how to install it and how to use it in the same readme of the repository. CPD supports Kotlin since v6.10.0.