Slide 1

Slide 1 text

gRPC on Android TakuSemba CyberAgent.Inc

Slide 2

Slide 2 text

@takusemba https://github.com/TakuSemba

Slide 3

Slide 3 text

Droidcon SF

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

What’s gRPC?

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

What’s Protocol Buffers?

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Let’s gRPC

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

message CoffeeResponse { int32 price = 1; string name = 2; string message = 3; } Response //coffee.proto

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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) {} }

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

->% 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

Slide 21

Slide 21 text

->% git clone [email protected]: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

Slide 22

Slide 22 text

->% 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

Slide 23

Slide 23 text

Generate Proto Files

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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 { it.onSuccess(stub.order(request)) }.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { /** do something */ }, { /** error handling */ } )

Slide 26

Slide 26 text

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 { it.onSuccess(stub.order(request)) }.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { /** do something */ }, { /** error handling */ } )

Slide 27

Slide 27 text

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 { it.onSuccess(stub.order(request)) }.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { /** do something */ }, { /** error handling */ } )

Slide 28

Slide 28 text

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 { it.onSuccess(stub.order(request)) }.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { /** do something */ }, { /** error handling */ } )

Slide 29

Slide 29 text

Demo

Slide 30

Slide 30 text

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