Android Gradle 导入 Kotlin gRPC

2022-10-25 14:03 CST

project build.gradle

1
2
3
plugins {
    id "com.google.protobuf" version "0.9.1" apply false
}

module build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
android {
    sourceSets {
        main {
            proto {
                srcDir '../../protos'
            }
        }
    }
}

dependencies {
    implementation 'io.grpc:grpc-stub:1.50.2'
    implementation 'io.grpc:grpc-protobuf-lite:1.50.2'
    implementation 'io.grpc:grpc-kotlin-stub:1.3.0'
    implementation 'com.google.protobuf:protobuf-kotlin-lite:3.21.8'
    runtimeOnly 'io.grpc:grpc-okhttp:1.46.0'
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.21.8'
    }
    plugins {
        java {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.50.0'
        }
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.50.0'
        }
        grpckt {
            artifact = 'io.grpc:protoc-gen-grpc-kotlin:1.3.0:jdk8@jar'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                kotlin {
                    option 'lite'
                }
            }
            task.plugins {
                java {
                    option 'lite'
                }
                grpc {
                    option 'lite'
                }
                grpckt {
                    option 'lite'
                }
            }
        }
    }
}

参考代码

https://github.com/grpc/grpc-kotlin/tree/master/examples/android

Android Gradle 导入 Kotlin gRPC by mkckr0 is licensed under CC BY-NC-ND 4.0