Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using protobuf in your android app

Using protobuf in your android app

How to setup your Android app to use Protobuf with Kotlin flavor and ignoring Protobuf pros and cons!

Mohsen Mirhoseini

April 25, 2018
Tweet

Other Decks in Technology

Transcript

  1. @mohsenoid In a single Text Message: 160 Char W14(3)|Type*(1)|#ofGoals(128)|UserPoint(4)|Qualified(16)|Error**(1)|Checksum(1) *Type:

    RESULT("R") / PREDICT("P") / STATES("S") ** Error: User last predict message error call back
  2. @mohsenoid * MTU: Maximum Transmission Unit * Package/Sec: 7.5~1.25 milliseconds

    https://punchthrough.com/blog/posts/maximizing-ble-throughput-on-ios-and-android BLE Data Throughput Best case MTU Worst case MTU 512 Bytes 20 Bytes!!!
  3. @mohsenoid Google developed Protocol Buffers for internal usage and also

    provided a code generator for multiple languages. https://github.com/google/protobuf C++ / Java / Python / Objective-C / C# / JavaNano / JavaScript / Ruby / Go / PHP / Dart
  4. @mohsenoid • Protocol Buffers is a method of serializing structured

    data • Useful in developing programs to communicate with each other and also good for storing data • interface description language: describes the structure of some data • a program that generates source code from description for generating or parsing a stream of bytes that represents the structured data What is Protocol Buffers?
  5. @mohsenoid top-level build.gradle buildscript { ext.protobufVersion = '0.8.3' ... dependencies

    { ... classpath "com.google.protobuf:protobuf-gradle-plugin:$protobufVersion" } ...
  6. @mohsenoid Java module build.gradle apply plugin: 'com.google.protobuf' … // lite

    version is recommended for Android apps implementation 'com.google.protobuf:protobuf-lite:3.0.0' ...
  7. @mohsenoid Protobuf plugin settings protobuf { protoc { // setting

    proto compiler artifact = 'com.google.protobuf:protoc:3.0.0' } plugins { // ask proto plugin to use java lite code generator javalite { artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0' } }
  8. @mohsenoid Protobuf plugin settings generateProtoTasks { all().each { task ->

    task.builtins { remove java // because of using javalite we should remove java outputs } task.plugins { javalite {} // and use javalite } }
  9. @mohsenoid a sample proto file syntax = "proto2"; package tutorial;

    option java_package = "com.mohsenoid.tutorial"; option java_outer_classname = "AddressBookProtos";
  10. @mohsenoid message Person { required string name = 1; required

    int32 id = 2; optional string email = 3; message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phones = 4;
  11. @mohsenoid enum PhoneType { MOBILE = 0; HOME = 1;

    WORK = 2; } } message AddressBook { repeated Person people = 1; }
  12. @mohsenoid Protocol buffer is a useful way of serializing and

    deserializing data (also obfuscating!!!) perfectly optimized in case of data transmission size and also avoiding all manual data typing