Android Java Quickstart

This guide gets you started with gRPC in Android Java with a simple working example.

Prerequisites

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):

  1. # Clone the repository at the latest release to get the example code:
  2. $ git clone -b v1.28.1 https://github.com/grpc/grpc-java
  3. # Navigate to the Java examples:
  4. $ cd grpc-java/examples

Run a gRPC application

  • Compile the server:
  1. $ ./gradlew installDist
  • Run the server:
  1. $ ./build/install/examples/bin/hello-world-server
  • From another terminal, compile and run the client:
  1. $ cd android/helloworld
  2. $ ../../gradlew installDebug

Congratulations! You’ve just run a client-server application with gRPC.

Update a gRPC service

Now let’s look at how to update the application with an extra method on theserver for the client to call. Our gRPC service is defined using protocolbuffers; you can find out lots more about how to define a service in a .protofile ingRPC Basics: Android Java. For now all you need to know is that both theserver and the client “stub” have a SayHello RPC method that takes aHelloRequest parameter from the client and returns a HelloResponse from theserver, and that this method is defined like this:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. }
  6. // The request message containing the user's name.
  7. message HelloRequest {
  8. string name = 1;
  9. }
  10. // The response message containing the greetings
  11. message HelloReply {
  12. string message = 1;
  13. }

Let’s update this so that the Greeter service has two methods. Editsrc/main/proto/helloworld.proto and update it with a new SayHelloAgainmethod, with the same request and response types:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. // Sends another greeting
  6. rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
  7. }
  8. // The request message containing the user's name.
  9. message HelloRequest {
  10. string name = 1;
  11. }
  12. // The response message containing the greetings
  13. message HelloReply {
  14. string message = 1;
  15. }

Remember to save the file!

Update and run the application

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.

Update the server

See theJava quick start.

Update the client

In the same directory, openapp/src/main/java/io/grpc/helloworldexample/HelloworldActivity.java. Call the newmethod like this:

  1. try {
  2. HelloRequest message = HelloRequest.newBuilder().setName(mMessage).build();
  3. HelloReply reply = stub.sayHello(message);
  4. reply = stub.sayHelloAgain(message);
  5. } catch (Exception e) {
  6. StringWriter sw = new StringWriter();
  7. PrintWriter pw = new PrintWriter(sw);
  8. e.printStackTrace(pw);
  9. pw.flush();
  10. return "Failed... : " + System.lineSeparator() + sw;
  11. }

Run!

Just like we did before, from the examples directory:

  • Compile the server
  1. $ ./gradlew installDist
  • Run the server:
  1. $ ./build/install/examples/bin/hello-world-server
  • From another terminal, compile and install the client to your device
  1. $ cd android/helloworld
  2. $ ../../gradlew installDebug

Connecting to the Hello World server via USB

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:

  1. 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 localhostand 8080 as the Host and Port.

Connecting to the Hello World server from an Android Virtual Device

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 Host and Port.

What’s next