我按照本指南将gRPC添加到我的Android项目中,但proto文件似乎无法生成代码。
我把book.proto
和我的Kotlin代码放在appsrcmainjavacomexampleandroid
下面。
这是我项目的build.gradle
:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// Tar's:
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.15' // gRPC
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
这是我的模块build.gradle
:
plugins {
id 'com.android.application'
id 'com.google.protobuf' // Tar's: gRPC
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.android"
minSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures { // Tar: for using ObservableList, which is part of the Data Binding Library - see https://stackoverflow.com/questions/66352403/observablelist-is-missing-in-android-studio and https://developer.android.com/topic/libraries/data-binding/start
dataBinding true
}
}
// Tar's: gRPC {
protobuf {
protoc { artifact = 'com.google.protobuf:protoc:3.10.0' }
plugins {
javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" }
grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.25.0' // CURRENT_GRPC_VERSION
}
}
generateProtoTasks {
all().each { task ->
task.plugins {
javalite {}
grpc { // Options added to --grpc_out
option 'lite' }
}
}
}
}
// }
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.gridlayout:gridlayout:1.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
// Tar's:
implementation "org.java-websocket:Java-WebSocket:1.5.1" // Webscokets
//implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0" // Tar: for JSON: https://github.com/Kotlin/kotlinx.serialization
// Tar's - gRPC: {
implementation 'com.google.protobuf:protobuf-lite:3.0.1'
implementation 'io.grpc:grpc-okhttp:1.25.0' // CURRENT_GRPC_VERSION
implementation 'io.grpc:grpc-protobuf-lite:1.25.0' // CURRENT_GRPC_VERSION
implementation 'io.grpc:grpc-stub:1.25.0' // CURRENT_GRPC_VERSION
implementation 'javax.annotation:javax.annotation-api:1.3.2'
// }
}
可能是什么问题?
看起来proto文件的路径不正确。尝试将proto文件移动到src/main/resouces/proto
或在protobuf插件配置中设置路径。
这与kotlin DSL配合良好。
import com.google.protobuf.gradle.*
plugins {
java
idea
application
id("com.google.protobuf") version "0.8.14"
id("io.freefair.lombok") version "5.3.0"
}
repositories {
mavenCentral()
jcenter()
}
val grpcVersion = "1.34.1"
val protocVersion = "3.12.0"
val slf4jVersion = "1.7.25"
dependencies {
implementation("io.grpc:grpc-netty:${grpcVersion}")
implementation("io.grpc:grpc-protobuf:${grpcVersion}")
implementation("io.grpc:grpc-stub:${grpcVersion}")
implementation("org.slf4j:slf4j-api:${slf4jVersion}")
implementation("org.slf4j:slf4j-simple:${slf4jVersion}")
testImplementation("junit:junit:4.13")
}
// Look here for set path
sourceSets {
main {
proto {
srcDir("src/main/resources/proto")
}
}
}
protobuf {
protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" }
plugins {
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
}
}
generateProtoTasks {
ofSourceSet("main").forEach {
it.plugins {
id("grpc")
}
}
}
}