grpc.io/public/blog/kotlin-gradle-projects/index.html

257 lines
15 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/css/style.css">
<title>
gRPC ❤ Kotlin &ndash; gRPC
</title>
<link rel="apple-touch-icon" href="/favicons/apple-touch-icon.png" sizes="180x180">
<link rel="icon" type="image/png" href="/favicons/android-chrome-192x192.png" sizes="192x192" >
<link rel="icon" type="image/png" href="/favicons/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/favicons/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="/favicons/manifest.json">
<link rel="mask-icon" href="/favicons/safari-pinned-tab.svg" color="#2DA6B0">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/favicons/mstile-150x150.png">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-60127042-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-60127042-1');
</script>
</head>
<body>
<div id="landing-content">
<div class="row">
<div class="topbannersub">
<nav class="navbar navbar-expand-md navbar-dark topnav">
<a class="navbar-brand" href="https://cjyabraham.github.io/">
<img src="https://cjyabraham.github.io/img/grpc-logo.png" width="114" height="50">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="topnav, collapse navbar-collapse" id="navbarSupportedContent" style="float:right !important">
<ul class="navbar-nav ml-auto">
<li class="nav-item ">
<a class="nav-link" href="https://cjyabraham.github.io/about/">About</a>
</li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle" href="https://cjyabraham.github.io/docs/" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Docs
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="/docs">
Overview
</a>
<a class="dropdown-item" href="/docs/quickstart/">
Quick Start
</a>
<a class="dropdown-item" href="/docs/guides/">
Guides
</a>
<a class="dropdown-item" href="/docs/tutorials/">
Tutorials
</a>
<a class="dropdown-item" href="/docs/reference/">
Reference
</a>
<a class="dropdown-item" href="/docs/samples/">
Samples
</a>
<a class="dropdown-item" href="/docs/talks">
Presentations
</a>
</div>
</li>
<li class="nav-item active">
<a class="nav-link" href="/blog">
Blog
</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="/community">Community</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://packages.grpc.io/">
Packages
</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="https://cjyabraham.github.io/faq/">FAQ</a>
</li>
</ul>
</div>
</nav>
<div class="headertext">Blog</div>
</div>
</div>
</div>
<div class="singleblog">
<h1>gRPC ❤ Kotlin</h1>
<h5>Posted on Tuesday, June 19, 2018
by
<a href="https://github.com/zpencer">Spencer Fang</a>
</h5>
<p>
<p>Did you know that gRPC Java now has out of box support for Kotlin projects built with Gradle? <a href="https://kotlinlang.org/">Kotlin</a> is a modern, statically typed language developed by JetBrains that targets the JVM and Android. It is generally easy for Kotlin programs to interoperate with existing Java libraries. To improve this experience further, we have added support to the <a href="https://github.com/google/protobuf-gradle-plugin/releases">protobuf-gradle-plugin</a> so that the generated Java libraries are automatically picked up by Kotlin. You can now add the protobuf-gradle-plugin to your Kotlin project, and use gRPC just like you would with a typical Java project.</p>
<p>The following examples show you how to configure a project for a JVM application and an Android application using Kotlin.</p>
<h3 id="kotlin-grpc-client-and-server">Kotlin gRPC client and server</h3>
<p>The full example can be found <a href="https://github.com/grpc/grpc-java/tree/master/examples/example-kotlin">here</a>.</p>
<p>Configuring gRPC for a Kotlin project is the same as configuring it for a Java project.</p>
<p>Below is a snippet of the example project&rsquo;s <code>build.gradle</code> highlighting some Kotlin related sections:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-groovy" data-lang="groovy">apply plugin: <span style="color:#e6db74">&#39;kotlin&#39;</span>
apply plugin: <span style="color:#e6db74">&#39;com.google.protobuf&#39;</span>
<span style="color:#75715e">// Generate IntelliJ IDEA&#39;s .idea &amp; .iml project files.
</span><span style="color:#75715e">// protobuf-gradle-plugin automatically registers *.proto and the gen output files
</span><span style="color:#75715e">// to IntelliJ as sources.
</span><span style="color:#75715e">// For best results, install the Protobuf and Kotlin plugins for IntelliJ.
</span><span style="color:#75715e"></span>apply plugin: <span style="color:#e6db74">&#39;idea&#39;</span>
buildscript <span style="color:#f92672">{</span>
ext<span style="color:#f92672">.</span><span style="color:#a6e22e">kotlin_version</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;1.2.21&#39;</span>
repositories <span style="color:#f92672">{</span>
mavenCentral<span style="color:#f92672">()</span>
<span style="color:#f92672">}</span>
dependencies <span style="color:#f92672">{</span>
classpath <span style="color:#e6db74">&#39;com.google.protobuf:protobuf-gradle-plugin:0.8.5&#39;</span>
classpath <span style="color:#e6db74">&#34;org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version&#34;</span>
<span style="color:#f92672">}</span>
<span style="color:#f92672">}</span>
dependencies <span style="color:#f92672">{</span>
compile <span style="color:#e6db74">&#34;org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version&#34;</span>
<span style="color:#75715e">// The rest of the projects dep are added below, refer to example URL
</span><span style="color:#75715e"></span><span style="color:#f92672">}</span>
<span style="color:#75715e">// The standard protobuf block, same as normal gRPC Java projects
</span><span style="color:#75715e"></span>protobuf <span style="color:#f92672">{</span>
protoc <span style="color:#f92672">{</span> artifact <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;com.google.protobuf:protoc:3.5.1-1&#39;</span> <span style="color:#f92672">}</span>
plugins <span style="color:#f92672">{</span>
grpc <span style="color:#f92672">{</span> artifact <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;io.grpc:protoc-gen-grpc-java:${grpcVersion}&#34;</span> <span style="color:#f92672">}</span>
<span style="color:#f92672">}</span>
generateProtoTasks <span style="color:#f92672">{</span>
all<span style="color:#f92672">()*.</span><span style="color:#a6e22e">plugins</span> <span style="color:#f92672">{</span> grpc <span style="color:#f92672">{}</span> <span style="color:#f92672">}</span>
<span style="color:#f92672">}</span>
<span style="color:#f92672">}</span></code></pre></div>
<p>Now Kotlin source files can use the proto generated messages and gRPC stubs. By default, Kotlin sources should be placed in <code>src/main/kotlin</code> and <code>src/test/kotlin</code>. If needed, run <code>./gradlew generateProto generateTestProto</code> and refresh IntelliJ for the generated sources to appear in the IDE. Finally, run <code>./gradlew installDist</code> to build the project, and use <code>./build/install/examples/bin/hello-world-client</code> or <code>./build/install/examples/bin/hello-world-server</code> to run the example.</p>
<p>You can read more about configuring Kotlin <a href="https://kotlinlang.org/docs/reference/using-gradle.html">here</a>.</p>
<h3 id="kotlin-android-grpc-application">Kotlin Android gRPC application</h3>
<p>The full example can be found <a href="https://github.com/grpc/grpc-java/tree/master/examples/example-kotlin/android/helloworld">here</a>.</p>
<p>Configuring gRPC for a Kotlin Android project is the same as configuring it for a normal Android project.</p>
<p>In the top level <code>build.gradle</code> file:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-groovy" data-lang="groovy">buildscript <span style="color:#f92672">{</span>
ext<span style="color:#f92672">.</span><span style="color:#a6e22e">kotlin_version</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;1.2.21&#39;</span>
repositories <span style="color:#f92672">{</span>
google<span style="color:#f92672">()</span>
jcenter<span style="color:#f92672">()</span>
<span style="color:#f92672">}</span>
dependencies <span style="color:#f92672">{</span>
classpath <span style="color:#e6db74">&#39;com.android.tools.build:gradle:3.0.1&#39;</span>
classpath <span style="color:#e6db74">&#34;com.google.protobuf:protobuf-gradle-plugin:0.8.5&#34;</span>
classpath <span style="color:#e6db74">&#34;org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version&#34;</span>
<span style="color:#f92672">}</span>
<span style="color:#f92672">}</span>
allprojects <span style="color:#f92672">{</span>
repositories <span style="color:#f92672">{</span>
google<span style="color:#f92672">()</span>
jcenter<span style="color:#f92672">()</span>
<span style="color:#f92672">}</span>
<span style="color:#f92672">}</span></code></pre></div>
<p>And in the app module&rsquo;s <code>build.gradle</code> file:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-groovy" data-lang="groovy">apply plugin: <span style="color:#e6db74">&#39;com.android.application&#39;</span>
apply plugin: <span style="color:#e6db74">&#39;kotlin-android&#39;</span>
apply plugin: <span style="color:#e6db74">&#39;kotlin-android-extensions&#39;</span>
apply plugin: <span style="color:#e6db74">&#39;com.google.protobuf&#39;</span>
repositories <span style="color:#f92672">{</span>
mavenCentral<span style="color:#f92672">()</span>
<span style="color:#f92672">}</span>
dependencies <span style="color:#f92672">{</span>
compile <span style="color:#e6db74">&#34;org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version&#34;</span>
<span style="color:#75715e">// refer to full example for remaining deps
</span><span style="color:#75715e"></span><span style="color:#f92672">}</span>
protobuf <span style="color:#f92672">{</span>
<span style="color:#75715e">// The normal gRPC configuration for Android goes here
</span><span style="color:#75715e"></span><span style="color:#f92672">}</span>
android <span style="color:#f92672">{</span>
<span style="color:#75715e">// Android Studio 3.1 does not automatically pick up &#39;src/main/kotlin&#39; as source files
</span><span style="color:#75715e"></span> sourceSets <span style="color:#f92672">{</span>
main<span style="color:#f92672">.</span><span style="color:#a6e22e">java</span><span style="color:#f92672">.</span><span style="color:#a6e22e">srcDirs</span> <span style="color:#f92672">+=</span> <span style="color:#e6db74">&#39;src/main/kotlin&#39;</span>
<span style="color:#f92672">}</span>
<span style="color:#f92672">}</span></code></pre></div>
<p>Just like the non-Android project, run <code>./gradlew generateProto generateProto</code> to run the proto code generator and <code>./gradlew build</code> to build the project.</p>
<p>Finally, test out the Android app by opening the project in Android Studio and selecting <code>Run &gt; Run 'app'</code>.</p>
<p><img src="/img/kotlin-project-android-app.png" alt="Kotlin Android app example" style="max-width: 404px"></p>
<p>We are excited about improving the gRPC experience for Kotlin developers. Please add enhancement ideas or bugs to the <a href="https://github.com/google/protobuf-gradle-plugin/issues">protobuf-gradle-plugin issue tracker</a> or the <a href="https://github.com/grpc/grpc-java/issues">grpc-java issue tracker</a>.</p>
</p>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>