gRPC は、HTTP/2 での全二重双方向ストリーミングのサポートを最初から考慮して設計されています。ストリーミングでは、大量の情報のアップロードやダウンロードが必要なオペレーションなど、リクエストとレスポンスのサイズを任意に指定できます。ストリーミングでは、クライアントとサーバーはメッセージの読み取りと書き込みを同時に行い、リソース ID をトラッキングすることなく相互にサブスクライブできます。これにより、アプリの実装の柔軟性が向上します。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-27 UTC。"],[],[],null,["# Build client-server applications with gRPC\n\ngRPC is a modern, open-source, high-performance RPC framework that can run in any\nenvironment. It can efficiently connect services in and across data centers with\npluggable support for load balancing, tracing, health-checking, and\nauthentication. It is also applicable in the last mile of distributed computing\nto connect devices, mobile applications, and browsers to backend services. You\ncan find documentation on gRPC's official website and get support from\nopen source communities. This guide points you to solutions for\nbuilding Android apps using gRPC.\n\n[grpc.io](https://grpc.io) is the official website for the\ngRPC project. To learn more about how gRPC works, see [What is gRPC?](https://grpc.io/docs/guides/)\nand [gRPC Concepts](https://grpc.io/docs/guides/concepts/).\nTo learn how to use gRPC in an Android app, see the Hello World example in\n[gRPC Android Java Quickstart](https://grpc.io/docs/quickstart/android/).\nYou can also find several other gRPC Android examples [on GitHub](https://github.com/grpc/grpc-java/tree/v1.24.0/examples/android).\n\nFeatures\n--------\n\n**Procedure call makes it simple**\n: Because it's RPC, the programming model is procedure calls: the networking\n aspect of the technology is abstracted away from application code, making it\n look almost as if it was a normal in-process function call. Your client-server\n interaction will not be constrained by the semantics of HTTP resource methods\n (such as GET, PUT, POST, and DELETE). Compared to REST APIs, your implementation\n looks more natural, without the need for handling HTTP protocol metadata.\n\n**Efficient network transmission with HTTP/2**\n: Transmitting data from mobile devices to a backend server can be a very\n resource-intensive process. Using the standard HTTP/1.1 protocol, frequent\n connections from a mobile device to a cloud service can drain the battery,\n increase latency, and block other apps from connecting. By default, gRPC runs on\n top of HTTP/2, which introduces bi-directional streaming, flow control, header\n compression, and the ability to multiplex requests over a single TCP/IP\n connection. The result is that gRPC can reduce resource usage, resulting in\n lower response times between your app and services running in the cloud,\n reduced network usage, and longer battery life for client running on mobile\n devices.\n\nBuilt-in streaming data exchange support\n: gRPC was designed with HTTP/2's support for full-duplex bidirectional\n streaming in mind from the outset. Streaming allows a request and response to\n have an arbitrarily large size, such as operations that require uploading or\n downloading a large amount of information. With streaming, client and server\n can read and write messages simultaneously and subscribe to each other without\n tracking resource IDs. This makes your app implementation more flexible.\n\n**Seamless integration with Protocol Buffer**\n: gRPC uses Protocol Buffers (Protobuf) as its\n serialization/deserialization method with optimized-for-Android codegen plugin\n ([Protobuf Java Lite](https://github.com/protocolbuffers/protobuf/blob/v3.9.0/java/lite.md)).\n Compared to text-based format (such as JSON), Protobuf offers more efficient data\n exchanging in terms of marshaling speed and code size, which makes it more\n suitable to be used in mobile environments. Also Protobuf's concise\n message/service definition syntax makes it much easier to define data model\n and application protocols for your app.\n\nUsage overview\n--------------\n\nFollowing the [gRPC Basics - Android Java](https://grpc.io/docs/tutorials/basic/android/)\ntutorial, using gRPC for Android apps involves four steps:\n\n- Define RPC services with protocol buffers and generate the gRPC client interfaces.\n- Build a Channel that serves as the medium for RPC calls between client and server.\n- Create a client Stub as the entry point for initiating RPC calls from client side.\n- Make RPC calls to remote server as you would when performing local procedure calls.\n\nFor demonstration purposes, bytes are transmitted in plain text in the provided\nexample. However, your app should always encrypt network data in production.\ngRPC provides SSL/TLS encryption support as well as OAuth token exchanging\n(OAuth2 with Google services) for authentication. For more details, see\n[TLS on Android](https://github.com/grpc/grpc-java/blob/v1.24.0/SECURITY.md#tls-on-android)\nand [Using OAuth2](https://github.com/grpc/grpc-java/blob/v1.24.0/SECURITY.md#using-oauth2).\n| **Note:** If you are using Gradle as the build tool for your app, the Protobuf Gradle plugin is a handy tool for automating the process of generating and building gRPC Java code into your app. For more information, see [Protobuf Plugin for Gradle](https://github.com/google/protobuf-gradle-plugin#protobuf-plugin-for-gradle-).\n\nTransport\n---------\n\ngRPC provides two choices of Transport implementations for Android clients:\nOkHttp and Cronet.\n| **Note:** Creating channels with transport-specific channel builders (such as [`OkHttpChannelBuilder`](https://github.com/grpc/grpc-java/blob/v1.24.0/okhttp/src/main/java/io/grpc/okhttp/OkHttpChannelBuilder.java#L57) or [`CronetChannelBuilder`](https://github.com/grpc/grpc-java/blob/v1.24.0/cronet/src/main/java/io/grpc/cronet/CronetChannelBuilder.java#L49)) is for more advanced usage. If you build the channel with [`ManagedChannelBuilder`](https://github.com/grpc/grpc-java/blob/v1.24.0/api/src/main/java/io/grpc/ManagedChannelBuilder.java#L31), the class loader will load the plain [`OkHttpChannelBuilder`](https://github.com/grpc/grpc-java/blob/v1.24.0/okhttp/src/main/java/io/grpc/okhttp/OkHttpChannelBuilder.java#L57) as the default implementation at runtime. Starting from gRPC's 1.24 release, we recommend using [`AndroidChannelBuilder`](https://github.com/grpc/grpc-java/blob/v1.24.0/android/src/main/java/io/grpc/android/AndroidChannelBuilder.java#L54), which is similar, but with some Android-specific optimizations.\n\n**Choose a transport (advanced)**\n\n-\n\n OkHttp\n : OkHttp is a light-weight networking stack designed for use on mobile. It is\n gRPC's default transport for running in Android environment. To use OkHttp as\n gRPC transport for your app, construct the channel with [`AndroidChannelBuilder`](https://github.com/grpc/grpc-java/blob/v1.24.0/android/src/main/java/io/grpc/android/AndroidChannelBuilder.java#L54),\n which wraps [`OkHttpChannelBuilder`](https://github.com/grpc/grpc-java/blob/v1.24.0/okhttp/src/main/java/io/grpc/okhttp/OkHttpChannelBuilder.java#L57)\n and will register a network monitor with the Android OS to quickly respond to\n network changes. An example usage can be found in [gRPC-Java AndroidChannelBuilder](https://github.com/grpc/grpc-java/blob/v1.24.0/documentation/android-channel-builder.md#example-usage).\n-\n\n Cronet (experimental)\n : Cronet is Chromium's Networking stack packaged as a library for mobile. It\n offers robust networking support with state-of-the-art QUIC protocol, which can\n be especially effective in unreliable network environments. To learn more\n details about Cronet, see [Perform network operations using Cronet](/guide/topics/connectivity/cronet).\n To use Cronet as gRPC transport for your app, construct the channel with\n [`CronetChannelBuilder`](https://github.com/grpc/grpc-java/blob/v1.24.0/cronet/src/main/java/io/grpc/cronet/CronetChannelBuilder.java#L49).\n An example usage is provided in [gRPC-Java Cronet Transport](https://github.com/grpc/grpc-java/tree/v1.24.0/cronet#grpc-cronet-transport).\n\n| **Note:** Cronet's bidirectional streaming feature is still experimental. Therefore, [`CronetChannelBuilder`](https://github.com/grpc/grpc-java/blob/v1.24.0/cronet/src/main/java/io/grpc/cronet/CronetChannelBuilder.java#L49) is marked as an experimental API.\n\nGenerally speaking, we recommend apps targeting recent SDK versions use Cronet\nas it offers a more-powerful network stack. The downside of using Cronet is the\nAPK size increase, as adding the binary Cronet dependency will add \\\u003e1MB to the\napp size, versus \\~100KB for OkHttp. Starting with GMSCore v.10, an\nup-to-date copy of Cronet can be loaded from Google Play Services. The APK size\nmay no longer be a concern, although devices without the latest GMSCore\ninstalled may still prefer using OkHttp.\n| **Note:** When loading Cronet from Google Play services, call [`CronetProviderInstaller.installProvider(Context)`](https://developers.google.com/android/reference/com/google/android/gms/net/CronetProviderInstaller) before creating [`CronetEngine/ExperimentalCronetEngine`](https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/CronetEngine) objects to prevent unexpected exceptions from being thrown due to errors like devices requiring an updated version of Google Play services."]]