<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Fernie</title>
    <description>The latest articles on DEV Community by Fernie (@efernie).</description>
    <link>https://dev.to/efernie</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F577708%2Fd76185a0-153b-41b0-8105-03358dd1b93c.png</url>
      <title>DEV Community: Fernie</title>
      <link>https://dev.to/efernie</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/efernie"/>
    <language>en</language>
    <item>
      <title>Android Java GRPC Tutorial</title>
      <dc:creator>Fernie</dc:creator>
      <pubDate>Mon, 22 Feb 2021 13:22:17 +0000</pubDate>
      <link>https://dev.to/efernie/android-java-grpc-tutorial-11pg</link>
      <guid>https://dev.to/efernie/android-java-grpc-tutorial-11pg</guid>
      <description>&lt;p&gt;This tutorial is to show you the basic set up to get GRPC working with an Android app using Java as the primary language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/studio" rel="noopener noreferrer"&gt;https://developer.android.com/studio&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Set Up&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Were going to be borrowing the example java server from the main grpc-java repo&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;clone &lt;a href="https://github.com/grpc/grpc-java" rel="noopener noreferrer"&gt;https://github.com/grpc/grpc-java&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;change directories to the examples folder&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;execute &lt;code&gt;./gradlew installDist&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;then run &lt;code&gt;./build/install/examples/bin/hello-world-server&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In the &lt;code&gt;Welcome to Android Studio&lt;/code&gt; window, click &lt;code&gt;Create New Project&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;code&gt;Select a Project Template&lt;/code&gt; window, select &lt;code&gt;Empty Activity&lt;/code&gt; and click &lt;code&gt;Next.&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm4a8ungo0ze8rlv8q7m0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm4a8ungo0ze8rlv8q7m0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In the Configure your project window, complete the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enter &lt;code&gt;HelloWorldGrpc&lt;/code&gt; in the &lt;code&gt;Name&lt;/code&gt; field.&lt;/li&gt;
&lt;li&gt;Select &lt;code&gt;Java Language&lt;/code&gt; drop-down menu.&lt;/li&gt;
&lt;li&gt;Click &lt;code&gt;Finish&lt;/code&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5rca57v80wtfwhbceatz.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Open &lt;code&gt;AndroidManifest.xml&lt;/code&gt; add this line above the application block.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;uses-permission android:name="android.permission.INTERNET" /&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;For reference here is what it should look like
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Open the project gradle file and add this line to the dependencies section&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.14"&lt;/code&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe6wwqgcurtqutzt250rj.png" alt="Alt Text"&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Next we add some lines to the module gradle file&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhsfl34o3u0wt6cah70bw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhsfl34o3u0wt6cah70bw.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add com.google.protobuf to the top in the plugins object
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Add the protobuf block, I add this right above the dependencies block
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Add the dependencies to the block, the &lt;code&gt;okhttp&lt;/code&gt;, &lt;code&gt;grpc-protobuf-lite&lt;/code&gt;, &lt;code&gt;grpc-stub&lt;/code&gt;, &lt;code&gt;annotations-api&lt;/code&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now time to add the proto file&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your going to want to go to the terminal and create the proto directory in the main folder
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foil5uwhx9788qjq59h3o.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;Then create the proto file, whats great about using gradle and GRPC dependencies is that the proto files are automatically compiled for you
&lt;code&gt;touch HelloWorld.proto&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add this to the file
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Lets now add the text fields and buttons to the main activity&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open the &lt;code&gt;activity_main.xml&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;Your going to want to delete the &lt;code&gt;TextView&lt;/code&gt; component
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0blfczilwcusg9wuyjur.png" alt="Alt Text"&gt;
Your going to want to add these components to the view&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PlainText&lt;/code&gt; three of them, one for the host input, another for the port input and a third for the message&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TextView&lt;/code&gt; for the result message&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Button&lt;/code&gt; for the sending action&lt;/li&gt;
&lt;li&gt;It doesn't matter how you arrange them, if you wanted to get this done quickly then copy the xml file from step 9&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add ID's and settings for the components&lt;br&gt;
For the host and port, I like to default those to the grpc server&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Host &lt;code&gt;PlainText&lt;/code&gt; component id should equal &lt;code&gt;host&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Port &lt;code&gt;PlainText&lt;/code&gt; component id should equal &lt;code&gt;port&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Message &lt;code&gt;PlainText&lt;/code&gt; component id should equal &lt;code&gt;message&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Send/Submit &lt;code&gt;Button&lt;/code&gt; component id should equal &lt;code&gt;send&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Result &lt;code&gt;TextView&lt;/code&gt; component id should equal &lt;code&gt;result&lt;/code&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkskjq996b2d0rwsjz6af.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now we move on to the code of the &lt;code&gt;MainActivity&lt;/code&gt;. Open up the MainActivity file and declare these vars&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;hostEdit&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;portEdit&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;messageEdit&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sendButton&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;resultText&lt;/code&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add this code to &lt;code&gt;onCreate&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the &lt;code&gt;GrpcTask&lt;/code&gt; and code&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the &lt;code&gt;sendGrpcMessage&lt;/code&gt; function that will tie to the submit button&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go back to &lt;code&gt;activity_main.xml&lt;/code&gt; and set the button onClick listener to &lt;code&gt;sendGrpcMessage&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn1l065k14pkul2kdpb4z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn1l065k14pkul2kdpb4z.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the app on the emulator&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For the host use &lt;code&gt;10.0.2.2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;For the port use &lt;code&gt;50051&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Then add a message&lt;/li&gt;
&lt;li&gt;Click Submit&lt;/li&gt;
&lt;li&gt;You should get back Hello and what ever you placed in the message component and see it in the result component&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;Link to the code for &lt;a href="https://github.com/efernie/Android-Java-Grpc-Tutorial" rel="noopener noreferrer"&gt;https://github.com/efernie/Android-Java-Grpc-Tutorial&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A Kotlin version is coming soon&lt;/p&gt;

</description>
      <category>android</category>
      <category>grpc</category>
      <category>java</category>
    </item>
  </channel>
</rss>
