Android Development Best Practices
General Kotlin Guidelines
Basic Principles
- Use English for all code and documentation
- Always declare variable and function types explicitly
- Avoid the
any type; create necessary custom types
- Eliminate blank lines within function bodies
Naming Standards
- PascalCase: Classes, interfaces, enums
- camelCase: Variables, functions, methods
- underscores_case: Files and directories
- UPPERCASE: Environment variables, constants
- Avoid magic numbers; define constants instead
- Functions should start with action verbs
- Boolean variables use prefixes:
isLoading, hasError, canDelete
Function Design
- Keep functions under 20 instructions with single responsibility
- Use verb-based naming
- Prefix boolean returns with
is, has, or can
- Prefix void returns with
execute, save, send
- Combat nesting through early returns, utility extraction, and higher-order functions
- Use default parameters instead of null checks
- Consolidate parameters into objects (RO-RO pattern)
Data and Classes
- Employ data classes for data structures
- Encapsulate primitives in composite types; validate internally
- Favor immutability; use
val for unchanging values
- Follow SOLID principles; prefer composition over inheritance
- Keep classes under 200 instructions, 10 public methods, 10 properties
Exception Handling
- Reserve exceptions for unexpected errors
- Catch exceptions only to fix anticipated issues or add context
- Create custom exception types for domain errors
Android Architecture
Clean Architecture
- Implement clean architecture with clear layer separation
- Use repository pattern for data persistence and caching
- Keep business logic in Use Cases or Interactors
- Separate concerns between UI, domain, and data layers
Project Structure
app/
├── data/ # Data sources, repositories, models
├── domain/ # Use cases, domain models, interfaces
├── presentation/ # UI components, ViewModels, state
└── di/ # Dependency injection modules
MVI Pattern
- Deploy MVI pattern for state and event management
- ViewModels manage UI state as a single immutable state object
- UI components observe state and render accordingly
- Handle user intents/events through a single entry point
- Keep side effects predictable and traceable
UI Development
Navigation
- Use Navigation Component for fragment and activity routing
- Define navigation graph for app flow
- Handle deep links through Navigation Component
- Implement safe args for type-safe navigation arguments
Main Activity Structure
- MainActivity manages primary navigation
- Use BottomNavigationView for main destinations (Home, Profile, Settings, etc.)
- Handle navigation state properly across configuration changes
View Binding and State
- Use ViewBinding for type-safe view access
- Use Flow or LiveData for UI state management
- Observe state changes in lifecycle-aware manner
- Handle loading, error, and success states consistently
UI Framework Preferences
- Prefer XML layouts and Fragments over Jetpack Compose (unless Compose is specifically required)
- Use ConstraintLayout for complex layouts
- Apply Material 3 design guidelines
- Follow responsive design practices for different screen sizes
Authentication Flow
Structure authentication screens properly:
- Splash Screen - Initial app launch
- Login Screen - User authentication
- Register Screen - New user registration
- Forgot Password Screen - Password recovery
- Verify Email Screen - Email verification
Testing
Unit Testing
- Follow Arrange-Act-Assert conventions
- Test ViewModels, Use Cases, and Repositories
- Use test doubles for dependencies
- Achieve good coverage of business logic
UI Testing
- Implement widget testing for UI components
- Write integration tests for API modules
- Test navigation flows
- Use Espresso for instrumented tests
Best Practices
Lifecycle Management
- Handle lifecycle events properly
- Avoid memory leaks from improper lifecycle handling
- Use lifecycle-aware components
Background Processing
- Use Coroutines for async operations
- Handle cancellation properly
- Use WorkManager for deferrable background work
Dependency Injection
- Use Hilt or Dagger for dependency injection
- Scope dependencies appropriately
- Keep DI configuration organized
Performance
- Avoid work on the main thread
- Optimize RecyclerView with DiffUtil
- Use lazy loading for heavy resources
- Profile and optimize memory usage
1---2name: android-development3description: Android development guidelines for Kotlin with clean architecture, MVI pattern, Material Design, and best practices for building robust mobile applications4metadata:5 maintainer: Mindrally6 source: https://skillmd.com/skills/mindrally/android-development7 upstream: https://github.com/Mindrally/skills8---910# Android Development Best Practices1112## General Kotlin Guidelines1314### Basic Principles15- Use English for all code and documentation16- Always declare variable and function types explicitly17- Avoid the `any` type; create necessary custom types18- Eliminate blank lines within function bodies1920### Naming Standards21- **PascalCase**: Classes, interfaces, enums22- **camelCase**: Variables, functions, methods23- **underscores_case**: Files and directories24- **UPPERCASE**: Environment variables, constants25- Avoid magic numbers; define constants instead26- Functions should start with action verbs27- Boolean variables use prefixes: `isLoading`, `hasError`, `canDelete`2829### Function Design30- Keep functions under 20 instructions with single responsibility31- Use verb-based naming32- Prefix boolean returns with `is`, `has`, or `can`33- Prefix void returns with `execute`, `save`, `send`34- Combat nesting through early returns, utility extraction, and higher-order functions35- Use default parameters instead of null checks36- Consolidate parameters into objects (RO-RO pattern)3738### Data and Classes39- Employ data classes for data structures40- Encapsulate primitives in composite types; validate internally41- Favor immutability; use `val` for unchanging values42- Follow SOLID principles; prefer composition over inheritance43- Keep classes under 200 instructions, 10 public methods, 10 properties4445### Exception Handling46- Reserve exceptions for unexpected errors47- Catch exceptions only to fix anticipated issues or add context48- Create custom exception types for domain errors4950## Android Architecture5152### Clean Architecture53- Implement clean architecture with clear layer separation54- Use repository pattern for data persistence and caching55- Keep business logic in Use Cases or Interactors56- Separate concerns between UI, domain, and data layers5758### Project Structure59```60app/61├── data/ # Data sources, repositories, models62├── domain/ # Use cases, domain models, interfaces63├── presentation/ # UI components, ViewModels, state64└── di/ # Dependency injection modules65```6667### MVI Pattern68- Deploy MVI pattern for state and event management69- ViewModels manage UI state as a single immutable state object70- UI components observe state and render accordingly71- Handle user intents/events through a single entry point72- Keep side effects predictable and traceable7374## UI Development7576### Navigation77- Use Navigation Component for fragment and activity routing78- Define navigation graph for app flow79- Handle deep links through Navigation Component80- Implement safe args for type-safe navigation arguments8182### Main Activity Structure83- MainActivity manages primary navigation84- Use BottomNavigationView for main destinations (Home, Profile, Settings, etc.)85- Handle navigation state properly across configuration changes8687### View Binding and State88- Use ViewBinding for type-safe view access89- Use Flow or LiveData for UI state management90- Observe state changes in lifecycle-aware manner91- Handle loading, error, and success states consistently9293### UI Framework Preferences94- Prefer XML layouts and Fragments over Jetpack Compose (unless Compose is specifically required)95- Use ConstraintLayout for complex layouts96- Apply Material 3 design guidelines97- Follow responsive design practices for different screen sizes9899## Authentication Flow100101Structure authentication screens properly:1021. Splash Screen - Initial app launch1032. Login Screen - User authentication1043. Register Screen - New user registration1054. Forgot Password Screen - Password recovery1065. Verify Email Screen - Email verification107108## Testing109110### Unit Testing111- Follow Arrange-Act-Assert conventions112- Test ViewModels, Use Cases, and Repositories113- Use test doubles for dependencies114- Achieve good coverage of business logic115116### UI Testing117- Implement widget testing for UI components118- Write integration tests for API modules119- Test navigation flows120- Use Espresso for instrumented tests121122## Best Practices123124### Lifecycle Management125- Handle lifecycle events properly126- Avoid memory leaks from improper lifecycle handling127- Use lifecycle-aware components128129### Background Processing130- Use Coroutines for async operations131- Handle cancellation properly132- Use WorkManager for deferrable background work133134### Dependency Injection135- Use Hilt or Dagger for dependency injection136- Scope dependencies appropriately137- Keep DI configuration organized138139### Performance140- Avoid work on the main thread141- Optimize RecyclerView with DiffUtil142- Use lazy loading for heavy resources143- Profile and optimize memory usage