Android Java GRPC Tutorial
Tags: android, grpc, java
Published: 2021-02-22 13:22:17

This tutorial is to show you the basic set up to get GRPC working with an Android app using Java as the primary language.

**Prerequisites**

* [https://developer.android.com/studio](https://developer.android.com/studio)

**Set Up**

Were going to be borrowing the example java server from the main grpc-java repo

* clone [https://github.com/grpc/grpc-java](https://github.com/grpc/grpc-java)

* change directories to the examples folder

* execute `./gradlew installDist`

* then run `./build/install/examples/bin/hello-world-server`

---

1. In the `Welcome to Android Studio` window, click `Create New Project`.

2. In the `Select a Project Template` window, select `Empty Activity` and click `Next.`
  ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m4a8ungo0ze8rlv8q7m0.png)

3. In the Configure your project window, complete the following:
  * Enter `HelloWorldGrpc` in the `Name` field.
  * Select `Java Language` drop-down menu.
  * Click `Finish`
  ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5rca57v80wtfwhbceatz.png)

4. Open `AndroidManifest.xml` add this line above the application block.
  * `<uses-permission android:name="android.permission.INTERNET" />`
  * For reference here is what it should look like
{% gist https://gist.github.com/efernie/7580a383724a264e7c74baab0b6cd839 %}

5. Open the project gradle file and add this line to the dependencies section
  * `classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.14"`
  ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6wwqgcurtqutzt250rj.png)
{% gist https://gist.github.com/efernie/498a01bc4a6fae664b8205282c60e9e0 %}

6. Next we add some lines to the module gradle file
  ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsfl34o3u0wt6cah70bw.png)
  * Add com.google.protobuf to the top in the plugins object
  {% gist https://gist.github.com/efernie/31dc3e27d4eb503786bd04e1049883dc %}
  * Add the protobuf block, I add this right above the dependencies block
  {% gist https://gist.github.com/efernie/0c5f5deb5689abe157ccc34f3a33c3c2 %}
  * Add the dependencies to the block, the `okhttp`, `grpc-protobuf-lite`, `grpc-stub`, `annotations-api`
  {% gist https://gist.github.com/efernie/a5ea8a95453df8844a4601ab0eb6b8e7 %}

7. Now time to add the proto file
  * Your going to want to go to the terminal and create the proto directory in the main folder
  ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oil5uwhx9788qjq59h3o.png)
  * Then create the proto file, whats great about using gradle and GRPC dependencies is that the proto files are automatically compiled for you
  `touch HelloWorld.proto`
  * Add this to the file
  {% gist https://gist.github.com/efernie/59406cb922c66f3a6b8b70d073ccc6d3 %}

8. Lets now add the text fields and buttons to the main activity
  * Open the `activity_main.xml` file
  * Your going to want to delete the `TextView` component
  ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0blfczilwcusg9wuyjur.png)
  Your going to want to add these components to the view
  * `PlainText` three of them, one for the host input, another for the port input and a third for the message
  * `TextView` for the result message
  * `Button` for the sending action
  * It doesn't matter how you arrange them, if you wanted to get this done quickly then copy the xml file from step 9

9. Add ID's and settings for the components
  For the host and port, I like to default those to the grpc server
  * Host `PlainText` component id should equal `host`
  * Port `PlainText` component id should equal `port`
  * Message `PlainText` component id should equal `message`
  * Send/Submit `Button` component id should equal `send`
  * Result `TextView` component id should equal `result`
  {% gist https://gist.github.com/efernie/93da3fec61f0830547243f237b65ec8a %}
  ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kskjq996b2d0rwsjz6af.png)

10. Now we move on to the code of the `MainActivity`. Open up the MainActivity file and declare these vars
  * `hostEdit`
  * `portEdit`
  * `messageEdit`
  * `sendButton`
  * `resultText`
  {% gist https://gist.github.com/efernie/1bd00f30a05a208ff9c018f79e9b561b %}

11. Add this code to `onCreate`
  {% gist https://gist.github.com/efernie/9bc68a94d84681ff69d146da0f3c15ee %}

11. Add the `GrpcTask` and code
  {% gist https://gist.github.com/efernie/2784c5b9fb2a376d3245001ae13ce1ce %}

13. Add the `sendGrpcMessage` function that will tie to the submit button
  {% gist https://gist.github.com/efernie/5f972cddcc1f2305412ed838fab41f97 %}

14. Go back to `activity_main.xml` and set the button onClick listener to `sendGrpcMessage`
  ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1l065k14pkul2kdpb4z.png)

15. Run the app on the emulator
  * For the host use `10.0.2.2`
  * For the port use `50051`
  * Then add a message
  * Click Submit
  * You should get back Hello and what ever you placed in the message component and see it in the result component

---

Link to the code for [https://github.com/efernie/Android-Java-Grpc-Tutorial](https://github.com/efernie/Android-Java-Grpc-Tutorial)

A Kotlin version is coming soon