当前位置:   article > 正文

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

android datastore

一、介绍

        通过前面的文字,我们已掌握了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:

  1. syntax = "proto3";
  2. message SearchRequest {
  3. string query = 1;
  4. int32 page_number = 2;
  5. int32 result_per_page = 3;
  6. }

解释:

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

message SearchRequest 定义一个消息

string 和int32是参数类型

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

数据类型:

.proto TypeNotesJava/Kotlin Type[1]
doubledouble
floatfloat
int32Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead.int
int64Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead.long
uint32Uses variable-length encoding.int[2]
uint64Uses variable-length encoding.long[2]
sint32Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s.int
sint64Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s.long
fixed32Always four bytes. More efficient than uint32 if values are often greater than 228.int[2]
fixed64Always eight bytes. More efficient than uint64 if values are often greater than 256.long[2]
sfixed32Always four bytes.int
sfixed64Always eight bytes.long
boolboolean
stringA string must always contain UTF-8 encoded or 7-bit ASCII text, and cannot be longer than 232.String
bytesMay 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 

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

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

默认值:

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

小试牛刀:

定义一个Settings.proto

  1. syntax = "proto3";
  2. option java_package = "com.example.wiik.testdemo.proto";
  3. option java_multiple_files = true;
  4. message Settings {
  5. int32 example_counter = 1;
  6. string name=2;
  7. }

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

如何引用prtot对象创建:

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

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

四、总结

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/752793
推荐阅读
相关标签
  

闽ICP备14008679号