0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

gRPC Kotlin を Android アプリに導入する構成ガイド

Last updated at Posted at 2025-06-06

概要

Androidアプリに gRPC Kotlin を導入する方法を紹介します。

実行環境

この記事で使用した環境は以下のとおりです:

  • OS: macOS Sequoia
  • Android Studio: Meerkat
  • Kotlin: 2.0.21
  • JDK: 11
  • compileSdk/ targetSdk: 35
  • minSdk: 28

protoファイルを配置

app/src/main/proto に .proto ファイルを配置します。

ディレクトリ構成の例: hello.proto を配置する場合

app/
└── src/
    └── main/
        └── proto/
            └── hello.proto

依存関係の追加

プロジェクトルートの gradle/libs.versions.toml に以下を追加します:

libs.versions.toml
[versions]
grpc = "1.71.0"
grpcKotlin = "1.4.1"
protobuf = "3.25.5"
protobufPlugin = "0.9.4"

[libraries]
grpc-stub = { group = "io.grpc", name = "grpc-stub", version.ref = "grpc" }
grpc-kotlin-stub = { group = "io.grpc", name = "grpc-kotlin-stub", version.ref = "grpcKotlin" }
grpc-okhttp = { group = "io.grpc", name = "grpc-okhttp", version.ref = "grpc" }
grpc-protobuf-lite = { group = "io.grpc", name = "grpc-protobuf-lite", version.ref = "grpc" }
protobuf-javalite = { group = "com.google.protobuf", name = "protobuf-javalite", version.ref = "protobuf" }
protobuf-kotlin-lite = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protobuf" }

[plugins]
protobuf = { id = "com.google.protobuf", version.ref = "protobufPlugin" }

プロジェクトの build.gradle.ktsPlugin を定義

build.gradle.kts (Project)
plugins {
    // 他のプラグイン設定...
    alias(libs.plugins.protobuf) apply false
}

アプリモジュールの build.gradle.kts を設定

build.gradle.kts
plugins {
    // 他のプラグイン設定...
    alias(libs.plugins.protobuf)
}

android {
    // Android構成(省略)
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:${libs.versions.protobuf.get()}"
    }
    plugins {
        create("grpc") {
            artifact = "io.grpc:protoc-gen-grpc-java:${libs.versions.grpc.get()}"
        }
        create("grpckt") {
            artifact = "io.grpc:protoc-gen-grpc-kotlin:${libs.versions.grpcKotlin.get()}:jdk8@jar"
        }
    }
    generateProtoTasks {
        all().forEach {
            it.plugins {
                create("grpc") {
                    option("lite")
                }
                create("grpckt") {
                    option("lite")
                }
            }
            it.builtins {
                create("kotlin") {
                    option("lite")
                }
                create("java") {
                    option("lite")
                }
            }
        }
    }
}

dependencies {
    // 他の依存関係...
    implementation(libs.grpc.protobuf.lite)
    implementation(libs.grpc.stub)
    implementation(libs.grpc.kotlin.stub)
    implementation(libs.protobuf.javalite)
    implementation(libs.protobuf.kotlin.lite)
    implementation(libs.grpc.okhttp)
}

ビルドして確認

Build > Rebuild Project を実行すると、以下のような gRPC 関連ファイルが生成されます。

build/generated/source/proto/
├── debug/
│   ├── grpc/
│   ├── grpckt/
│   ├── java/
│   └── kotlin
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?