affaan-m/ECC · android-clean-architecture · SKILL.md

安卓清洁架构

适用于Android和Kotlin多平台项目的Clean Architecture模式——模块结构、依赖规则、用例、仓库以及数据层模式。

★ 253991 质量 94.0 许可证 MIT 数据 generic

这是公开 Agent Skill 的中文说明,由采集翻译或编辑润色,完整内容以上游 SKILL.md 为准。适用于 Cursor / Claude Code / Codex / Gemini。 打开上游 SKILL.md

安装

通用(npx skills)

npx skills add affaan-m/ECC -s android-clean-architecture

Cursor

把该 Skill 复制到项目的 .cursor/skills/android-clean-architecture/ 目录

Claude Code

/plugin marketplace add affaan-m/ECC

中文说明

# Android 整洁架构

适用于 Android 和 KMP 项目的整洁架构模式。涵盖模块边界、依赖反转、UseCase/Repository 模式,以及使用 Room、SQLDelight 和 Ktor 的数据层设计。

## 何时启用

* 构建 Android 或 KMP 项目模块结构
* 实现 UseCases、Repositories 或 DataSources
* 设计各层(领域层、数据层、表示层)之间的数据流
* 使用 Koin 或 Hilt 设置依赖注入
* 在分层架构中使用 Room、SQLDelight 或 Ktor

## 模块结构

### 推荐布局

```
project/
├── app/                  # Android 入口点,DI 装配,Application 类
├── core/                 # 共享工具类,基类,错误类型
├── domain/               # 用例,领域模型,仓库接口(纯 Kotlin)
├── data/                 # 仓库实现,数据源,数据库,网络
├── presentation/         # 界面,ViewModel,UI 模型,导航
├── design-system/        # 可复用的 Compose 组件,主题,排版
└── feature/              # 功能模块(可选,用于大型项目)
    ├── auth/
    ├── settings/
    └── profile/
```

### 依赖规则

```
app → presentation, domain, data, core
presentation → domain, design-system, core
data → domain, core
domain → core (或无依赖)
core → (无依赖)
```

**关键**:`domain` 绝不能依赖 `data`、`presentation` 或任何框架。它仅包含纯 Kotlin 代码。

## 领域层

### UseCase 模式

每个 UseCase 代表一个业务操作。使用 `operator fun invoke` 以获得简洁的调用点:

```kotlin
class GetItemsByCategoryUseCase(
    private val repository: ItemRepository
) {
    suspend operator fun invoke(category: String): Result<List<Item>> {
        return repository.getItemsByCategory(category)
    }
}

// Flow-based UseCase for reactive streams
class ObserveUserProgressUseCase(
    private val repository: UserRepository
) {
    operator fun invoke(userId: String): Flow<UserProgress> {
        return repository.observeProgress(userId)
    }
}
```

### 领域模型

领域模型是普通的 Kotlin 数据类——没有框架注解:

```kotlin
data class Item(
    val id: String,
    val title: String,
    val description: String,
    val tags: List<String>,
    val status: Status,
    val category: String
)

enum class Status { DRAFT, ACTIVE, ARCHIVED }
```

### 仓库接口

在领域层定义,在数据层实现:

```kotlin
interface ItemRepository {
    suspend fun getItemsByCategory(category: String): Result<List<Item>>
    suspend fun saveItem(item: Item): Result<Unit>
    fun observeItems(): Flow<List<Item>>
}
```

## 数据层

### 仓

返回目录