美文网首页
Android 架构组件 之 DataStore(6) - Pr

Android 架构组件 之 DataStore(6) - Pr

作者: 行走中的3卡 | 来源:发表于2022-11-10 11:05 被阅读0次

前面的文章有介绍过 DataStore 基本概念 和 Preferences DataStore的用法,
下面就开始介绍DataStore 的另一种 Proto DataStore.
示例代码仍然是使用之前的那个.

开发环境:
AS 2020.3.1 Fox
android gradle 4.2.1
gradle plugin 6.8.3

1. 添加依赖项

为了使用 Proto Data
为了使用 Proto DataStore,让协议缓冲区为我们的架构生成代码,
我们需要对 build.gradle 文件进行一些更改:

(1)添加 协议缓冲区 插件
(2)添加协议缓冲区和 Proto DataStore 依赖项
(3)配置协议缓冲区

project/build.gradle (官方源码并没有这个,可能是它的gradle 版本比较新

buildscript {
    //....
    dependencies {
        //...
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.9.1' //{@add proto datastore}
    }
}

app/build.gradle (plugins 和官方源码不一样,可能是它的gradle版本比较新, 可以在 插件名称 后 直接加 版本号)

plugins {
    ...
    id "com.google.protobuf" 
}

dependencies {
    implementation "androidx.datastore:datastore:1.0.0" //{@modify 取代:datastore-core:1.0.0 }
    implementation  "com.google.protobuf:protobuf-javalite:3.18.0"
    ...
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.14.0"
    }

    // Generates the java Protobuf-lite code for the Protobufs in this project. See
    // https://github.com/google/protobuf-gradle-plugin#customizing-protobuf-compilation
    // for more information.
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    option 'lite'
                }
            }
        }
    }

参考文献:
https://developer.android.com/codelabs/android-proto-datastore#4

相关文章

网友评论

      本文标题:Android 架构组件 之 DataStore(6) - Pr

      本文链接:https://www.haomeiwen.com/subject/lsibtdtx.html