Pro Yearly is on sale from $80 to $50! »

gRPC on Android @DroidconSF Report

A374f41eab3f73c50d8bab0652cb207a?s=47 TakuSemba
November 20, 2017

gRPC on Android @DroidconSF Report

A374f41eab3f73c50d8bab0652cb207a?s=128

TakuSemba

November 20, 2017
Tweet

Transcript

  1. gRPC on Android TakuSemba CyberAgent.Inc

  2. @takusemba https://github.com/TakuSemba

  3. Droidcon SF

  4. None
  5. What’s gRPC?

  6. gRPC ɾProtocol Buffers ɾRemote Procedure Call ɾHTTP/2

  7. ɾProtocol Buffers ɾRemote Procedure Call ɾHTTP/2 gRPC

  8. ɾProtocol Buffers ɾRemote Procedure Call ɾHTTP/2 gRPC

  9. What’s Protocol Buffers?

  10. ɾBinary based transmission Protocol Buffers ɾAvailable for many languages ɾFaster

    than Json
  11. ɾBinary based transmission ɾAvailable for many languages ɾFaster than Json

    Protocol Buffers
  12. ɾBinary based transmission ɾAvailable for many languages ɾFaster than Json

    Protocol Buffers
  13. Protocol BuffersͰͷ௨৴͸jsonͱൺ΂ͯૣ͍ͷ͔ IUUQTRJJUBDPNUBLVTFNCBJUFNTGFBCEBEDFEEC

  14. Let’s gRPC

  15. message CoffeeRequest { string name = 1; } Request //coffee.proto

  16. message CoffeeResponse { int32 price = 1; string name =

    2; string message = 3; } Response //coffee.proto
  17. service Coffee { rpc Order (CoffeeRequest) returns (CoffeeResponse) {} }

    Service //coffee.proto
  18. Service service Coffee { rpc Order (CoffeeRequest) returns (CoffeeResponse) {}

    } service Coffee { rpc Order (stream CoffeeRequest) returns (CoffeeResponse) {} } service Coffee { rpc Order (CoffeeRequest) returns (stream CoffeeResponse) {} } service Coffee { rpc Order (stream CoffeeRequest) returns (stream CoffeeResponse) {} }
  19. syntax = "proto3"; option java_package = "com.takusemba.grpc.android.protos"; option go_package =

    "protos"; package Coffee; message CoffeeRequest { string name = 1; } message CoffeeResult { string message = 1; } service Coffee { rpc Order (CoffeeRequest) returns (CoffeeResult) {} } //coffee.proto
  20. ->% curl -OL https://github.com/google/protobuf/releases/download/v3.5.0/ protobuf-java-3.5.0.tar.gz ->% tar -zxvf protobuf-java-3.5.0.tar.gz ->%

    cd protobuf-3.5.0 ->% ./configure ->% make ->% sudo make install ->% protoc —version // -> libprotoc 3.5.0 :) //protoc
  21. ->% git clone git@github.com:grpc/grpc-java.git ->% cd grpc-java ->% git checkout

    -b v1.7.0 ->% cd compiler ->% make ->% ../gradlew java_pluginExecutable ->% cp build/exe/java_plugin/protoc-gen-grpc-java /usr/local/bin //protoc-gen-grpc-java
  22. ->% protoc coffee.proto --grpc-java_out=lite:. --java_out=. --plugin=protoc-gen- grpc-java=/usr/local/bin/protoc-gen-grpc-java //generate proto files

    for client ->% protoc --go_out=plugins=grpc:. coffee.proto //generate proto files for server Generate Proto Files
  23. Generate Proto Files

  24. gRPC on Android classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.2' implementation "io.grpc:grpc-okhttp:1.7.0" implementation "io.grpc:grpc-protobuf:1.7.0" implementation

    "io.grpc:grpc-stub:1.7.0" implementation "javax.annotation:javax.annotation-api:1.2" //build.gradle //app/build.gradle
  25. gRPC on Android val channel = ManagedChannelBuilder.forAddress("10.0.2.2", 8080) .usePlaintext(true) .build()

    val stub = CoffeeGrpc.newBlockingStub(channel) val request = CoffeeOuterClass.CoffeeRequest.newBuilder() .setName(“hot coffee") .build() Single .create<CoffeeOuterClass.CoffeeResponse> { it.onSuccess(stub.order(request)) }.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { /** do something */ }, { /** error handling */ } )
  26. gRPC on Android val channel = ManagedChannelBuilder.forAddress("10.0.2.2", 8080) .usePlaintext(true) .build()

    val stub = CoffeeGrpc.newBlockingStub(channel) val request = CoffeeOuterClass.CoffeeRequest.newBuilder() .setName(“hot coffee") .build() Single .create<CoffeeOuterClass.CoffeeResponse> { it.onSuccess(stub.order(request)) }.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { /** do something */ }, { /** error handling */ } )
  27. gRPC on Android val channel = ManagedChannelBuilder.forAddress("10.0.2.2", 8080) .usePlaintext(true) .build()

    val stub = CoffeeGrpc.newBlockingStub(channel) val request = CoffeeOuterClass.CoffeeRequest.newBuilder() .setName(“hot coffee") .build() Single .create<CoffeeOuterClass.CoffeeResponse> { it.onSuccess(stub.order(request)) }.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { /** do something */ }, { /** error handling */ } )
  28. gRPC on Android val channel = ManagedChannelBuilder.forAddress("10.0.2.2", 8080) .usePlaintext(true) .build()

    val stub = CoffeeGrpc.newBlockingStub(channel) val request = CoffeeOuterClass.CoffeeRequest.newBuilder() .setName(“hot coffee") .build() Single .create<CoffeeOuterClass.CoffeeResponse> { it.onSuccess(stub.order(request)) }.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { /** do something */ }, { /** error handling */ } )
  29. Demo

  30. gRPC on Android https://github.com/takusemba https://twitter.com/takusemba