home / skills / pluginagentmarketplace / custom-plugin-android / data

data skill

/skills/data

This skill helps Android developers securely persist data using Room, EncryptedSharedPreferences, and DataStore with best practices and migrations.

npx playbooks add skill pluginagentmarketplace/custom-plugin-android --skill data

Review the files below or copy the command above to add this skill to your agents.

Files (8)
SKILL.md
2.1 KB
---
name: data
description: Room ORM, SQLite, SharedPreferences, DataStore, encryption.
version: "2.0.0"
sasmp_version: "1.3.0"

# Agent Binding
bonded_agent: 04-data-management
bond_type: PRIMARY_BOND

# Skill Configuration
atomic: true
single_responsibility: Data persistence & storage

# Parameter Validation
parameters:
  storage_type:
    type: string
    enum: [room, datastore, preferences, encrypted]
    required: false
  operation:
    type: string
    enum: [create, read, update, delete, migrate]
    required: false

# Retry Configuration
retry:
  max_attempts: 2
  backoff: exponential
  on_failure: suggest_transaction_rollback

# Observability
logging:
  level: info
  include: [query, storage_type, security_level]
---

# Data Persistence Skill

## Quick Start

### Room Entity & DAO
```kotlin
@Entity
data class User(@PrimaryKey val id: Int, val name: String)

@Dao
interface UserDao {
    @Query("SELECT * FROM User")
    suspend fun getAllUsers(): List<User>
    
    @Insert
    suspend fun insert(user: User)
}
```

### EncryptedSharedPreferences
```kotlin
val prefs = EncryptedSharedPreferences.create(context, "secret",
    MasterKey.Builder(context).setKeyScheme(AES256_GCM).build(),
    AES256_SIV, AES256_GCM)

prefs.edit { putString("token", value) }
```

### DataStore
```kotlin
val dataStore = context.createDataStore("settings")
val preferences = dataStore.data.map { it[KEY] ?: "" }
```

## Key Concepts

### Room Advantages
- Type-safe queries
- Compile-time checks
- Suspend/Flow support
- Migration management

### SharedPreferences
- Simple key-value store
- Use Encrypted version for sensitive data
- Limited to small data

### DataStore
- Modern SharedPreferences
- Coroutine-native
- Type-safe
- ACID transactions

## Best Practices

✅ Use Room for complex data
✅ Encrypt sensitive data
✅ Implement proper migrations
✅ Handle database errors
✅ Test database operations

## Resources

- [Room Documentation](https://developer.android.com/training/data-storage/room)
- [DataStore Guide](https://developer.android.com/topic/libraries/architecture/datastore)

Overview

This skill provides practical guidance and examples for Android data persistence using Room (SQLite), EncryptedSharedPreferences, and DataStore, with a focus on secure storage and migration management. It bundles concise code snippets, core concepts, and best practices to help developers choose the right storage mechanism and implement it safely. The content is geared toward coroutine-friendly, testable solutions for app data handling.

How this skill works

The skill inspects common persistence patterns and supplies minimal, copy-ready examples: a Room Entity/DAO for structured SQLite storage, EncryptedSharedPreferences for small sensitive key-value pairs, and DataStore for modern coroutine-based preferences. It highlights type-safety, compile-time checks, ACID behavior, and encryption setup so you can implement storage with correct threading and migration strategies.

When to use it

  • Use Room for relational or complex structured data with queries, joins, and migrations.
  • Use EncryptedSharedPreferences for small secrets like tokens or user credentials.
  • Use DataStore for modern, coroutine-native key-value settings and small state.
  • Choose DataStore when you need ACID transactions and Flow/Coroutine integration.
  • Prefer EncryptedSharedPreferences or encrypted databases when handling sensitive PII or auth tokens.

Best practices

  • Define Room entities and DAOs with suspend or Flow return types for coroutine safety.
  • Always implement and test migrations to avoid runtime crashes on schema changes.
  • Encrypt sensitive key-value pairs with EncryptedSharedPreferences or an encrypted DB.
  • Limit SharedPreferences to small pieces of data; use Room or DataStore for larger datasets.
  • Wrap database calls in try/catch and provide clear error handling and retry logic.

Example use cases

  • Store user profiles, relationships, and offline cache in Room with migration support.
  • Persist authentication tokens securely using EncryptedSharedPreferences.
  • Save UI preferences, feature flags, and small settings with DataStore and expose via Flow.
  • Migrate legacy SharedPreferences keys into DataStore or Room during app updates.
  • Test DAO queries in isolation with an in-memory Room database during unit tests.

FAQ

When should I migrate from SharedPreferences to DataStore?

Migrate when you need coroutine support, stronger type safety, ACID guarantees, or to reduce risks with synchronous disk I/O on the main thread.

Is Room required if I use DataStore?

No. Use Room for structured relational data and complex queries; use DataStore for simple settings and preferences. They serve different needs and can coexist.