xref: /aosp_15_r20/external/grpc-grpc-java/cronet/README.md (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1*e07d83d3SAndroid Build Coastguard WorkergRPC Cronet Transport
2*e07d83d3SAndroid Build Coastguard Worker========================
3*e07d83d3SAndroid Build Coastguard Worker
4*e07d83d3SAndroid Build Coastguard Worker**EXPERIMENTAL:**  *gRPC's Cronet transport is an experimental API. Its stability
5*e07d83d3SAndroid Build Coastguard Workerdepends on upstream Cronet's implementation, which involves some experimental features.*
6*e07d83d3SAndroid Build Coastguard Worker
7*e07d83d3SAndroid Build Coastguard WorkerThis code enables using the [Chromium networking stack
8*e07d83d3SAndroid Build Coastguard Worker(Cronet)](https://chromium.googlesource.com/chromium/src/+/master/components/cronet)
9*e07d83d3SAndroid Build Coastguard Workeras the transport layer for gRPC on Android. This lets your Android app make
10*e07d83d3SAndroid Build Coastguard WorkerRPCs using the same networking stack as used in the Chrome browser.
11*e07d83d3SAndroid Build Coastguard Worker
12*e07d83d3SAndroid Build Coastguard WorkerSome advantages of using Cronet with gRPC:
13*e07d83d3SAndroid Build Coastguard Worker
14*e07d83d3SAndroid Build Coastguard Worker* Bundles an OpenSSL implementation, enabling TLS connections even on older
15*e07d83d3SAndroid Build Coastguard Worker  versions of Android without additional configuration
16*e07d83d3SAndroid Build Coastguard Worker* Robust to Android network connectivity changes
17*e07d83d3SAndroid Build Coastguard Worker* Support for [QUIC](https://www.chromium.org/quic)
18*e07d83d3SAndroid Build Coastguard Worker
19*e07d83d3SAndroid Build Coastguard WorkerSince gRPC's 1.24 release, the `grpc-cronet` package provides access to the
20*e07d83d3SAndroid Build Coastguard Worker`CronetChannelBuilder` class. Cronet jars are available on Google's Maven repository.
21*e07d83d3SAndroid Build Coastguard WorkerSee the example app at https://github.com/GoogleChrome/cronet-sample/blob/master/README.md.
22*e07d83d3SAndroid Build Coastguard Worker
23*e07d83d3SAndroid Build Coastguard Worker## Example usage:
24*e07d83d3SAndroid Build Coastguard Worker
25*e07d83d3SAndroid Build Coastguard WorkerIn your app module's `build.gradle` file, include a dependency on both
26*e07d83d3SAndroid Build Coastguard Worker`io.grpc:grpc-cronet` and the Google Play Services Client Library for Cronet,
27*e07d83d3SAndroid Build Coastguard Worker`com.google.android.gms:play-services-cronet`.
28*e07d83d3SAndroid Build Coastguard Worker
29*e07d83d3SAndroid Build Coastguard WorkerIn cases where Cronet cannot be loaded from Google Play services, there is a less performant
30*e07d83d3SAndroid Build Coastguard Workerimplementation of Cronet's API that can be used. Depend on `org.chromium.net:cronet-fallback`
31*e07d83d3SAndroid Build Coastguard Workerto use this fall-back implementation.
32*e07d83d3SAndroid Build Coastguard Worker
33*e07d83d3SAndroid Build Coastguard Worker
34*e07d83d3SAndroid Build Coastguard WorkerYou will also need permission to access the device's network state in your
35*e07d83d3SAndroid Build Coastguard Worker`AndroidManifest.xml`:
36*e07d83d3SAndroid Build Coastguard Worker
37*e07d83d3SAndroid Build Coastguard Worker```
38*e07d83d3SAndroid Build Coastguard Worker<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
39*e07d83d3SAndroid Build Coastguard Worker```
40*e07d83d3SAndroid Build Coastguard Worker
41*e07d83d3SAndroid Build Coastguard WorkerOnce the above steps are completed, you can create a gRPC Cronet channel as
42*e07d83d3SAndroid Build Coastguard Workerfollows:
43*e07d83d3SAndroid Build Coastguard Worker
44*e07d83d3SAndroid Build Coastguard Worker```
45*e07d83d3SAndroid Build Coastguard Workerimport io.grpc.cronet.CronetChannelBuilder;
46*e07d83d3SAndroid Build Coastguard Workerimport org.chromium.net.ExperimentalCronetEngine;
47*e07d83d3SAndroid Build Coastguard Worker
48*e07d83d3SAndroid Build Coastguard Worker...
49*e07d83d3SAndroid Build Coastguard Worker
50*e07d83d3SAndroid Build Coastguard WorkerExperimentalCronetEngine engine =
51*e07d83d3SAndroid Build Coastguard Worker    new ExperimentalCronetEngine.Builder(context /* Android Context */).build();
52*e07d83d3SAndroid Build Coastguard WorkerManagedChannel channel = CronetChannelBuilder.forAddress("localhost", 8080, engine).build();
53*e07d83d3SAndroid Build Coastguard Worker```
54*e07d83d3SAndroid Build Coastguard Worker
55