Skip to content

Commit 3759291

Browse files
ralstondasilvaRalston Da Silva
andauthored
Add a new project for DataStore (#663)
The data store details on DAC has inline code snippets https://developer.android.com/topic/libraries/architecture/datastore This PR adds code samples that can be used as snippets on DAC. Co-authored-by: Ralston Da Silva <[email protected]>
1 parent 977c8e0 commit 3759291

39 files changed

+1558
-0
lines changed

datastore/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

datastore/build.gradle.kts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
plugins {
2+
alias(libs.plugins.android.application)
3+
alias(libs.plugins.kotlin.android)
4+
alias(libs.plugins.compose.compiler)
5+
6+
// [START android_datastore_proto_plugin]
7+
alias(libs.plugins.google.protobuf)
8+
// [END android_datastore_proto_plugin]
9+
10+
// [START android_datastore_serialization_plugin]
11+
alias(libs.plugins.kotlin.serialization)
12+
// [END android_datastore_serialization_plugin]
13+
}
14+
15+
android {
16+
namespace = "com.example.datastore.snippets"
17+
compileSdk = 36
18+
19+
defaultConfig {
20+
applicationId = "com.example.datastore.snippets"
21+
minSdk = 23
22+
targetSdk = 36
23+
versionCode = 1
24+
versionName = "1.0"
25+
26+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
27+
}
28+
29+
buildTypes {
30+
release {
31+
isMinifyEnabled = false
32+
proguardFiles(
33+
getDefaultProguardFile("proguard-android-optimize.txt"),
34+
"proguard-rules.pro"
35+
)
36+
}
37+
}
38+
compileOptions {
39+
sourceCompatibility = JavaVersion.VERSION_11
40+
targetCompatibility = JavaVersion.VERSION_11
41+
}
42+
kotlinOptions {
43+
jvmTarget = "11"
44+
}
45+
buildFeatures {
46+
compose = true
47+
}
48+
}
49+
50+
dependencies {
51+
implementation(libs.androidx.core.ktx)
52+
implementation(libs.androidx.lifecycle.runtime)
53+
implementation(libs.androidx.activity.compose)
54+
implementation(platform(libs.androidx.compose.bom))
55+
implementation(libs.androidx.compose.ui)
56+
implementation(libs.androidx.compose.ui.graphics)
57+
implementation(libs.androidx.compose.ui.tooling.preview)
58+
implementation(libs.androidx.compose.material3)
59+
60+
// [START android_datastore_dependency]
61+
// Typed DataStore (Typed API surface, such as Proto)
62+
implementation(libs.androidx.datastore)
63+
64+
// Alternatively - without an Android dependency.
65+
implementation(libs.androidx.datastore.core)
66+
// [END android_datastore_dependency]
67+
68+
// [START android_datastore_preferences_dependency]
69+
// Preferences DataStore (SharedPreferences like APIs)
70+
implementation(libs.androidx.datastore.preferences)
71+
72+
// Alternatively - without an Android dependency.
73+
implementation(libs.androidx.datastore.preferences.core)
74+
// [END android_datastore_preferences_dependency]
75+
76+
// [START android_datastore_preferences_dependency_rxjava]
77+
// optional - RxJava2 support
78+
implementation(libs.androidx.datastore.preferences.rxjava2)
79+
80+
// optional - RxJava3 support
81+
implementation(libs.androidx.datastore.preferences.rxjava3)
82+
// [END android_datastore_preferences_dependency_rxjava]
83+
84+
implementation(libs.androidx.navigation3.ui)
85+
implementation(libs.androidx.navigation3.runtime)
86+
87+
// [START android_datastore_proto_dependency]
88+
implementation(libs.google.protobuf.kotlin.lite)
89+
// [END android_datastore_proto_dependency]
90+
91+
// [START android_datastore_json_dependency]
92+
implementation(libs.kotlinx.serialization.json)
93+
// [END android_datastore_json_dependency]
94+
95+
testImplementation(libs.junit)
96+
androidTestImplementation(libs.androidx.test.ext.junit)
97+
androidTestImplementation(libs.androidx.test.espresso.core)
98+
androidTestImplementation(platform(libs.androidx.compose.bom))
99+
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
100+
debugImplementation(libs.androidx.compose.ui.tooling)
101+
debugImplementation(libs.androidx.compose.ui.test.manifest)
102+
}
103+
104+
// [START android_datastore_proto_task]
105+
protobuf {
106+
protoc {
107+
artifact = "com.google.protobuf:protoc:4.32.1"
108+
}
109+
generateProtoTasks {
110+
all().forEach { task ->
111+
task.builtins {
112+
create("java") {
113+
option("lite")
114+
}
115+
create("kotlin")
116+
}
117+
}
118+
}
119+
}
120+
// [END android_datastore_proto_task]

datastore/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datastore.snippets
18+
19+
import androidx.compose.ui.test.junit4.createComposeRule
20+
import androidx.compose.ui.test.onNodeWithText
21+
import androidx.test.ext.junit.runners.AndroidJUnit4
22+
import com.example.datastore.snippets.json.JsonDataStoreScreen
23+
import com.example.datastore.snippets.multiprocess.MultiProcessDataStoreScreen
24+
import com.example.datastore.snippets.preferences.PreferencesDataStoreScreen
25+
import com.example.datastore.snippets.proto.ProtoDataStoreScreen
26+
import org.junit.Rule
27+
import org.junit.Test
28+
import org.junit.runner.RunWith
29+
30+
/**
31+
* Instrumented test, which will execute on an Android device.
32+
*
33+
* See [testing documentation](http://d.android.com/tools/testing).
34+
*/
35+
@RunWith(AndroidJUnit4::class)
36+
class DataStoreSnippetsTest {
37+
@get:Rule
38+
val rule = createComposeRule()
39+
40+
@Test
41+
fun launchPreferencesDataStore() {
42+
rule.setContent { PreferencesDataStoreScreen() }
43+
rule.onNodeWithText("Preferences DataStore").assertExists()
44+
}
45+
46+
@Test
47+
fun launchProtoDataStore() {
48+
rule.setContent { ProtoDataStoreScreen() }
49+
rule.onNodeWithText("Proto DataStore").assertExists()
50+
}
51+
52+
@Test
53+
fun launchJsonDataStore() {
54+
rule.setContent { JsonDataStoreScreen() }
55+
rule.onNodeWithText("Json DataStore").assertExists()
56+
}
57+
58+
@Test
59+
fun launchMultiProcessDataStore() {
60+
rule.setContent { MultiProcessDataStoreScreen() }
61+
rule.onNodeWithText("Multi-process DataStore").assertExists()
62+
}
63+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2025 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
18+
19+
<application
20+
android:allowBackup="true"
21+
android:icon="@mipmap/ic_launcher"
22+
android:label="@string/app_name"
23+
android:roundIcon="@mipmap/ic_launcher_round"
24+
android:supportsRtl="true"
25+
android:theme="@style/Theme.Snippets">
26+
<!-- // [START android_datastore_multiprocess_manifest] -->
27+
<service
28+
android:name=".TimestampUpdateService"
29+
android:process=":my_process_id" />
30+
<!-- // [END android_datastore_multiprocess_manifest] -->
31+
<activity
32+
android:name=".MainActivity"
33+
android:exported="true"
34+
android:theme="@style/Theme.Snippets">
35+
<intent-filter>
36+
<action android:name="android.intent.action.MAIN" />
37+
<category android:name="android.intent.category.LAUNCHER" />
38+
</intent-filter>
39+
</activity>
40+
</application>
41+
42+
</manifest>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datastore.snippets
18+
19+
import androidx.compose.foundation.clickable
20+
import androidx.compose.foundation.layout.Box
21+
import androidx.compose.foundation.layout.Column
22+
import androidx.compose.foundation.layout.fillMaxWidth
23+
import androidx.compose.foundation.layout.padding
24+
import androidx.compose.material3.Text
25+
import androidx.compose.runtime.Composable
26+
import androidx.compose.ui.Modifier
27+
import androidx.compose.ui.unit.dp
28+
import androidx.compose.ui.unit.sp
29+
30+
@Composable
31+
internal fun Home(backStack: MutableList<MainActivity.Snippets>) {
32+
Column {
33+
Item("Preferences Data Store") {
34+
backStack.add(MainActivity.Snippets.PreferencesDataStore)
35+
}
36+
Item("Proto Data Store") {
37+
backStack.add(MainActivity.Snippets.ProtoDataStore)
38+
}
39+
Item("Json Data Store") {
40+
backStack.add(MainActivity.Snippets.JsonDataStore)
41+
}
42+
Item("Multi process Data Store") {
43+
backStack.add(MainActivity.Snippets.MultiProcessDataStore)
44+
}
45+
}
46+
}
47+
48+
@Composable
49+
private fun Item(text: String, onClick: () -> Unit) {
50+
Box(
51+
Modifier
52+
.fillMaxWidth()
53+
.clickable(onClick = onClick)
54+
.padding(10.dp)
55+
) {
56+
Text(fontSize = 30.sp, text = text)
57+
}
58+
}

0 commit comments

Comments
 (0)