概述
protobuf序列化
使用参考:README.md
https://github.com/google/protobuf-gradle-plugin
https://jingyan.baidu.com/article/925f8cb8a6c21ac0dce0566e.html
https://www.jianshu.com/p/0f047e1b7e16
开发环境
Android studio 3.6.1
jdk 1.8
gradle 5.6.4
gradle 插件 3.6.1
1.引入插件
在工程目录下的build.gradle中添加如下:
classpath ‘com.google.protobuf:protobuf-gradle-plugin:0.8.13’
buildscript {
repositories {
google()
jcenter()
//第一步
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
//第三步
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.13'
}
}
allprojects {
repositories {
google()
jcenter()
//第二步
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
在module下的build.gradle
apply plugin: 'com.android.application'
//第四步
apply plugin: 'com.google.protobuf'
android {
///第五步
sourceSets {
main {
java {
srcDir 'src/main/java'
}
proto {
srcDir 'src/main/protobuf' //指定.proto文件路径
include '**/*.proto'
}
}
}
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "io.gitbub.protobuf"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//第六步,android的与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.0.0'
}
plugins {
javalite {
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
remove java
}
task.plugins {
javalite {}
}
}
}
//配置生成java代码的目录
generatedFilesBaseDir = "$projectDir/src/generated"
}
2.在与java同级目录protobuf下编写Person.proto文件
syntax = "proto3";
//生成类的包名
package io.gitbub.protobuf;
//导入某个类
//import "google/protobuf/timestamp.proto";
//生成类的包名,比上面的package优先
//option java_package = "io.gitbub.protobuf";
//生成类的类名
option java_outer_classname = "PersonProto";
message Person {
string name = 1;
int32 id = 2;
string email = 3;
string phone = 4;
}
3.Sync一下,下载相应的插件
4.Rebuidle project 重构一下工程, 就会生成 PersonProto.java文件
5.简单使用
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
useProtobuf();
}
/**
* 简单使用
*/
public void useProtobuf() {
PersonProto.Person.Builder builder = PersonProto.Person.newBuilder();
builder.setId(10086);
builder.setName("kavin");
builder.setEmail("kavin_tian@163.com");
builder.setPhone("10086");
PersonProto.Person person = builder.build();
byte[] bytes = person.toByteArray();
String string = Arrays.toString(bytes);
Log.e("TAG", "person.toByteArray(): " + string);
Log.e("TAG", "personByteArray: length: " + bytes.length);
try {
PersonProto.Person parseFrom = PersonProto.Person.parseFrom(bytes);
int id = parseFrom.getId();
String name = parseFrom.getName();
String email = parseFrom.getEmail();
String phone = parseFrom.getPhone();
Log.e("TAG", "id: " + id);
Log.e("TAG", "name: " + name);
Log.e("TAG", "email: " + email);
Log.e("TAG", "name: " + phone);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
Log.e("TAG", "parseFrom: " + e);
}
}
}
打印结果:
E/TAG: person.toByteArray(): [10, 5, 107, 97, 118, 105, 110, 16, -26, 78, 26, 18, 107, 97, 118, 105, 110, 95, 116, 105, 97, 110, 64, 49, 54, 51, 46, 99, 111, 109, 34, 5, 49, 48, 48, 56, 54]
E/TAG: personByteArray: length: 37
E/TAG: id: 10086
E/TAG: name: kavin
E/TAG: email: kavin_tian@163.com
E/TAG: name: 10086
代码demo: https://gitee.com/kavin_tian/protobuf
最后
以上就是失眠宝贝为你收集整理的android protobuf使用的全部内容,希望文章能够帮你解决android protobuf使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复