-
Notifications
You must be signed in to change notification settings - Fork 287
Add a new project for DataStore #663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
63 changes: 63 additions & 0 deletions
63
datastore/src/androidTest/java/com/example/datastore/snippets/DataStoreSnippetsTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
58
datastore/src/main/java/com/example/datastore/snippets/Home.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.