gRPC for Kotlin: Libraries and Usage Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

gRPC for Kotlin: Libraries and Usage Examples



gRPC is a high-performance remote procedure call (RPC) framework developed by Google. It uses Protocol Buffers (protobuf) for data serialization and HTTP/2 for transport. In the Kotlin ecosystem, gRPC integrates particularly smoothly thanks to libraries that support coroutines and strong typing. In this article, we will look at the main tools needed to work with gRPC in Kotlin and provide working examples of a server and client.



Main Libraries



To work with gRPC in Kotlin, you will need the following stack:



  • grpc-kotlin — the official library from Google providing Kotlin-specific APIs, including support for coroutines (suspend functions, Flow).
  • protobuf-java and protobuf-gradle-plugin — for generating classes from .proto files.
  • grpc-netty or grpc-okhttp — the transport layer (Netty for server, OkHttp for Android/client).
  • javax.annotation-api — often required for correct compilation of generated code.


Use Gradle (Kotlin DSL) to manage dependencies. Example of a dependencies block:



dependencies {    implementation("io.grpc:grpc-netty-shaded:1.58.0")    implementation("io.grpc:grpc-protobuf:1.58.0")    implementation("io.grpc:grpc-stub:1.58.0")    implementation("io.grpc:grpc-kotlin-stub:1.4.0")    implementation("com.google.protobuf:protobuf-java:3.24.3")    implementation("javax.annotation:javax.annotation-api:1.3.2")}


Also, be sure to add the protobuf plugin for code generation:



plugins {    id("com.google.protobuf") version "0.9.4"}

protobuf { protoc { artifact = "com.google.protobuf:protoc:3.24.3" } plugins { create("grpc") { artifact = "io.grpc:protoc-gen-grpc-java:1.58.0" } create("grpckt") { artifact = "io.grpc:protoc-gen-grpc-kotlin:1.4.0:jdk8@jar" } } generateProtoTasks { all().forEach { it.plugins { create("grpc") create("grpckt") } } }}


Defining a Service via a .proto File



Create the file src/main/proto/hello.proto:



syntax = "proto3";

package com.example.grpc;

service Greeter { rpc SayHello (HelloRequest) returns (HelloReply);}

message HelloRequest { string name = 1;}

message HelloReply { string message = 1;}


After running the Gradle build, the following classes will be generated: HelloRequest, HelloReply, GreeterGrpc, and GreeterGrpcKt (Kotlin version with coroutines).



Implementing a gRPC Server in Kotlin



The server will use the generated class GreeterGrpcKt.GreeterCoroutineImplBase:



import com.example.grpc.GreeterGrpcKtimport com.example.grpc.HelloReplyimport com.example.grpc.HelloRequestimport io.grpc.ServerBuilderimport kotlinx.coroutines.runBlocking

class GreeterService : GreeterGrpcKt.GreeterCoroutineImplBase() { override suspend fun sayHello(request: HelloRequest): HelloReply { val message = "Hello, ${request.name}!" println("Received request: ${request.name}") return HelloReply.newBuilder().setMessage(message).build() }}

fun main() { val port = 50051 val server = ServerBuilder.forPort(port) .addService(GreeterService()) .build()

server.start() println("Server started on port $port") server.awaitTermination()}


Implementing a gRPC Client in Kotlin



The client uses a channel and a stub with coroutine support:



import com.example.grpc.GreeterGrpcKti                    

Recommendations