赞
踩
通过前面的文字,我们已掌握了DataStore 的存储,但是留下一个尾巴,那就是Proto的接入。
Protobuf,类似于json和xml,是一种序列化结构数据机制,可以用于数据通讯等场景,相对于xml而言更小,相对于json而言解析更快,支持多语言
官网:Language Guide (proto 3) | Protocol Buffers Documentation
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'
apply plugin: 'com.google.protobuf'
sourceSets { main { proto { //指定proto文件位置,你的proto文件放置在此文件夹中 srcDir 'src/main/proto' } } }
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"
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是评级。
这样,我们已完成了proto接入android的流程。
先简单的看下一个小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 在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 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索引序列号
定义一个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的存储已形成闭环。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。