Android Data Layer

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

by new-silvermoon82900eacc8dbe13de93c6310af27b9df4b2bd2f6No licenseListed Oct 9, 2026Updated Oct 9, 2026

Guidance on implementing the Data Layer using Repository pattern, Room (Local), and Retrofit (Remote) with offline-first synchronization.

Instructions onlySoftware Development
AI-generated overview

Guides Android developers in building an offline-first data layer with Repository, Room, Retrofit, and Hilt.

What it does
This skill provides instructional guidance for structuring an Android app's data layer around the Repository pattern as a single source of truth. It covers local persistence with Room entities and DAOs returning Flow, remote fetching with Retrofit suspend functions, and error handling for network failures. It also describes synchronization strategies such as stale-while-revalidate reads and the outbox pattern for writes, plus Hilt module binding of repository interfaces to implementations.
When to use it
Use it when planning or implementing the data layer of an Android application that needs caching, offline support, or coordination between a local database and a remote API. It is also useful when deciding how to structure repositories, Room DAOs, Retrofit services, and dependency injection bindings.
Requirements
No scripts or assets are included; it is instructions only. The guidance assumes an Android project using Kotlin, Room, Retrofit, Hilt, and WorkManager, but no packages, credentials, or network access are required to read it.

Android Data Layer & Offline-First

Instructions

The Data Layer coordinates data from multiple sources.

1. Repository Pattern

  • Role: Single Source of Truth (SSOT).
  • Logic: The repository decides whether to return cached data or fetch fresh data.
  • Implementation:
    kotlin
    class NewsRepository @Inject constructor(    private val newsDao: NewsDao,    private val newsApi: NewsApi) {    // Expose data from Local DB as the source of truth    val newsStream: Flow<List<News>> = newsDao.getAllNews()
        // Sync operation    suspend fun refreshNews() {        val remoteNews = newsApi.fetchLatest()        newsDao.insertAll(remoteNews)    }}

2. Local Persistence (Room)

  • Usage: Primary cache and offline storage.
  • Entities: Define @Entity data classes.
  • DAOs: Return Flow<T> for observable data.

3. Remote Data (Retrofit)

  • Usage: Fetching data from backend.
  • Response: Use suspend functions in interfaces.
  • Error Handling: Wrap network calls in try-catch blocks or a Result wrapper to handle exceptions (NoInternet, 404, etc.) gracefully.

4. Synchronization

  • Read: "Stale-While-Revalidate". Show local data immediately, trigger a background refresh.
  • Write: "Outbox Pattern" (Advanced). Save local change immediately, mark as "unsynced", use WorkManager to push changes to server.

5. Dependency Injection

  • Bind Repository interfaces to implementations in a Hilt Module.
    kotlin
    @Bindsabstract fun bindNewsRepository(impl: OfflineFirstNewsRepository): NewsRepository

Source and attribution

Source:new-silvermoon/awesome-android-agent-skillsin.github/skills/architecture/android-data-layerat commit82900ea

License: No license

Content belongs to its original authors. SourceWeft indexes it from a public repository.

Report or request removal