{"id":46815,"date":"2024-01-03T16:02:24","date_gmt":"2024-01-03T10:32:24","guid":{"rendered":"https:\/\/www.oneclickitsolution.com\/blog\/?p=46815"},"modified":"2025-06-26T16:24:40","modified_gmt":"2025-06-26T10:54:40","slug":"implementation-proto-datastore-in-android","status":"publish","type":"post","link":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android","title":{"rendered":"Step-by-Step Guide to Implement Proto DataStore in Android App Development"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-introduction-of-proto-datastore\" style=\"font-size:25px\"><strong>Introduction of Proto DataStore<\/strong><\/h2>\n\n\n\n<p>Proto DataStore is a library for storage purposes introduced by Android Jetpack. Using this, we can store medium-sized data like the recent searched locations list.&nbsp;<\/p>\n\n\n\n<p>We can use SharedPreferences for the same purpose, but it is not the main thread-safe and storing a list using it is not a good option.<\/p>\n\n\n\n<p>Moreover, Kotlin coroutines and Flow make data storing, using Proto Datastore, asynchronous and consistent. It also provides transaction support. A Flutter developer specializes in <a href=\"https:\/\/www.oneclickitsolution.com\/services\/android-app-development\">Android app development<\/a> using the Flutter framework.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-good-to-have-knowledge-of-proto-datastore\" style=\"font-size:25px\"><strong>Good to have knowledge of Proto DataStore<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Dependency Injection Using Hilt(Basics)<\/li>\n\n\n\n<li><strong><a href=\"https:\/\/www.oneclickitsolution.com\/blog\/choose-android-mvvm-over-mvp-architecture\/\" target=\"_blank\" rel=\"noreferrer noopener\">Knowledge about MVVM(Basics)<\/a><\/strong><\/li>\n\n\n\n<li>Flow and Coroutine(Basics)<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"328\" src=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/image-1.webp\" alt=\"Tools and Tech Stack Using in implementing Proto DataStore in Android App Development\" class=\"wp-image-63047\" srcset=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/image-1.webp 1200w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/image-1-768x210.webp 768w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/image-1-150x41.webp 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-why-it-is-important\" style=\"font-size:25px\"><strong>Why it is Important?<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Proto DataStore is the main thread-safe storage option that makes the app smoother as it does not stop the main thread.&nbsp;<\/li>\n\n\n\n<li>It is the safest way to store data. We need to create our data structure for storing data using <strong>protocol buffers<\/strong>.<\/li>\n\n\n\n<li>Uses Flow and Coroutines, which stores and reads data asynchronously.&nbsp;<\/li>\n\n\n\n<li>Accessible from UI thread as it uses Dispatcher. IO underneath its implementation.&nbsp;<\/li>\n\n\n\n<li>It provides transaction support which was missing in Shared Preference.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-features-of-proto-datastore\" style=\"font-size:25px\"><strong>Features of Proto DataStore <\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Easy to integrate compared to Room or any other databases.&nbsp;<\/li>\n\n\n\n<li>Easy to store structured data, which is not possible using Shared Preference.<\/li>\n\n\n\n<li>Provides a hinder-free experience to the user as it works on a separate thread.<\/li>\n\n\n\n<li>Frequent changes in the data can be observed in a facile manner using Flow and Coroutines.<\/li>\n\n\n\n<li>Protocol buffers get checked for data type mistakes during compilation. Therefore no type-safety errors can occur on run time.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-does-it-work\" style=\"font-size:25px\"><strong>How does it Work?<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\" id=\"h-1-add-plugin-dependencies-and-proto-buff-related-code-in-your-module-build-gradle-file\">1. Add Plugin, Dependencies and Proto Buff-related code in your module build.gradle file:<\/h3>\n\n\n\n<p>Add below code in build.gradle file which resides in the app folder and sync the project afterward.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"basic\" class=\"language-basic\">plugins {\n   id \"com.google.protobuf\" version \"0.8.17\"\n\n}\ndependencies {\n\/*************************************************************\n*  Proto Data Store\n*************************************************************\/\nimplementation  \"androidx.datastore:datastore-core:1.0.0\"\nimplementation  \"com.google.protobuf:protobuf-javalite:3.18.0\"\n\n\/*************************************************************\n*  Preference Data Store(Excluding this might cause some errors.)\n*************************************************************\/\nimplementation \"androidx.datastore:datastore-preferences:1.0.0\"\n}\nprotobuf {\n   protoc {\n       artifact = \"com.google.protobuf:protoc:3.14.0\"\n   }\n   generateProtoTasks {\n       all().each { task -&gt;\n           task.builtins {\n               java {\n                   option 'lite'\n\n               }\n           }\n       }\n   }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\" id=\"h-2-create-proto-file-at-shown-location\">2. Create .proto File at Shown Location<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/12\/proto_location.png\" alt=\"proto location in project\" class=\"wp-image-46828\"\/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\" id=\"h-3-making-data-structure-in-proto-file\">3. Making Data Structure in .proto File<\/h3>\n\n\n\n<p>You have to define your data structure in a .proto file.&nbsp;<\/p>\n\n\n\n<p>Here, Location is a model data class that contains information about a location. In this 1,2,3,4 are just unique numbers known as Field Numbers. Each variable should be assigned a unique field number. Try to allocate these field numbers between 1 to 15, as it takes only 1-byte memory. Field Number larger than 15 will take 2 bytes of memory space, so try to avoid it as long as possible.<\/p>\n\n\n\n<p>Recent Locations is the class that contains the list of Location objects.<\/p>\n\n\n\n<p>Provide your package name in the java_package parameter. All the generated classes will be stored in the package name provided by you.<\/p>\n\n\n\n<p>Build the project when you are done changing the proto file.&nbsp;<\/p>\n\n\n\n<p>For more information on<strong><strong> <\/strong><\/strong>proto buffer, <strong><a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/oveview\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">check out this link<\/a><\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"basic\" class=\"language-basic\">syntax = \"proto3\";\n\noption java_package = \"com.kotlin.mvvm.structure.datastore\";\/\/Replace it with your package.\noption java_multiple_files = true;\n\n\/\/ This is the Location class\nmessage Location{\n int32 id = 1;  \/\/ Unique ID number for this location.\n string name = 2;\n double longitude=3;\n double latitude=4;\n}\n\n\/\/ This is object which will contain the list of recent locations\nmessage RecentLocations {\n repeated Location location = 1;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\" id=\"h-4-create-a-hilt-module-for-dependency-injection\">4. Create a Hilt Module for Dependency Injection<\/h3>\n\n\n\n<p>You need to create a serializer for your proto class which will be used by Proto DataStore internally. This is boilerplate code which will be repeated every time you implement a Proto DataStore so just copy it and replace the class name with your class name.<\/p>\n\n\n\n<p>Here, RecentLocationsSerializer is created for serializing the RecentLocations class.&nbsp;<\/p>\n\n\n\n<p>After defining the serializer, create the Proto DataStore using the \u201cby <em>dataStore<\/em>\u201d property delegate as shown in the code.<\/p>\n\n\n\n<p>Now, provide this DataStore object using Hilt. For this demo, I have supplied this as a constructor parameter of DefaultDataRepostiory class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"basic\" class=\"language-basic\">@Module\n@InstallIn(SingletonComponent::class)\nclass AppModule {\n\n    \/**\n    * Some other code\u2026\n    *\/ \n\n\n   \/**\n    * For Proto Data Store\n    *\/\n   @Suppress(\"BlockingMethodInNonBlockingContext\")\n   object RecentLocationsSerializer : Serializer&lt;RecentLocations&gt; {\n       override val defaultValue: RecentLocations = RecentLocations.getDefaultInstance()\n\n       override suspend fun readFrom(input: InputStream): RecentLocations {\n           try {\n               return RecentLocations.parseFrom(input)\n           } catch (exception: InvalidProtocolBufferException) {\n               throw CorruptionException(\"Cannot read proto.\", exception)\n           } catch (e: java.io.IOException) {\n               e.printStackTrace()\n               throw e\n           }\n       }\n\n       override suspend fun writeTo(t: RecentLocations, output: OutputStream) = t.writeTo(output)\n   }\n\n   private val Context.recentLocationsDataStore: DataStore&lt;RecentLocations&gt; by dataStore(\n       fileName = \"RecentLocations.pb\",\n       serializer = RecentLocationsSerializer\n   )\n\n   @Provides\n   @Reusable\n   fun provideProtoDataStore(@ApplicationContext context: Context) =\n   \tcontext.recentLocationsDataStore\n\n   @Provides\n   @Reusable\n   internal fun providesDataRepository(\n      @ApplicationContext context: Context,\n      recentLocationsDataStore: DataStore&lt;RecentLocations&gt;\n   ): DataRepository {\n      return DefaultDataRepository(\n          context,\n          recentLocationsDataStore\n      ) as DataRepository\n   }\n}\n<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n<div class=\"box_section_read\">\n<p style=\"border-left: 5px solid #0072bb; padding: 20px 20px; font-size: 20px; line-height: 22px; color: #0072bb; text-align: center; font-style: italic; margin-bottom: 20px; font-weight: 700;\"><span style=\"color:#000000\"> Read More:<\/span> <a href=\"https:\/\/www.oneclickitsolution.com\/blog\/awesome-push-notifications-plugin-using-flutter\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Implement Awesome Push Notifications Plugin Using Flutter?<\/a><\/p>\n<\/div>\n\n<div class=\"wp-block-spacer\" style=\"height: 20px;\" aria-hidden=\"true\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\" id=\"h-5-create-a-repository-interface\">5. Create a Repository Interface<\/h3>\n\n\n\n<p>Making Repository Interface helps in unit testing as we can create a Fake Repository easily.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"basic\" class=\"language-basic\">interface DataRepository {\n\n   \/**\n    * This method is used for the purpose of fetching the recent locations items from the data store.\n    *\/\n   suspend fun getRecentLocations(): LiveData&lt;List&lt;Location&gt;&gt;\n\n   \/**\n    * This method is used for the purpose of adding the recent locations items in the data store.\n    *\/\n   suspend fun addRecentLocations(locations: List&lt;Location&gt;)\n\n   \/**\n    * This method is used for the purpose of clearing the recent locations items.\n    *\/\n   suspend fun clearAllRecentLocations()\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\" id=\"h-6-implement-repository\">6. Implement Repository<\/h3>\n\n\n\n<p>Here is the implementation for the repository interface we created before.&nbsp;<\/p>\n\n\n\n<p>To read data, dataStoreObject.data can be used as shown in getRecentLocations() implementation.<br>To add, update or clear data, we can use dataStoreObject.updateData which will provide us the DataStore class instance which we can manipulate as per our need. You can check out addRecentLocations() and clearAllRecentLocations() method implementations for knowing the usage of dataStoreObject.updateData method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"basic\" class=\"language-basic\">class DefaultDataRepository @Inject constructor(\n   private val context: Context,\n   private val recentLocationsDataStore: DataStore&lt;RecentLocations&gt;\n) : DataRepository {\n\n\n   override suspend fun getRecentLocations(): LiveData&lt;List&lt;Location&gt;&gt; {\n       return recentLocationsDataStore.data.asLiveData()\n           .switchMap { MutableLiveData(it.locationList) }\n   }\n\n   override suspend fun addRecentLocations(locations: List&lt;Location&gt;) {\n       recentLocationsDataStore.updateData { recentLocations: RecentLocations -&gt;\n           recentLocations.toBuilder().addAllLocation(locations).build()\n       }\n   }\n\n   override suspend fun clearAllRecentLocations() {\n       recentLocationsDataStore.updateData {\n           it.toBuilder().clear().build()\n       }\n   }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\" id=\"h-7-creating-recyclerview-adapter-and-raw-xml-layout-to-show-recent-locations\">7. Creating Recyclerview Adapter and Raw xml layout to show Recent Locations<\/h3>\n\n\n\n<p>For showing the list of recent locations, create a list or recycler view adapter and raw layout.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.oneclickitsolution.com\/contact-us\/\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"275\" src=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/12\/proto-datastore-cta-1.png\" alt=\"proto datastore cta\" class=\"wp-image-54392\" srcset=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/12\/proto-datastore-cta-1.png 1200w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/12\/proto-datastore-cta-1-768x176.png 768w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/12\/proto-datastore-cta-1-20x5.png 20w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\" id=\"h-8-creating-view-model\">8. Creating View Model<\/h3>\n\n\n\n<p>Finally, we will integrate all the created classes, repositories and adapters in the view model.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"basic\" class=\"language-basic\">@HiltViewModel\nclass ProtoViewModel @Inject constructor(\n   private val dataRepository: DataRepository\n) :ViewModel() {\n   \/\/Initializer\n   init {\n       \/\/Getting all recent locations\n       getAllRecentLocations()\n   }\n\n    \/**\n    * Some other code\u2026\n    *\/ \n\n   \/\/Private variables\n   private lateinit var _recentLocations: LiveData&lt;List&lt;Location&gt;&gt;\n\n   \/\/Live data variables\n   val recentLocations: LiveData&lt;List&lt;Location&gt;&gt; = _recentLocations\n\n   private fun getAllRecentLocations() = viewModelScope.launch {\n       _recentLocations = dataRepository.getRecentLocations()\n   }\n\n   fun addRecentLocations(locations:List&lt;Location&gt;) = viewModelScope.launch {\n       dataRepository.addRecentLocations(locations)\n   }\n\n   fun clearRecentLocations() = viewModelScope.launch {\n       dataRepository.clearAllRecentLocations()\n   }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\" id=\"h-9-use-the-view-model-in-your-activity-fragment\">9. Use the View Model in Your Activity\/Fragment<\/h3>\n\n\n\n<p>Now, use the view model instance in your activity\/fragment to perform DataStore operations.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"basic\" class=\"language-basic\">@AndroidEntryPoint\nclass FragmentProtoDataStoreDemo : Fragment(R.layout.fragment_proto_data_store_demo) {\n   \/**\n    * Proto View Model instance.\n    *\/\n   private val protoViewModel: ProtoViewModel by viewModels()\n\n   \/**\n    * Variables for storing fake location data.\n    *\/\n   private var lastId = 0\n   private var lastLatitude = 0.0\n   private var lastLongitude = 0.0\n\n    \/**\n    * Some other code\u2026\n    *\/ \n\n\n   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {\n       super.onViewCreated(view, savedInstanceState)\n       \u2026\nbinding.fragmentProtoDataStorePlusFab.setOnClickListener {\n           protoViewModel.addRecentLocations(\n               listOf(\n                   Location.newBuilder()\n                       .setId(++lastId)\n                       .setName(\"Location Number : $lastId\")\n                       .setLatitude(++lastLatitude)\n                       .setLongitude(++lastLongitude)\n                       .build()\n               )\n           )\n      }\nbinding.fragmentProtoDataStoreClearFab.setOnClickListener{\n           protoViewModel.clearRecentLocations()\n      }\nprotoViewModel.recentLocations.observe(viewLifecycleOwner) {\n               recentLocationsListAdapter.submitList(it.toMutableList())\n      }\n\n   }\n}\n<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n<div class=\"box_section_read\">\n<p style=\"border-left: 5px solid #0072bb; padding: 20px 20px; font-size: 20px; line-height: 22px; color: #0072bb; text-align: center; font-style: italic; margin-bottom: 20px; font-weight: 700;\"><span style=\"color:#000000\"> Read More:<\/span> <a href=\"https:\/\/www.oneclickitsolution.com\/blog\/android-custom-library\/\" target=\"_blank\" rel=\"noreferrer noopener\">Android Custom Library: Everything You Need to Know<\/a><\/p>\n<\/div>\n\n<div class=\"wp-block-spacer\" style=\"height: 20px;\" aria-hidden=\"true\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\" id=\"h-after-running-the-app-will-look-something-like-this\"><strong>After Running the App will Look Something like This<\/strong>:<\/h3>\n\n\n\n<p>On the click of the Add Floating Action button, a new location will be added and reflected.<\/p>\n\n\n\n<p>On the click of the Clear Floating Action Button, all locations will be deleted and reflected.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"266\" height=\"563\" src=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/floating-action-button-function.webp\" alt=\"Clear Floating Action Button\" class=\"wp-image-63045\" srcset=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/floating-action-button-function.webp 266w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/floating-action-button-function-150x317.webp 150w\" sizes=\"(max-width: 266px) 100vw, 266px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-challenges-and-solutions-in-proto-datastore-implementation\" style=\"font-size:25px\"><strong>Challenges and Solutions in Proto DataStore Implementation<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\" id=\"h-challenges\">Challenges: <\/h3>\n\n\n\n<p>If you are following google code labs or any other sources then you might have added only two dependencies in the project build.gradle file.&nbsp;<\/p>\n\n\n\n<p>implementation&nbsp; &#8220;androidx.datastore:datastore-core:1.0.0&#8221;<\/p>\n\n\n\n<p>implementation&nbsp; &#8220;com.google.protobuf:protobuf-javalite:3.18.0&#8221;<\/p>\n\n\n\n<p>This might cause an \u201cUnresolved reference: dataStore\u201d issue as shown in the below image.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1292\" height=\"743\" src=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/datastore-invalid-reference.webp\" alt=\"dataStore unresolved reference\" class=\"wp-image-63044\" srcset=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/datastore-invalid-reference.webp 1292w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/datastore-invalid-reference-768x442.webp 768w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/datastore-invalid-reference-150x86.webp 150w\" sizes=\"(max-width: 1292px) 100vw, 1292px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\" id=\"h-solution\">Solution:<\/h3>\n\n\n\n<p>This can be solved easily by adding the following dependency. Though it is the dependency for the preference data store, it solves the issue perfectly as shown in the below picture.<\/p>\n\n\n\n<p>implementation &#8220;androidx.datastore:datastore-preferences:1.0.0&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1296\" height=\"743\" src=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/datastore-found.webp\" alt=\"solution to find dataStore\" class=\"wp-image-63042\" srcset=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/datastore-found.webp 1296w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/datastore-found-768x440.webp 768w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/datastore-found-150x86.webp 150w\" sizes=\"(max-width: 1296px) 100vw, 1296px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\" style=\"font-size:25px\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Proto DataStore is a great way to store small to mid-range structured data in <strong><a href=\"https:\/\/www.oneclickitsolution.com\/hire-dedicated-kotlin-developers\/\" target=\"_blank\" rel=\"noreferrer noopener\">Kotlin Android Project<\/a><\/strong>. It can be implemented pretty fast and doesn\u2019t require much change. Moreover, It provides a better user experience due to main-thread safety.&nbsp;<\/p>\n\n\n\n<p>If you are <strong><a href=\"https:\/\/www.oneclickitsolution.com\/services\/mobile-app-development\/\" target=\"_blank\" rel=\"noreferrer noopener\">making an application<\/a><\/strong> and there is a need to store structured information, then you should consider using Proto DataStore.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.oneclickitsolution.com\/contact-us\/\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"275\" src=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/12\/proto-datastore-cta.png\" alt=\"proto datastore cta\" class=\"wp-image-54391\" srcset=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/12\/proto-datastore-cta.png 1200w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/12\/proto-datastore-cta-768x176.png 768w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/12\/proto-datastore-cta-20x5.png 20w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faqs-for-proto-datastore\" style=\"font-size:25px\"><strong>FAQs for Proto DataStore:<\/strong><\/h2>\n\n\n\n<h5 class=\"wp-block-heading has-medium-font-size\" id=\"h-1-what-is-proto-datastore-used-for\"><strong>1. What is Proto DataStore used for?<\/strong><\/h5>\n\n\n\n<p>Proto DataStore is used for storing small to medium size data. It is one of the alternatives for SharedPreference provided by Google.&nbsp;<\/p>\n\n\n\n<h5 class=\"wp-block-heading has-medium-font-size\" id=\"h-2-should-we-change-the-current-sharedpreference-implementation-and-use-this-instead\"><strong>2. Should we change the current SharedPreference Implementation and use this instead?<\/strong><\/h5>\n\n\n\n<p>No. If you are already using SharedPreference in your application, and it works perfectly with it, then there is no need to implement this. Moreover, this only works for Kotlin projects.&nbsp;<\/p>\n\n\n\n<h5 class=\"wp-block-heading has-medium-font-size\" id=\"h-3-why-use-this-in-place-of-room-or-any-other-databases\"><strong>3. Why use this in place of Room or any other databases?<\/strong><\/h5>\n\n\n\n<p>Suppose you have created an application without any database. Now, you need to store a list of some items. Using a database is not the best decision in this case as it will take too much time and effort. However, you have to change its version with each change.&nbsp;<\/p>\n\n\n\n<p>You can surely go for a database if there is a large set of data or multiple lists that you need to store.<\/p>\n\n\n\n<h5 class=\"wp-block-heading has-medium-font-size\" id=\"h-4-when-to-use-preference-datastore-and-when-proto-datastore\"><strong>4. When to use Preference DataStore and when Proto DataStore?<\/strong><\/h5>\n\n\n\n<p>When there is key-value pair data, then using preference DataStore will be a better choice. For example, storing the username of the user.&nbsp;<\/p>\n\n\n\n<p>You can use Proto DataStore when there is data that cannot be stored using primitive data types.<\/p>\n\n\n\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"What is Proto DataStore used for?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Proto DataStore is used for storing small to medium size data. It is one of the alternatives for SharedPreference provided by Google.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Should we change the current SharedPreference Implementation and use this instead?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"No. If you are already using SharedPreference in your application, and it works perfectly with it, then there is no need to implement this. Moreover, this only works for Kotlin projects.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Why use this in place of Room or any other databases?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Suppose you have created an application without any database. Now, you need to store a list of some items. Using a database is not the best decision in this case as it will take too much time and effort. However, you have to change its version with each change. \n\nYou can surely go for a database if there is a large set of data or multiple lists that you need to store.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"When to use Preference DataStore and when Proto DataStore?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"When there is key-value pair data, then using preference DataStore will be a better choice. For example, storing the username of the user. \n\nYou can use Proto DataStore when there is data that cannot be stored using primitive data types.\"\n    }\n  }]\n}\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Introduction of Proto DataStore Proto DataStore is a library for storage purposes introduced by Android Jetpack. Using this, we can store medium-sized data like the recent searched locations list.&nbsp; We can use SharedPreferences for the same purpose, but it is not the main thread-safe and storing a list using it is not a good option. &hellip;<\/p>\n","protected":false},"author":1,"featured_media":63041,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[516,22],"tags":[798,1338,795],"class_list":["post-46815","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobile-apps","category-technology","tag-best-mobile-application-development","tag-implement-proto-datastore-in-android","tag-mobile-app-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v18.2.1 (Yoast SEO v24.8.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Step-by-Step Guide to Implement Proto DataStore in Android<\/title>\n<meta name=\"description\" content=\"In this tutorial, We learn how to implement Jetpack Proto DataStore in the android applications. Explore the tutorial for more details.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Step-by-Step Guide to Implement Proto DataStore in Android\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, We learn how to implement Jetpack Proto DataStore in the android applications. Explore the tutorial for more details.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android\" \/>\n<meta property=\"og:site_name\" content=\"OneClick IT Consultancy\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/oneclickconsultancy\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-03T10:32:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-26T10:54:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/implementing-proto-datastore-in-android-app-development.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"OneClick IT Consultancy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@OneclickIT\" \/>\n<meta name=\"twitter:site\" content=\"@OneclickIT\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"OneClick IT Consultancy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Step-by-Step Guide to Implement Proto DataStore in Android","description":"In this tutorial, We learn how to implement Jetpack Proto DataStore in the android applications. Explore the tutorial for more details.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android","og_locale":"en_US","og_type":"article","og_title":"Step-by-Step Guide to Implement Proto DataStore in Android","og_description":"In this tutorial, We learn how to implement Jetpack Proto DataStore in the android applications. Explore the tutorial for more details.","og_url":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android","og_site_name":"OneClick IT Consultancy","article_publisher":"https:\/\/www.facebook.com\/oneclickconsultancy","article_published_time":"2024-01-03T10:32:24+00:00","article_modified_time":"2025-06-26T10:54:40+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/implementing-proto-datastore-in-android-app-development.webp","type":"image\/webp"}],"author":"OneClick IT Consultancy","twitter_card":"summary_large_image","twitter_creator":"@OneclickIT","twitter_site":"@OneclickIT","twitter_misc":{"Written by":"OneClick IT Consultancy","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android#article","isPartOf":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android"},"author":{"name":"OneClick IT Consultancy","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/person\/c2616c0a433427a79a96fe5ca2b34ec3"},"headline":"Step-by-Step Guide to Implement Proto DataStore in Android App Development","datePublished":"2024-01-03T10:32:24+00:00","dateModified":"2025-06-26T10:54:40+00:00","mainEntityOfPage":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android"},"wordCount":1151,"commentCount":0,"publisher":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android#primaryimage"},"thumbnailUrl":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/implementing-proto-datastore-in-android-app-development.webp","keywords":["Best Mobile Application Development","Implement Proto DataStore in Android","Mobile App Development"],"articleSection":["Mobile Application","Technology"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android","url":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android","name":"Step-by-Step Guide to Implement Proto DataStore in Android","isPartOf":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android#primaryimage"},"image":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android#primaryimage"},"thumbnailUrl":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/implementing-proto-datastore-in-android-app-development.webp","datePublished":"2024-01-03T10:32:24+00:00","dateModified":"2025-06-26T10:54:40+00:00","description":"In this tutorial, We learn how to implement Jetpack Proto DataStore in the android applications. Explore the tutorial for more details.","breadcrumb":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android#primaryimage","url":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/implementing-proto-datastore-in-android-app-development.webp","contentUrl":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2024\/01\/implementing-proto-datastore-in-android-app-development.webp","width":1200,"height":628,"caption":"Step by Step Guide to Implement Proto DataStore in Android App Development"},{"@type":"BreadcrumbList","@id":"https:\/\/www.oneclickitsolution.com\/blog\/implementation-proto-datastore-in-android#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.oneclickitsolution.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Step-by-Step Guide to Implement Proto DataStore in Android App Development"}]},{"@type":"WebSite","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#website","url":"https:\/\/www.oneclickitsolution.com\/blog\/","name":"OneClick IT Consultancy","description":"We Build Brands from Ideas","publisher":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/#organization"},"alternateName":"OneClick IT Solution","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.oneclickitsolution.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#organization","name":"OneClick IT Consultancy","alternateName":"OneClick IT Solution","url":"https:\/\/www.oneclickitsolution.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/oneclick-official-logo.png","contentUrl":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/oneclick-official-logo.png","width":100,"height":100,"caption":"OneClick IT Consultancy"},"image":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/oneclickconsultancy","https:\/\/x.com\/OneclickIT","https:\/\/www.instagram.com\/oneclick.it.consultancy\/","https:\/\/www.linkedin.com\/company\/one-click-it-consultancy\/","https:\/\/www.pinterest.com\/oneclickitconsultancy\/","https:\/\/www.youtube.com\/channel\/UCsEG6aiwOwvYrcZxMoP5xjg","https:\/\/oneclickit.tumblr.com\/","https:\/\/dribbble.com\/oneclickitconsultancy"]},{"@type":"Person","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/person\/c2616c0a433427a79a96fe5ca2b34ec3","name":"OneClick IT Consultancy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8169ffe1b63da548d77fb666e94f1aba?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8169ffe1b63da548d77fb666e94f1aba?s=96&d=mm&r=g","caption":"OneClick IT Consultancy"},"description":"OneClick IT Consultancy is the best custom software development company based in India &amp; USA with expertise in BLE, travel, mobile, and web development. With nearly a decade\u2019s experience, we use best practices and development standards to deliver high-performance applications, focused on the user experience.","sameAs":["https:\/\/www.oneclickitsolution.com\/blog\/"],"jobTitle":"Founder","url":"https:\/\/www.oneclickitsolution.com\/blog\/author\/oneclick"}]}},"_links":{"self":[{"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/posts\/46815"}],"collection":[{"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/comments?post=46815"}],"version-history":[{"count":3,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/posts\/46815\/revisions"}],"predecessor-version":[{"id":63999,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/posts\/46815\/revisions\/63999"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/media\/63041"}],"wp:attachment":[{"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/media?parent=46815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/categories?post=46815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/tags?post=46815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}