# Data

> Persists app data on Android using Room, SQLite, SharedPreferences, DataStore, and encryption.

- Skill: `diegosouzapw/data` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add diegosouzapw/data`
- Raw SKILL.md: https://api.skillmd.com/api/skills/diegosouzapw/data/raw
- Safety review: PASS (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools, Mobile Development
- Tags: Android, Datastore, Encryption, Kotlin, Room, Sharedpreferences, Sqlite
- Author: diegosouzapw (https://skillmd.com/u/diegosouzapw)
- Updated: 2026-08-22
- Page: https://skillmd.com/skills/diegosouzapw/data

---


# 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)

