xref: /aosp_15_r20/external/grpc-grpc-java/documentation/android-channel-builder.md (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1*e07d83d3SAndroid Build Coastguard Worker# AndroidChannelBuilder
2*e07d83d3SAndroid Build Coastguard Worker
3*e07d83d3SAndroid Build Coastguard Worker The `grpc-android` package provides access to the
4*e07d83d3SAndroid Build Coastguard Worker`AndroidChannelBuilder` class. Given an Android Context, this builder
5*e07d83d3SAndroid Build Coastguard Workerregisters a network event listener upon channel construction.  The listener
6*e07d83d3SAndroid Build Coastguard Workerautomatically responds to changes in the device's network state, avoiding
7*e07d83d3SAndroid Build Coastguard Workerdelays and interrupted RPCs that may otherwise occur.
8*e07d83d3SAndroid Build Coastguard Worker
9*e07d83d3SAndroid Build Coastguard WorkerBy default, gRPC uses exponential backoff to recover from connection failures.
10*e07d83d3SAndroid Build Coastguard WorkerDepending on the scheduled backoff delay when the device regains connectivity,
11*e07d83d3SAndroid Build Coastguard Workerthis could result in a  one minute or longer delay before gRPC re-establishes
12*e07d83d3SAndroid Build Coastguard Workerthe connection. This delay is removed when `AndroidChannelBuilder` is provided
13*e07d83d3SAndroid Build Coastguard Workerwith the app's Android Context.  Notifications from the network listener
14*e07d83d3SAndroid Build Coastguard Workercause the channel to immediately reconnect upon network recovery.
15*e07d83d3SAndroid Build Coastguard Worker
16*e07d83d3SAndroid Build Coastguard WorkerOn Android API levels 24+, `AndroidChannelBuilder`'s network listener mechanism
17*e07d83d3SAndroid Build Coastguard Workerallows graceful switching from cellular to wifi connections. When an Android
18*e07d83d3SAndroid Build Coastguard Workerdevice on a cellular network connects to a wifi network, there is a brief
19*e07d83d3SAndroid Build Coastguard Worker(typically 30 second) interval when both cellular and wifi networks remain
20*e07d83d3SAndroid Build Coastguard Workeravailable, then any connections on the cellular network are terminated.  By
21*e07d83d3SAndroid Build Coastguard Workerlistening for changes in the device's default network, `AndroidChannelBuilder`
22*e07d83d3SAndroid Build Coastguard Workersends new RPCs via wifi rather than using an already-established cellular
23*e07d83d3SAndroid Build Coastguard Workerconnection. Without listening for pending network changes, new RPCs sent on an
24*e07d83d3SAndroid Build Coastguard Workeralready established cellular connection would fail when the device terminates
25*e07d83d3SAndroid Build Coastguard Workercellular connections.
26*e07d83d3SAndroid Build Coastguard Worker
27*e07d83d3SAndroid Build Coastguard Worker***Note:*** *Currently, `AndroidChannelBuilder` is only compatible with gRPC
28*e07d83d3SAndroid Build Coastguard WorkerOkHttp. We plan to offer additional Android-specific features compatible with
29*e07d83d3SAndroid Build Coastguard Workerboth the OkHttp and Cronet transports in the future, but the network listener
30*e07d83d3SAndroid Build Coastguard Workermechanism is only necessary with OkHttp; the Cronet library internally handles
31*e07d83d3SAndroid Build Coastguard Workerconnection management on Android devices.*
32*e07d83d3SAndroid Build Coastguard Worker
33*e07d83d3SAndroid Build Coastguard Worker## Example usage:
34*e07d83d3SAndroid Build Coastguard Worker
35*e07d83d3SAndroid Build Coastguard WorkerIn your `build.gradle` file, include a dependency on both `io.grpc:grpc-android` and
36*e07d83d3SAndroid Build Coastguard Worker`io.grpc:grpc-okhttp`.
37*e07d83d3SAndroid Build Coastguard Worker
38*e07d83d3SAndroid Build Coastguard WorkerYou also need permission to access the device's network state in your
39*e07d83d3SAndroid Build Coastguard Worker`AndroidManifest.xml`:
40*e07d83d3SAndroid Build Coastguard Worker
41*e07d83d3SAndroid Build Coastguard Worker```
42*e07d83d3SAndroid Build Coastguard Worker<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
43*e07d83d3SAndroid Build Coastguard Worker```
44*e07d83d3SAndroid Build Coastguard Worker
45*e07d83d3SAndroid Build Coastguard WorkerWhen constructing your channel, use `AndroidChannelBuilder` and provide it with
46*e07d83d3SAndroid Build Coastguard Workeryour app's Context:
47*e07d83d3SAndroid Build Coastguard Worker
48*e07d83d3SAndroid Build Coastguard Worker```
49*e07d83d3SAndroid Build Coastguard Workerimport io.grpc.android.AndroidChannelBuilder;
50*e07d83d3SAndroid Build Coastguard Worker...
51*e07d83d3SAndroid Build Coastguard WorkerManagedChannel channel = AndroidChannelBuilder.forAddress("localhost", 8080)
52*e07d83d3SAndroid Build Coastguard Worker    .context(getApplicationContext())
53*e07d83d3SAndroid Build Coastguard Worker    .build();
54*e07d83d3SAndroid Build Coastguard Worker```
55*e07d83d3SAndroid Build Coastguard Worker
56*e07d83d3SAndroid Build Coastguard WorkerYou continue to use the constructed channel exactly as you would any other
57*e07d83d3SAndroid Build Coastguard Workerchannel. gRPC will now monitor and respond to the device's network state
58*e07d83d3SAndroid Build Coastguard Workerautomatically. When you shutdown the managed channel, the network listener
59*e07d83d3SAndroid Build Coastguard Workerregistered by `AndroidChannelBuilder` will be unregistered.
60*e07d83d3SAndroid Build Coastguard Worker
61