Androidã§gRPCã¯ã©ã¤ã¢ã³ãã®å®è£ ããããã¨æã£ã¦ãã¥ã¼ããªã¢ã«èªãã ãã©ããã£ã¨åãããªãã£ãã®ã§ã¡ã¢ãã¾ãã¯ãGoã§ç°¡åãªgRPCãµã¼ãã¼ã¨ã¯ã©ã¤ã¢ã³ããå®è£ ããã®å¾ã¯ã©ã¤ã¢ã³ããAndrodã¢ããªã¨ãã¦å®è£ ãããã¨ããã®ããã£ã¦ã¿ã¾ããã
å®è£ ãã¦ã¿ãã½ã¼ã¹ã³ã¼ãã¯ããã§ãâ
gRPCãã©ããªãã®ãªã®ã触ã
ã¾ããGoã§å®è£ ãã¦ã¿ãã以ä¸ã®è¨äºãé²ããããã£ãã§ãããããã¨ããããã¾ãã
ãã®è¨äºã®éãã«é²ããã
- protoãã¡ã¤ã«ã§ã¯ã©ã¤ã¢ã³ãã»ãµã¼ãã¼éã®ã¤ã³ã¿ãã§ã¼ã¹ãå®ç¾©ãã
- protocã³ãã³ãã§protoãã¡ã¤ã«ããã¯ã©ã¤ã¢ã³ãã¨ãµã¼ãã¼ã®éä¿¡é¨åã«ã¤ãã¦ã³ã¼ãèªåçæãã
- èªåçæããã³ã¼ããå©ç¨ãããµã¼ãã¼ã¨ã¯ã©ã¤ã¢ã³ããå®è£ ãåããã¦é°å²æ°ãæ´ã
ã¯ã©ã¤ã¢ã³ããAndroidã¢ããªã§å®è£ ãã
ä¸è¨ã®Goã§å®è£ ããã¯ã©ã¤ã¢ã³ãã¯CLIãæã¤ããã°ã©ã ããã®ã¯ã©ã¤ã¢ã³ããAndroidã¢ããªã«ãã¦ã¿ãã
gRPCå ¬å¼ã®Quick start
ã¾ãã¯gRPCã®å ¬å¼ããã¥ã¡ã³ãã®Androidã®ãã¼ã¸ãè¦ã«è¡ãã
Quick start | Kotlin for Android | gRPC
以ä¸ã®ããã¸ã§ã¯ãã使ã£ã¦èª¬æããã¦ããã確èªããã¨ãã®ææ°ã®tag㯠v1.3.0
ã
grpc-kotlin/examples at v1.3.0 · grpc/grpc-kotlin
ãã®ããã¸ã§ã¯ãã¯gradleã§æ§æããã¦ãããè²ã ãã£ã¦ã©ããè¦ãã°ãããããã«åãããªãã£ããã©ãAndroidã¢ããªã§gRPCã¯ã©ã¤ã¢ã³ãã«ã¤ãã¦ã¯3ã¤ã®ãã£ã¬ã¯ããªï¼Gradleã®ã¢ã¸ã¥ã¼ã«ï¼ã ããè¦ãã°è¯ãã
examples/protos
ã¯protoãã¡ã¤ã«ãç½®ãã¦ããã¢ã¸ã¥ã¼ã«ã examples/android
ã¯gRPCã¯ã©ã¤ã¢ã³ãã¨ãªãAndroidã¢ããªã®ã¢ã¸ã¥ã¼ã«ã
ã§ã¯ example/stub-android
ã¯ï¼
stub
example/stub-android
ã¯ãprotoãã¡ã¤ã«ããã¯ã©ã¤ã¢ã³ãã³ã¼ããèªåçæããããã®ã¢ã¸ã¥ã¼ã«ãçæãããã¯ã©ã¤ã¢ã³ãã³ã¼ããstubã¨å¼ã¶ãã¯ã©ã¤ã¢ã³ãã¨ãªãAndroidã¢ããªã®ã½ã¼ã¹ã³ã¼ãããstubã®ã¡ã½ãããæ®éã®ã¡ã½ããã¨ãã¦å¼ã³åºãããstubãæä¾ãã¦ããã¡ã½ããã®å®æ
ã¯ãµã¼ãã¼å´ã§å®è£
ããã¦ãããã¤ã¾ããRPCã¨ãªãã
ã¦ããããã¹ãã§ä»®ã®å®è£ ã¨ãã¦ã¹ã¿ãã¨ããè¨èã使ããã¨ããããã©ãä»®ã®å®è£ ã¨ããããã§ã¯ãªãã
stubã«ã¤ãã¦ãgRPCå ¬å¼ã§è»½ã触ãããã¦ããã
Androidã¢ããªã¤ãã
ä¸è¨ã®exampleãåèã«ä½ã£ã¦ã¿ãã
project_root/app
ãAndroidã¢ããªã feature/grpc-client
ãµãã¢ã¸ã¥ã¼ã«ãgRPCã¯ã©ã¤ã¢ã³ãã®ã½ã¼ã¹ã³ã¼ãã§ãå
¬å¼exampleã® android
ã«ç¸å½ããã
ãã®Androidã¢ããªã¯ client_stub
ã¢ã¸ã¥ã¼ã«ã«ä¾åãããä»ã«ãgRPCé¢é£ã®ã©ã¤ãã©ãªã«ãä¾åããã
dependencies { implementation(project(path: ":client_stub")) ... // gRPC implementation("io.grpc:grpc-kotlin-stub:$grpc_kotlin_version") implementation("com.google.protobuf:protobuf-kotlin-lite:$protobuf_version") implementation("io.grpc:grpc-android:$grpc_android_version") implementation("io.grpc:grpc-okhttp:$grpc_android_version") }
feature/grpc-client/build.gradle
client_stub
ã¯protoãã¡ã¤ã«ããã³ã¼ãèªåçæããã¢ã¸ã¥ã¼ã«ãAndroid Libraryã¢ã¸ã¥ã¼ã«ã¨ããã
å
¬å¼exampleã® build.gradle
ãåèã«è²ã
ããã£ã¦ã¿ãã
protobuf { protoc { artifact = "com.google.protobuf:protoc:$protobuf_version" } plugins { java { artifact = "io.grpc:protoc-gen-grpc-java:$grpc_version" } grpc { artifact = "io.grpc:protoc-gen-grpc-java:$grpc_version" } grpckt { artifact = "io.grpc:protoc-gen-grpc-kotlin:$grpc_kotlin_version:jdk7@jar" } } ... }
以ä¸ã®3ã¤ããããã¨protocã³ãã³ãã¨ã®å¯¾å¿ãã¤ã¡ã¼ã¸ããã¨ææ¡ãããããJavaå®è£ ã®stubãèªåçæããªãã¨ãããªãã¨ããã®ãæåã¨ã£ã¤ãã¥ããã£ãã
com.google.protobuf:protoc
protoc
ã³ãã³ãèªä½
io.grpc:protoc-gen-grpc-java
- Javaã§å®è£ ãããstubãçæ
- Goã®ä¾ã§è¨ãã¨
protoc
ã³ãã³ãã®ãªãã·ã§ã³--go-grpc_out
ã--go-grpc_opt
io.grpc:protoc-gen-grpc-kotlin
- Kotlinã§å®è£ ãããä¸è¨ã®Javaå®è£ stubã®ã©ããã¼
protocããèªåçæããstubãAndroid Libraryã¢ã¸ã¥ã¼ã«ã¨ãã¦ã project_root/app
ããåç
§ã§ããã°ããã¨ã¯å
¬å¼exampleã®androidãè¦ãªããå®è£
ããã°è¯ãã
ããã¾ã
protocã³ãã³ãã§çæã§ããã¨ã¨ã£ã¤ããããã®ã§ãããAndroid Libraryã¢ã¸ã¥ã¼ã«ã«ãããå ´åã¯Gradleã§ãããã¨æ¥½ã§ããããä»å㯠client_stub
ãåä¸ããã¸ã§ã¯ãã«ãã¾ããããå¥ãªãã¸ããªã¨ãã¦aarãpublishããã®ãè¯ãããã§ãã