Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions datastore/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
120 changes: 120 additions & 0 deletions datastore/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.compose.compiler)

// [START android_datastore_proto_plugin]
alias(libs.plugins.google.protobuf)
// [END android_datastore_proto_plugin]

// [START android_datastore_serialization_plugin]
alias(libs.plugins.kotlin.serialization)
// [END android_datastore_serialization_plugin]
}

android {
namespace = "com.example.datastore.snippets"
compileSdk = 36

defaultConfig {
applicationId = "com.example.datastore.snippets"
minSdk = 23
targetSdk = 36
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
buildFeatures {
compose = true
}
}

dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.graphics)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.compose.material3)

// [START android_datastore_dependency]
// Typed DataStore (Typed API surface, such as Proto)
implementation(libs.androidx.datastore)

// Alternatively - without an Android dependency.
implementation(libs.androidx.datastore.core)
// [END android_datastore_dependency]

// [START android_datastore_preferences_dependency]
// Preferences DataStore (SharedPreferences like APIs)
implementation(libs.androidx.datastore.preferences)

// Alternatively - without an Android dependency.
implementation(libs.androidx.datastore.preferences.core)
// [END android_datastore_preferences_dependency]

// [START android_datastore_preferences_dependency_rxjava]
// optional - RxJava2 support
implementation(libs.androidx.datastore.preferences.rxjava2)

// optional - RxJava3 support
implementation(libs.androidx.datastore.preferences.rxjava3)
// [END android_datastore_preferences_dependency_rxjava]

implementation(libs.androidx.navigation3.ui)
implementation(libs.androidx.navigation3.runtime)

// [START android_datastore_proto_dependency]
implementation(libs.google.protobuf.kotlin.lite)
// [END android_datastore_proto_dependency]

// [START android_datastore_json_dependency]
implementation(libs.kotlinx.serialization.json)
// [END android_datastore_json_dependency]

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.androidx.test.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
debugImplementation(libs.androidx.compose.ui.tooling)
debugImplementation(libs.androidx.compose.ui.test.manifest)
}

// [START android_datastore_proto_task]
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:4.32.1"
}
generateProtoTasks {
all().forEach { task ->
task.builtins {
create("java") {
option("lite")
}
create("kotlin")
}
}
}
}
// [END android_datastore_proto_task]
21 changes: 21 additions & 0 deletions datastore/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.datastore.snippets

import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.datastore.snippets.json.JsonDataStoreScreen
import com.example.datastore.snippets.multiprocess.MultiProcessDataStoreScreen
import com.example.datastore.snippets.preferences.PreferencesDataStoreScreen
import com.example.datastore.snippets.proto.ProtoDataStoreScreen
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class DataStoreSnippetsTest {
@get:Rule
val rule = createComposeRule()

@Test
fun launchPreferencesDataStore() {
rule.setContent { PreferencesDataStoreScreen() }
rule.onNodeWithText("Preferences DataStore").assertExists()
}

@Test
fun launchProtoDataStore() {
rule.setContent { ProtoDataStoreScreen() }
rule.onNodeWithText("Proto DataStore").assertExists()
}

@Test
fun launchJsonDataStore() {
rule.setContent { JsonDataStoreScreen() }
rule.onNodeWithText("Json DataStore").assertExists()
}

@Test
fun launchMultiProcessDataStore() {
rule.setContent { MultiProcessDataStoreScreen() }
rule.onNodeWithText("Multi-process DataStore").assertExists()
}
}
42 changes: 42 additions & 0 deletions datastore/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2025 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Snippets">
<!-- // [START android_datastore_multiprocess_manifest] -->
<service
android:name=".TimestampUpdateService"
android:process=":my_process_id" />
<!-- // [END android_datastore_multiprocess_manifest] -->
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.Snippets">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
58 changes: 58 additions & 0 deletions datastore/src/main/java/com/example/datastore/snippets/Home.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.datastore.snippets

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
internal fun Home(backStack: MutableList<MainActivity.Snippets>) {
Column {
Item("Preferences Data Store") {
backStack.add(MainActivity.Snippets.PreferencesDataStore)
}
Item("Proto Data Store") {
backStack.add(MainActivity.Snippets.ProtoDataStore)
}
Item("Json Data Store") {
backStack.add(MainActivity.Snippets.JsonDataStore)
}
Item("Multi process Data Store") {
backStack.add(MainActivity.Snippets.MultiProcessDataStore)
}
}
}

@Composable
private fun Item(text: String, onClick: () -> Unit) {
Box(
Modifier
.fillMaxWidth()
.clickable(onClick = onClick)
.padding(10.dp)
) {
Text(fontSize = 30.sp, text = text)
}
}
Loading