Skip to main content

Android - Linting and Best Practices

This page outlines the linting process and best practices for Android development. Following these guidelines will help ensure clean, maintainable, and high-quality code.

Linting with Ktlint

We use Ktlint as our linter for Kotlin code. Ktlint is a static code analysis tool that enforces the Kotlin coding style. It helps maintain consistency in your codebase and ensures that your code adheres to the standard coding conventions for Kotlin.

Why Use Ktlint?

  • Code Consistency – Ensures all developers follow the same style, making the code easier to read and maintain.
  • Error Prevention – Highlights potential issues and enforces best practices early in the development process.
  • Automation Support – Is integrated into the development workflow for automated lint checks.

Running Ktlint

We provide the following commands to check and fix lint issues:

Check Lint Issues

Run the following command to identify any linting problems in your code:

./gradlew ktlint

Auto-Fix Lint Issues

Use this command to automatically correct some linting issues:

./gradlew ktlintFormat
Important

The ktlintFormat tool can fix many common issues but cannot resolve all problems. Manual intervention may still be required.

Lint Checks During Merge Requests

When submitting a merge request to the master branch, lint checks will automatically run as part of the process as the first step. If any lint issues are detected, you will need to fix them before the code review begins.

To Avoid Delays

  • Run lint checks frequently during development using ./gradlew ktlint.
  • Fix issues early to ensure a smoother merge process.

Auto-Formatting in Android Studio

For quick fixes, you can auto-format your code directly in Android Studio:

  • macOS: Press Command + Option + L.
  • Windows/Linux: Press Ctrl + Alt + L.

This auto-format feature aligns your code with the project's style guidelines and can help resolve many minor formatting issues.

Best Practices for Clean Android Code

Following best practices ensures your codebase remains clean, maintainable, and easy to understand for all team members.

Avoid Debug Logic in Production

  • Remove Debug Statements – Ensure all debug code, such as Log.d, Log.v, or println, is removed before committing.
  • Minimize Noise – Debug statements left in production can clutter logs and expose sensitive data.

Eliminate Unused or Commented-Out Code

  • Clean Up Code – Avoid leaving large blocks of commented-out code in your files. If the code isn’t needed, delete it.
  • Reduce Clutter – Unused code makes the codebase harder to read and navigate.

Use Meaningful Names

  • Descriptive Names – Name files, variables, and functions clearly and with purpose.

    • ✅ Use UserDetailsActivity.kt instead of MainActivity2.kt.
    • ❌ Avoid generic names like Helper.kt unless its purpose is truly broad and utility-based.

Follow the Single Responsibility Principle

  • Keep it Focused – Ensure each class and method has a single, clear responsibility.
  • Improve Reusability – Breaking functionality into smaller, focused components makes your code easier to test and reuse.

Write Small and Modular Methods

  • Avoid Monolithic Code – Break down large methods or classes into smaller, manageable pieces.
  • Enhance Readability – Smaller methods are easier to understand and debug.

Comment Judiciously

  • Explain the "Why" – Use comments to describe why certain decisions were made, rather than what the code is doing.
  • Avoid Redundancy – Good naming conventions should make the purpose of code clear without extensive comments.

Update Dependencies Regularly

  • Stay Current – Keep your libraries and SDK versions up to date. A new version of version-catalog is released with each Q2 Mobile App release.
  • Ensure Security and Compatibility – Regular updates reduce the risk of security vulnerabilities and compatibility issues.

By incorporating these best practices into your development process, you contribute to a more robust and maintainable codebase. Clean code is easier to review, debug, and extend, benefiting the entire team.