Android Development

作者 mindrally97184105b5da無授權條款269 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 週前更新

Android development guidelines for Kotlin with clean architecture, MVI pattern, Material Design, and best practices for building robust mobile applications

AI 產生的概覽

針對 Kotlin 的 Android 開發規範,涵蓋整潔架構、MVI、Material 3 介面、測試與效能實務。

功能
提供一套成文的 Android 與 Kotlin 程式碼規範,包含命名慣例、函式與類別的設計規則,以及例外處理方式。它描述分層整潔架構,劃分為 data、domain、presentation 與 di 模組,並說明用於狀態與事件管理的 MVI 模式。內容也涵蓋 Navigation Component、ViewBinding、XML 版面與 Compose 的取捨、驗證畫面流程、測試方法,以及生命週期、背景工作、相依性注入與效能方面的實務。
適用情境
在規劃、撰寫或審查 Kotlin Android 應用程式碼,並希望統一架構、介面結構與測試慣例時使用。適合需要參考整潔架構分層、MVI 狀態管理或 Material 3 介面決策的團隊或代理。
執行需求
不包含指令碼或工具,僅為說明性文件。套用其中建議需要具備 Kotlin Android 專案及所引用的函式庫與框架,例如 Navigation Component、ViewBinding、Flow 或 LiveData、Coroutines、WorkManager、Hilt 或 Dagger,以及用於儀器化測試的 Espresso。

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:

  1. Splash Screen - Initial app launch
  2. Login Screen - User authentication
  3. Register Screen - New user registration
  4. Forgot Password Screen - Password recovery
  5. 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

來源與署名

來源:mindrally/skills位於android-development提交9718410

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架