experience protocol buffer on android

19
Experience Protocol Buffer on Android

Upload: richard-chang

Post on 13-Apr-2017

672 views

Category:

Software


2 download

TRANSCRIPT

ExperienceProtocol Buffer

on Android

HELLO!

I am Richard ChangAndroid EngineerWorked for HTC, Movial, VMFive and AndroVideoYou can find me via chiel99 AT gmail.com

Outline● Introduce Protocol Buffers● Why Protocol Buffers● How to use Protocol Buffers● Protocol buffer meets Android● Demo● Reference

https://github.com/chiel99/ExperienceProtobuf

What areProtocol Buffers?

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing

structured data

From: https://developers.google.com/protocol-buffers/

Serialize Deserialize

WhyProtocol Buffers?

Benefits

● Speed○ Fast

● Size○ Small○ Serialize to binary-format stream

● Backward Compatible● Multiple programming language support

○ C++ / Java / Python / C# / Go...etc● Easy to define the data schema

How to use protocol buffers?

Usages

● Install protoc (protocol buffer compiler) and related libs○ steps

● Write a .proto file● Use protoc to generate the Java/C++/Python... source code

○ protoc --cpp_out=./cpp --java_out=./java sample.proto● Add related libs to your project● Put the generated source code to your project● writeTo() and parseFrom()● Happy coding with Protocol Buffers!

Sample

Protocol Buffers meet Android

● AOSP/external/protobuf/○ protoc => aprotoc○ AOSP is not following the latest version

● Dex 65535 method count issue ● Apk size issue

Protocol Buffers for Java

Lite version Micro version Nano version

Lib size 128 KB 21 KB 43 KB

Generated Java class size* 7~8x 1.2~1.3x 1x

Object creation Builder Constructor Constructor

Object operations

Builder /Setter / Getter Setter / Getter public member

Repeated objects List List Array

Demo

Speed and Size comparison with GSON

● Speed○ Serialize: ~10x faster (40 ms v.s 400 ms)○ Deserialize: ~2x faster (15 ms v.s 30 ms)

● Serialized data size○ ~2x smaller (24 KB v.s 45 KB)

● Lib size○ ~10x smaller (21 KB v.s 232 KB)

Drawbacks

● Human unreadable format● Need tool (protoc) to generate source code● Most network services are JSON based?

Thanks!Any questions?

You can find the sample code on github:https://github.com/chiel99/ExperienceProtobuf

Reference

● https://developers.google.com/protocol-buffers/● https://github.com/android/platform_external_protobuf ● AOSP: frameworks/native/opengl/libs/GLES_trace● https://github.com/square/wire ● https://github.com/Biuni/PokemonGO-Pokedex