Android DataStore Proto框架存储接入AndroidStudio教程详解与使用

一、介绍

        通过前面的文字,我们已掌握了DataStore 的存储,但是留下一个尾巴,那就是Proto的接入。

Proto是什么?

Protobuf,类似于json和xml,是一种序列化结构数据机制,可以用于数据通讯等场景,相对于xml而言更小,相对于json而言解析更快,支持多语言

官网:Language Guide (proto 3) | Protocol Buffers Documentation

二、AndroidStudio加入Proto流程

1、项目build引入tools:

classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'

2、module引入插件:

apply plugin: 'com.google.protobuf'

3、在module的build文件进行配置

3.1指定proto文件目录

sourceSets {
    main {
        proto {
            //指定proto文件位置,你的proto文件放置在此文件夹中
            srcDir 'src/main/proto'
        }

    }

}

3.2引入依赖库

implementation 'com.google.protobuf:protobuf-java:3.5.1'
implementation 'com.google.protobuf:protoc:3.5.1'
implementation "com.suning.oneplayer:commonutils:1.10.30"

3.3.在build最外层加入proto节点

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.5.1' // 也可以配置本地编译器路径
    }

    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.builtins {
                java {}// 生产java源码
            }
        }
    }
}

注意:直接新增protobuf,这个和android以及dependencies是评级。

3.4在main文件夹下新建一个proto的文件夹

 这样,我们已完成了proto接入android的流程。

三、Proto如何对象的创建

先简单的看下一个小demo:

syntax = "proto3";

message SearchRequest {
  string query = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
}

解释:

syntax = "proto3";指定语言版本

message SearchRequest 定义一个消息

string 和int32是参数类型

从下到下参数后面都被指向了序列号,这些后面在序列化的时候的顺序。

数据类型:

.proto Type Notes Java/Kotlin Type[1]
double double
float float
int32 Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead. int
int64 Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. long
uint32 Uses variable-length encoding. int[2]
uint64 Uses variable-length encoding. long[2]
sint32 Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. int
sint64 Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. long
fixed32 Always four bytes. More efficient than uint32 if values are often greater than 228. int[2]
fixed64 Always eight bytes. More efficient than uint64 if values are often greater than 256. long[2]
sfixed32 Always four bytes. int
sfixed64 Always eight bytes. long
bool boolean
string A string must always contain UTF-8 encoded or 7-bit ASCII text, and cannot be longer than 232. String
bytes May contain any arbitrary sequence of bytes no longer than 232. ByteString

 新增:repeated

repeated 在proto的语法类似List

repeated Person list=1,类似list<Person>
 

头部扩展:

syntax:指定proto的版本,protobuf目前有proto2和proto3两个常用版本,如果没有声明,则默认是proto2.

package:指定包名。

import:导入包,类似于java的import.

java_package:指定生成类所在的包名

java_outer_classname:定义当前文件的类名,如果没有定义,则默认为文件的首字母大写名称

message:定义类,类似于java class;可以嵌套repeated:字段可以有多个内容(包括0),类似于array
 

枚举:enum 

enum Corpus {
  CORPUS_UNSPECIFIED = 0;
  CORPUS_UNIVERSAL = 1;
  CORPUS_WEB = 2;
  CORPUS_IMAGES = 3;
  CORPUS_LOCAL = 4;
  CORPUS_NEWS = 5;
  CORPUS_PRODUCTS = 6;
  CORPUS_VIDEO = 7;
}

proto也支持枚举,如上面所示,枚举也要指定tag索引序列号

默认值:

  1. 对于字符串,默认值为空字符串。
  2. 对于字节,默认值为空字节。
  3. 对于布尔值,默认值为false。
  4. 对于数字类型,默认值为零。
  5. 对于枚举,默认值是第一个定义的枚举值,该值必须为0。
  6. 对于消息字段,未设置该字段。它的确切值取决于语言。有关详细信息,请参阅生成的代码指南。

小试牛刀:

定义一个Settings.proto

syntax = "proto3";

option java_package = "com.example.wiik.testdemo.proto";
option java_multiple_files = true;
message Settings {
  int32 example_counter = 1;
  string name=2;
}

这样我们就完成了proto对象的创建。

如何引用prtot对象创建:

        val set=Settings.newBuilder().setName("name").setExampleCounter(1).build()
        set.name
        set.exampleCounter

这样我们就完成对象的创建。

四、总结

        关于如何使用proto的语法,这边文章不予过多介绍。如果需要的,可以前往官网学习。这样DataStore proto的存储已形成闭环。

猜你喜欢

转载自blog.csdn.net/qq36246172/article/details/129087977