Android 中使用Protocol Buffer
宝宝其实是根据一老外的文章学习的,原文:
https://medium.com/@elye.project/simple-android-protobuf-tutorial-with-actual-code-bfb581299f47
本篇文章主要介绍Android中配置Gradle Protocol Buffer,及一个简单的使用demo,下篇文件讲解API
在其中一个项目中遇到SharedPerfernce 插入数据时报ANR,弄的我是有口难言,谁叫我滥用SP呢?
无意中在一本讲python的书中讲到protobuf,研究之,兴奋之,撸之!其实我的原本的想法是把protobuf
封装成SharedPerfernce这样的方式既能快速使用,又能提升性能。于是protobuf处女作
宝宝在marven上面找到最新的插件,可以看出,上面已经开始落满了灰。但是有名言不是说了么,
酒香不怕巷子深,猪肉不怕年头陈。
截至2018/05/20
在mvnrepository 上面找到最新的 protobuf-gradle-plugin 0.8.5下面是官网
http://mvnrepository.com/artifact/com.google.protobuf/protobuf-gradle-plugin/0.8.5
Protocol Buffers [Lite] 最新的3.0.1
http://mvnrepository.com/artifact/com.google.protobuf/protobuf-lite
protoc 最新的为 3.5.1
http://mvnrepository.com/artifact/com.google.protobuf/protoc
:protoc-gen-javalite 最新的为 3.0.0
https://medium.com/@elye.project/simple-android-protobuf-tutorial-with-actual-code-bfb581299f47
本篇文章主要介绍Android中配置Gradle Protocol Buffer,及一个简单的使用demo,下篇文件讲解API
在其中一个项目中遇到SharedPerfernce 插入数据时报ANR,弄的我是有口难言,谁叫我滥用SP呢?
无意中在一本讲python的书中讲到protobuf,研究之,兴奋之,撸之!其实我的原本的想法是把protobuf
封装成SharedPerfernce这样的方式既能快速使用,又能提升性能。于是protobuf处女作
宝宝在marven上面找到最新的插件,可以看出,上面已经开始落满了灰。但是有名言不是说了么,
酒香不怕巷子深,猪肉不怕年头陈。
截至2018/05/20
在mvnrepository 上面找到最新的 protobuf-gradle-plugin 0.8.5下面是官网
http://mvnrepository.com/artifact/com.google.protobuf/protobuf-gradle-plugin/0.8.5
Protocol Buffers [Lite] 最新的3.0.1
http://mvnrepository.com/artifact/com.google.protobuf/protobuf-lite
protoc 最新的为 3.5.1
http://mvnrepository.com/artifact/com.google.protobuf/protoc
:protoc-gen-javalite 最新的为 3.0.0
http://mvnrepository.com/artifact/com.google.protobuf/protoc-gen-javalite
AndroidStudio Project build.gradle
buildscript { repositories { jcenter() google() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5' } } allprojects { repositories { jcenter() google() } } task clean(type: Delete) { delete rootProject.buildDir }
application build.gradle
apply plugin: 'com.android.application' apply plugin: 'com.google.protobuf' android { compileSdkVersion 25 buildToolsVersion '26.0.2' defaultConfig { applicationId "com.example.rxjavademo" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } repositories { flatDir { dirs 'libs' } } } protobuf { protoc { artifact = 'com.google.protobuf:protoc:3.5.1-1' } plugins { javalite { artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0' } } generateProtoTasks { all().each { task -> task.builtins { remove java } task.plugins { javalite {} } } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'io.reactivex.rxjava2:rxjava:2.0.1' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' implementation 'com.google.protobuf:protobuf-lite:3.0.1' implementation 'com.squareup.okhttp3:okhttp:3.8.0' }
在main 文件夹下创建proto文件夹,编写.proto文件
dataformat.proto文件
syntax = "proto3"; package tutorial; message Person { string name = 1; int32 id = 2; string email = 3; string phone = 4; map<string, string> pairs = 5; }
编译项目,会在build文件夹下proto文件夹内生成对应的java文件
下面就可以愉快的玩耍了
Activity中代码,里面是使用okhttp请求网络,接收到protobuf的byte字节转成java对象
package com.example.rxjavademo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.functions.Consumer; import io.reactivex.functions.Function; import io.reactivex.schedulers.Schedulers; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; import tutorial.Dataformat.Person; public class ProtocalActivity extends AppCompatActivity { private OkHttpClient okHttpClient = new OkHttpClient(); private Observable observable = Observable.just("http://elyeproject.x10host.com/experiment/protobuf") .map(new Function<String, Person>() { @Override public Person apply(String url) throws Exception { Request request = new Request.Builder().url(url).build(); okhttp3.Call call = okHttpClient.newCall(request); Response response = call.execute(); if (response.isSuccessful()) { ResponseBody responseBody = response.body(); if (responseBody != null) { return Person.parseFrom(responseBody.byteStream()); } } return null; } }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_protocal); observable.subscribe(new Consumer() { @Override public void accept(Object object) throws Exception { showResult((Person) object); } }); } private void showResult(Person result) { TextView textView = (TextView) findViewById(R.id.txt_main); textView.setText(result.toString()); // result.writeTo(); // Person.Builder builder = Person.newBuilder(); // builder.putPairs(); } }
效果图