Android Architecture

new-silvermoon/awesome-android-agent-skills/.github/skills/architecture/android-architecture

作者 new-silvermoon82900eacc8dbe13de93c6310af27b9df4b2bd2f6無授權條款收錄於 2026年10月9日更新於 2026年10月9日

Expert guidance on setting up and maintaining a modern Android application architecture using Clean Architecture and Hilt. Use this when asked about project structure, module setup, or dependency injection.

AI 產生的概覽

指導 Android 應用程式架構,涵蓋 Clean Architecture 分層、Hilt 依賴注入與多模組專案結構。

功能
提供設定與維護現代 Android 應用程式架構的專業指引。它定義 UI、Domain 與 Data 三層及嚴格的內向依賴流向,規定 Hilt 註解與模組模式用於依賴注入,並概述包含核心模組與功能模組的多模組策略。也包含實作檢查清單,涵蓋分層純淨性、主執行緒安全的 suspend 函式與 StateFlow 使用。
適用情境
適用於設計或重構 Android 應用程式的專案結構、模組設定或依賴注入時。針對關於 Clean Architecture 分層、Hilt 設定或模組化策略的問題。
執行需求
無需指令碼或工具,僅為指引說明。假定使用 Kotlin、Hilt 的 Android 專案,可選用 Retrofit、Room 與 Jetpack Compose。

Android Modern Architecture & Modularization

Instructions

When designing or refactoring an Android application, adhere to the Guide to App Architecture and Clean Architecture principles.

1. High-Level Layers

Structure the application into three primary layers. Dependencies must strictly flow inwards (or downwards) to the core logic.

  • UI Layer (Presentation):
    • Responsibility: Displaying data and handling user interactions.
    • Components: Activities, Fragments, Composables, ViewModels.
    • Dependencies: Depends on the Domain Layer (or Data Layer if simple). Never depends on the Data Layer implementation details directly.
  • Domain Layer (Business Logic) [Optional but Recommended]:
    • Responsibility: Encapsulating complex business rules and reuse.
    • Components: Use Cases (e.g., GetLatestNewsUseCase), Domain Models (pure Kotlin data classes).
    • Pure Kotlin: Must NOT contain any Android framework dependencies (no android.* imports).
    • Dependencies: Depends on Repository Interfaces.
  • Data Layer:
    • Responsibility: Managing application data (fetching, caching, saving).
    • Components: Repositories (implementations), Data Sources (Retrofit APIs, Room DAOs).
    • Dependencies: Depends only on external sources and libraries.

2. Dependency Injection with Hilt

Use Hilt for all dependency injection.

  • @HiltAndroidApp: Annotate the Application class.
  • @AndroidEntryPoint: Annotate Activities and Fragments.
  • @HiltViewModel: Annotate ViewModels; use standard constructor injection.
  • Modules:
    • Use @Module and @InstallIn(SingletonComponent::class) for app-wide singletons (e.g., Network, Database).
    • Use @Binds in an abstract class to bind interface implementations (cleaner than @Provides).

3. Modularization Strategy

For production apps, use a multi-module strategy to improve build times and separation of concerns.

  • :app: The main entry point, connects features.
  • :core:model: Shared domain models (Pure Kotlin).
  • :core:data: Repositories, Data Sources, Database, Network.
  • :core:domain: Use Cases and Repository Interfaces.
  • :core:ui: Shared Composables, Theme, Resources.
  • :feature:[name]: Standalone feature modules containing their own UI and ViewModels. Depends on :core:domain and :core:ui.

4. Checklist for implementation

  • Ensure Domain layer has no Android dependencies.
  • Repositories should default to main-safe suspend functions (use Dispatchers.IO internally if needed).
  • ViewModels should interact with the UI layer via StateFlow (see android-viewmodel skill).

來源與署名

來源:new-silvermoon/awesome-android-agent-skills位於.github/skills/architecture/android-architecture提交82900ea

授權條款: 無授權條款

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

檢舉或申請下架