KEMBAR78
Experience protocol buffer on android | PDF
Experience
Protocol Buffer
on Android
HELLO!
I am Richard Chang
Android Engineer
Worked for HTC, Movial, VMFive and AndroVideo
You 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 are
Protocol 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
Why
Protocol 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

Experience protocol buffer on android

  • 1.
  • 2.
    HELLO! I am RichardChang Android Engineer Worked for HTC, Movial, VMFive and AndroVideo You can find me via chiel99 AT gmail.com
  • 3.
    Outline ● Introduce ProtocolBuffers ● Why Protocol Buffers ● How to use Protocol Buffers ● Protocol buffer meets Android ● Demo ● Reference https://github.com/chiel99/ExperienceProtobuf
  • 4.
  • 5.
    Protocol buffers areGoogle's language-neutral, platform-neutral, extensible mechanism for serializing structured data From: https://developers.google.com/protocol-buffers/
  • 6.
  • 8.
  • 9.
    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
  • 10.
  • 11.
    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!
  • 12.
  • 13.
    Protocol Buffers meetAndroid ● AOSP/external/protobuf/ ○ protoc => aprotoc ○ AOSP is not following the latest version ● Dex 65535 method count issue ● Apk size issue
  • 14.
    Protocol Buffers forJava 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
  • 15.
  • 16.
    Speed and Sizecomparison 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)
  • 17.
    Drawbacks ● Human unreadableformat ● Need tool (protoc) to generate source code ● Most network services are JSON based?
  • 18.
    Thanks! Any questions? You canfind the sample code on github: https://github.com/chiel99/ExperienceProtobuf
  • 19.
    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