This guide gets you started with gRPC in Android Java with a simple working example.
- JDK version 7 or higher
- Android SDK, API level 14 or higher
- An android device set up for or anAndroid Virtual Device
Note
gRPC Java does not support running a server on an Android device. For this quick start, the Android client app will connect to a server running on your local (non-Android) computer.
Download the example
You’ll need a local copy of the example code to work through this quick start.Download the example code from our GitHub repository (the following commandclones the entire repository, but you just need the examples for this quick startand other tutorials):
- Compile the server:
- Run the server:
$ ./build/install/examples/bin/hello-world-server
- From another terminal, compile and run the client:
Congratulations! You’ve just run a client-server application with gRPC.
Update a gRPC service
// The greeting service definition.
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
}
Let’s update this so that the Greeter
service has two methods. Editsrc/main/proto/helloworld.proto
and update it with a new SayHelloAgain
method, with the same request and response types:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
// Sends another greeting
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
string message = 1;
}
Remember to save the file!
When we recompile the example, normal compilation will regenerateGreeterGrpc.java
, which contains our generated gRPC client and server classes.This also regenerates classes for populating, serializing, and retrieving ourrequest and response types.
However, we still need to implement and call the new method in the human-writtenparts of our example application.
See the.
Just like we did before, from the examples
directory:
- Compile the server
$ ./gradlew installDist
- Run the server:
$ ./build/install/examples/bin/hello-world-server
- From another terminal, compile and install the client to your device
To run the application on a physical device via USB debugging, you mustconfigure USB port forwarding to allow the device to communicate with the serverrunning on your computer. This is done via the adb
command line tool asfollows:
adb reverse tcp:8080 tcp:50051
This sets up port forwarding from port 8080
on the device to port 50051
onthe connected computer, which is the port that the Hello World server islistening on.
Now you can run the Android Hello World app on your device, using localhost
and 8080
as the Host
and Port
.
To run the Hello World app on an Android Virtual Device, you don’t need toenable port forwarding. Instead, the emulator can use the IP address10.0.2.2
to refer to the host machine. Inside the Android Hello World app,enter 10.0.2.2
and 50051
as the and Port
.
What’s next
- Read a full explanation of how gRPC works inandgRPC Concepts.
- Work through a more detailed tutorial in.
- Explore the gRPC Java core API in itsreferencedocumentation.