文章目录
- 环境配置:
- 基本使用方式
- 注意事项:
- 参考链接:
环境配置:
< ProjectName>/build.gradle
1
2
3
4
5
6
7
8
9buildscript { repositories { mavenCentral() } dependencies { classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.17' } }
< ProjectName>/app/build.gradle
1
2
3
4apply plugin: 'com.android.application' // or 'com.android.library' apply plugin: 'com.google.protobuf' // or apply plugin: 'com.google.protobuf' version "0.8.17" //这种方式可以不用在classpath中添加插件依赖
-
protobuf-lite以来方式
- version3.8以前
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31dependencies { // You need to depend on the lite runtime library, not protobuf-java implementation 'com.google.protobuf:protobuf-lite:3.0.0' } protobuf { protoc { // You still need protoc like in the non-Android case artifact = 'com.google.protobuf:protoc:3.7.0' } plugins { javalite { // The codegen for lite comes as a separate artifact artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0' } } generateProtoTasks { all().each { task -> task.builtins { // In most cases you don't need the full Java output // if you use the lite output. remove java } task.plugins { javalite { } } } } }
- version3.8以后
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20dependencies { // You need to depend on the lite runtime library, not protobuf-java implementation 'com.google.protobuf:protobuf-javalite:3.8.0' } protobuf { protoc { artifact = 'com.google.protobuf:protoc:3.8.0' } generateProtoTasks { all().each { task -> task.builtins { java { option "lite" } } } } }
- protobuf-java方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21dependencies { implementation 'com.google.protobuf:protobuf-java:3.17.3' } protobuf { protoc { artifact = 'com.google.protobuf:protoc:3.17.3' } generateProtoTasks { all().each { task -> task.builtins { remove java } task.plugins { java {} } } } }
基本使用方式
这里对最基本的使用方式进行举例。对proto
的文件夹默认定在../app/src/main/proto
文件夹中,如果想更改位置可以使用以下方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14android { sourceSets { main { jniLibs.srcDir 'libs' jni.srcDirs = [] //disable automatic ndk-build proto { // proto 文件默认路径是 src/main/proto // 可以通过 srcDir 修改 proto 文件的位置 srcDir 'src/main/proto' } } } }
创建拓展名为proto
的文件,如test.proto
,其内容格式如下:
1
2
3
4
5
6
7
8
9
10syntax = "proto3"; option java_package = "com.sample.android_sample_preference_datastore"; option java_multiple_files = true; message UserStore { bool is_logged_in = 1; string user_name = 2; }
当对test.proto
文件的更改,需要进行重新编译(build)来重新生成代码,生成的代码位于app/build/generated/source/proto
下面
注意事项:
- protocol buffer 需要使用最新版本,否则可能会出现编译错误
- protobuf-java是完整版本,protobuf-lite是轻量版本,轻量版本里面只有基本类型,没有
google/protobuf/any.proto
文件 - protocol buffer数据格式使用的话最好双方统一为该数据格式,否则一方面使用protocol buffer,一方面使用json或者xml的话,需要使用额外的数据格式转换库(比如protobuf-java-format)。但是这种方式会比较麻烦而且要兼容一些额外的操作,比如Any的范型
- 在使用json和protocol buffer进行转换的时候,如果在测试的时候需要注意不能使用单引号’。例如以下两种方式在单纯的解析时候可以使用,但是单引号那种在protocol buffer就不可以使用
第一种转义符使用:复制代码1
2val json = "{"code":"3"}"
第二种单引号使用(错误示范):
1
2
3
4```kotlin val json = "{'code':'3'}" ```
- Mac电脑上的M1芯片兼容问题
在Mac电脑的M1芯片进行编译Protocol文件时候会出现类似以下的错误:
1
2Could not find protoc-3.14.0-osx-aarch_64.exe (com.google.protobuf:protoc:3.14.0)
参考github上面的解决对gradle配置的解决方案如下:
修改文件地址: ../app/build.gradle
1
2
3
4
5
6
7
8
9
10protobuf { protoc { if (osdetector.os == "osx") {//区分平台 artifact = 'com.google.protobuf:protoc:3.14.0:osx-x86_64' } else { artifact = 'com.google.protobuf:protoc:3.14.0' } } }
当然上面还提供了maven的解决方案,如下(该方案没有尝试过)
修改文件地址 ~/.m2/settings.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<settings> ... <activeProfiles> <activeProfile> apple-silicon </activeProfile> ... </activeProfiles> <profiles> <profile> <id>apple-silicon</id> <properties> <os.detected.classifier>osx-x86_64</os.detected.classifier> </properties> </profile> ... </profiles> ... </settings>
参考链接:
- Android 中使用Protocol Buffer
https://blog.csdn.net/qq_35599978/article/details/80386356
- 查看protocol buffer最新版本
https://github.com/google/protobuf-gradle-plugin
- Protocol Buffer
https://developers.google.com/protocol-buffers/docs/proto3
- Protocol Buffer在Java上的使用
https://developers.google.com/protocol-buffers/docs/javatutorial
-
Protocol Buffer在网络数据传输中的使用
https://blog.csdn.net/weixin_32757449/article/details/114093565 -
protobuf与json相互转换的方法
https://www.cnblogs.com/pcheng/p/9586039.html -
protobuf-java-format
https://github.com/bivas/protobuf-java-format -
proto在Android上的使用
https://proandroiddev.com/android-jetpack-proto-datastore-a11ff8edcda8 -
Mac电脑的M1芯片兼容问题
https://github.com/grpc/grpc-java/issues/7690 -
Android Developer官网中datastore的使用https://developer.android.google.cn/topic/libraries/architecture/datastore?hl=zh-cn
最后
以上就是高兴猎豹最近收集整理的关于Android上使用Protocol Buffer的全部内容,更多相关Android上使用Protocol内容请搜索靠谱客的其他文章。
发表评论 取消回复