android-clean-architecture
Skill này làm gì
Skill này giúp tổ chức code cho app Android và các nền tảng khác. Nó định nghĩa cách các phần của chương trình giao tiếp với nhau, giúp code dễ hiểu và dễ bảo trì hơn.
Khi nào nên dùng
- ✓Khi bắt đầu một dự án Android mới.
- ✓Khi tổ chức các phần khác nhau của một app đang có.
- ✓Khi code của app bắt đầu trở nên rối rắm.
Liên kết skill
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/android-clean-architecture/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Làm sao để tôi cấu trúc code Android theo Clean Architecture?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Nó chia code thành các tầng logic, như “domain” (quy tắc nghiệp vụ) và “data” (nơi lưu thông tin). Các quy tắc đảm bảo tầng domain không phụ thuộc vào tầng khác, giữ cho nó thuần khiết.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Android Clean Architecture
Clean Architecture patterns for Android and KMP projects. Covers module boundaries, dependency inversion, UseCase/Repository patterns, and data layer design with Room, SQLDelight, and Ktor.
When to Activate
- Structuring Android or KMP project modules
- Implementing UseCases, Repositories, or DataSources
- Designing data flow between layers (domain, data, presentation)
- Setting up dependency injection with Koin or Hilt
- Working with Room, SQLDelight, or Ktor in a layered architecture
Module Structure
Recommended Layout
project/
├── app/ # Android entry point, DI wiring, Application class
├── core/ # Shared utilities, base classes, error types
├── domain/ # UseCases, domain models, repository interfaces (pure Kotlin)
├── data/ # Repository implementations, DataSources, DB, network
├── presentation/ # Screens, ViewModels, UI models, navigation
├── design-system/ # Reusable Compose components, theme, typography
└── feature/ # Feature modules (optional, for larger projects)
├── auth/
├── settings/
└── profile/
Dependency Rules
app → presentation, domain, data, core
presentation → domain, design-system, core
data → domain, core
domain → core (or no dependencies)
core → (nothing)
Critical: domain must NEVER depend on data, presentation, or any framework. It contains pure Kotlin only.
Domain Layer
UseCase Pattern
Each UseCase represents one business operation. Use operator fun invoke for clean call sites:
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)
}
}
Domain Models
Domain models are plain Kotlin data classes — no framework annotations:
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 }
Repository Interfaces
Defined in domain, implemented in data:
interface ItemRepository {
suspend fun getItemsByCategory(category: String): Result<List<Item>>
suspend fun saveItem(item: Item): Result<Unit>
fun observeItems(): Flow<List<Item>>
}
Data Layer
Repository Implementation
Coordinates between local and remote data sources:
class ItemRepositoryImpl(
private val localDataSource: ItemLocalDataSource,
private val remoteDataSource: ItemRemoteDataSource
) : ItemRepository {
override suspend fun getItemsByCategory(category: String): Result<List<Item>> {
return runCatching {
val remote = remoteDataSource.fetchItems(category)
localDataSource.insertItems(remote.map { it.toEntity() })
localDataSource.getItemsByCategory(category).map { it.toDomain() }
}
}
override suspend fun saveItem(item: Item): Result<Unit> {
return runCatching {
localDataSource.insertItems(listOf(item.toEntity()))
}
}
override fun observeItems(): Flow<List<Item>> {
return localDataSource.observeAll().map { entities ->
entities.map { it.toDomain() }
}
}
}
Mapper Pattern
Keep mappers as extension functions near the data models:
// In data layer
fun ItemEntity.toDomain() = Item(
id = id,
title = title,
description = description,
tags = tags.split("|"),
status = Status.valueOf(status),
category = category
)
fun ItemDto.toEntity() = ItemEntity(
id = id,
title = title,
description = description,
tags = tags.joinToString("|"),
status = status,
category = category
)
Room Database (Android)
@Entity(tableName = "items")
data class ItemEntity(
@PrimaryKey val id: String,
val title: String,
val description: String,
val tags: String,
val status: String,
val category: String
)
@Dao
interface ItemDao {
@Query("SELECT * FROM items WHERE category = :category")
…
Câu hỏi thường gặp
“Domain” là gì?
Đó là phần code chứa quy tắc cốt lõi của app, không cần lo lắng về chi tiết kỹ thuật như dữ liệu được lưu ở đâu.
“Use Case” là gì?
Đó là một hành động cụ thể mà app có thể thực hiện, như “lấy danh sách” hoặc “lưu người dùng”.
Tại sao “domain” không được phụ thuộc vào “data”?
Để quy tắc nghiệp vụ có thể dùng trong các bối cảnh khác nhau (như Android, web) mà không cần thay đổi, và để code gọn gàng và dễ kiểm thử hơn.
Chi tiết
Mô tả gốc của tác giả
Clean Architecture patterns for Android and Kotlin Multiplatform projects — module structure, dependency rules, UseCases, Repositories, and data layer patterns. Use when structuring modules, layers, or data flow in an Android or KMP project.
repo: affaan-m/ECC
Độ phổ biến
- Sao GitHub
- 280k
- Vị trí xếp hạng
- #135
- Giấy phép
- MIT
- Cập nhật
- 15/10/2026
Bình luận
- …
Thêm skill từ kho mã này
accessibility
280kGiúp agent của bạn tạo giao diện mà ai cũng dùng được, đảm bảo website và app hoạt động tốt với người khuyết tật như người mù hoặc khó vận động.
agent-architecture-audit
280kGiúp agent của bạn kiểm tra xem mọi thứ có hoạt động đúng không, đồng thời tìm và sửa các lỗi trong hệ thống AI để chúng chạy như mong đợi.
agent-eval
280kGiúp agent của bạn so sánh các công cụ lập trình AI khác nhau, đo xem công cụ nào nhanh hơn, rẻ hơn và đáng tin cậy hơn cho từng tác vụ.
agent-harness-construction
280kGiúp agent của bạn hiểu rõ hơn cách thực hiện tác vụ, tối ưu các công cụ và chỉ dẫn mà agent dùng để tăng khả năng thành công.
Skill tương tự
flutter-animating-apps
101kGiúp agent của bạn thêm hiệu ứng hoạt ảnh và chuyển cảnh trong ứng dụng Flutter. Hữu ích để tạo chuyển động trên ứng dụng iOS và Android.
agent-spec-mobile-react-native
81kGiúp agent xây dựng ứng dụng di động cho iPhone và Android bằng React Native, gồm màn hình, điều hướng và module gốc.
orientation
75kGiúp agent của bạn đảm bảo trang hoạt động cả ở chế độ dọc và ngang, gỡ khóa xoay màn hình và thông báo xoay thiết bị đang chặn người dùng xem nội dung.