AppState Android Jetpack の一部。
Jetpack AppState は、Jetpack Compose の State を UI レイヤを超えて拡張し、アプリの任意のレイヤでリアクティブな状態を変換して共有できるようにするライブラリのグループです。AppState は、主に次の 2 つのライブラリで構成されています。
- Transform: Compose ランタイム上に構築されたライブラリ。UI なしで
@Composableブロックを実行し、状態ソースを組み合わせて ComposeStateに変換します。 - StateStore: 共有アプリの**状態**を Compose
Stateとして保存して公開する、**型安全性**なコンテナ。これは、複数のアプリケーション コンポーネント間で状態を共有するためによく使用されます。
Compose State が UI コンポーザブルの外部でどのように機能するか、また、Transform と StateStore がアーキテクチャ全体で mutableStateOf や SavedStateHandle.saveable などの既存の Compose API とどのように連携するかについては、UI の外部で Compose State を使用するをご覧ください。
アーティファクト
AppState の使用を開始するには、プロジェクトに必要なアーティファクトとサポート ライブラリを追加します。
| 名前 | 機能 | アーティファクト |
|---|---|---|
| 変換 | UI の外部で Compose State を生成して監視します(transform と listener)。 |
androidx.appstate.transform:transform |
| 変換のテスト | Transform(transformTest と TransformTestScope)のテスト ユーティリティ。 |
androidx.appstate.transform:transform-testing |
| StateStore | コアの型安全性状態コンテナと StateStoreKey の定義。 |
androidx.appstate.statestore:statestore |
| StateStore DataStore の統合 | Jetpack DataStore を使用して、StateStore キーをディスクに永続化して復元します。 |
androidx.appstate.statestore:statestore-datastore |
| Compose Compiler プラグイン | transform または listener を使用するモジュール内の @Composable 関数とブロックをコンパイルします。 |
org.jetbrains.kotlin.plugin.compose |
| KotlinX Serialization プラグイン | statestore-datastore を使用する場合に、@Serializable StateStoreKey とカスタム状態タイプのシリアライザを生成します。 |
org.jetbrains.kotlin.plugin.serialization |
| KotlinX Serialization ライブラリ | statestore-datastore を使用する場合の @Serializable StateStoreKey とカスタム状態タイプのランタイム シリアル化ライブラリ。 |
org.jetbrains.kotlinx:kotlinx-serialization-core |
プロジェクトの設定
プロジェクトに AppState ライブラリを追加するには、libs.versions.toml ファイルでバージョン、ライブラリ、Gradle プラグインを宣言します。
[versions]
appstate = "1.0.0-alpha01"
kotlin = "2.4.10"
kotlinxSerializationCore = "1.11.0"
[libraries]
# Transform library for producing and observing Compose State
androidx-appstate-transform = { module = "androidx.appstate.transform:transform", version.ref = "appstate" }
# Testing utilities for Transform
androidx-appstate-transform-testing = { module = "androidx.appstate.transform:transform-testing", version.ref = "appstate" }
# Core StateStore library
androidx-appstate-statestore = { module = "androidx.appstate.statestore:statestore", version.ref = "appstate" }
# DataStore integration for StateStore (and required serialization runtime)
androidx-appstate-statestore-datastore = { module = "androidx.appstate.statestore:statestore-datastore", version.ref = "appstate" }
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinxSerializationCore" }
[plugins]
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
# Required only when using statestore-datastore
jetbrains-kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
次に、モジュールの build.gradle.kts ファイルを構成します。
plugins {
...
alias(libs.plugins.compose.compiler)
// Required only when using statestore-datastore
alias(libs.plugins.jetbrains.kotlin.serialization)
}
android {
...
buildFeatures {
compose = true
}
}
dependencies {
...
implementation(libs.androidx.appstate.transform)
implementation(libs.androidx.appstate.statestore)
// Optional: DataStore integration for StateStore
implementation(libs.androidx.appstate.statestore.datastore)
implementation(libs.kotlinx.serialization.core)
...
testImplementation(libs.androidx.appstate.transform.testing)
}
コードサンプル
AppState レシピ リポジトリには、一般的な Android アーキテクチャ パターンとフォーム ファクタで Transform と StateStore を使用する方法を示す実用的なサンプルが含まれています。
フィードバックを送信
フィードバックの共有、バグの報告、機能のリクエストを行うには、Transform Issue Tracker または StateStore Issue Tracker を使用してください。