Closed louie-wh closed 1 year ago
this is build.gradle.kts
import com.google.protobuf.gradle.id
import com.google.protobuf.gradle.proto
plugins {
id("com.android.library")
id("com.google.protobuf")
}
android {
namespace = "com.example.car.grpc.proto"
compileSdk = 33
defaultConfig {
minSdk = 16
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
sourceSets {
getByName("main") {
proto {
srcDir("src/main/proto")
}
}
}
}
protobuf {
protoc {
// The artifact spec for the Protobuf Compiler
artifact = "com.google.protobuf:protoc:3.22.3"
}
plugins {
// Optional: an artifact spec for a protoc plugin, with "grpc" as
// the identifier, which can be referred to in the "plugins"
// container of the "generateProtoTasks" closure.
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:1.57.1"
}
id("javalite") {
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
}
}
generateProtoTasks {
ofSourceSet("src/main/proto").forEach {
it.plugins {
// Apply the "grpc" plugin whose spec is defined above, without
// options. Note the braces cannot be omitted, otherwise the
// plugin will not be added. This is because of the implicit way
// NamedDomainObjectContainer binds the methods.
// id("grpc") { option("lite") }
// id("java") { option("lite") }
id("grpc")
id("javalite") { }
}
}
}
}
dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.9.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
no work!
What error did you experience?
Remove this:
sourceSets {
getByName("main") {
proto {
srcDir("src/main/proto")
}
}
}
Use all()
, not ofSourceSet()
.
generateProtoTasks {
ofSourceSet("src/main/proto").forEach {
Don't use the ancient protoc. This will cause problems:
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
You're going to need those uncommented:
// id("grpc") { option("lite") }
// id("java") { option("lite") }
I suggest you take a look at our main README again. It seems you already have enough here to see the difference in syntax.
this is my build.gradle.ksl . no error , no java file generated. this is my project url
https://github.com/louie-wh/GrpcProto/tree/master
import com.google.protobuf.gradle.id
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("com.google.protobuf")
}
android {
namespace = "com.example.kotlindsl"
compileSdk = 34
defaultConfig {
minSdk = 24
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}
buildTypes {
release {
isMinifyEnabled = 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"
}
}
protobuf {
protoc {
// The artifact spec for the Protobuf Compiler
artifact = "com.google.protobuf:protoc:3.6.1"
}
plugins {
// Optional: an artifact spec for a protoc plugin, with "grpc" as
// the identifier, which can be referred to in the "plugins"
// container of the "generateProtoTasks" closure.
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:1.15.1"
}
}
generateProtoTasks {
ofSourceSet("main").forEach {
it.plugins {
// Apply the "grpc" plugin whose spec is defined above, without
// options. Note the braces cannot be omitted, otherwise the
// plugin will not be added. This is because of the implicit way
// NamedDomainObjectContainer binds the methods.
id("grpc") { }
}
}
}
}
dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.9.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
this is my build.gradle.ksl . no error , no java file generated.
Are you sure? I tried it and I get lots of compilation errors because protobuf and grpc aren't dependencies.
The main components missing I see it need are:
id("grpc") { option("lite") }
id("java") { option("lite") }
and:
dependencies {
implementation("io.grpc:grpc-protobuf-lite:1.57.2")
implementation("io.grpc:grpc-stub:1.57.2")
compileOnly("org.apache.tomcat:annotations-api:6.0.53")
}
Although at that point you realize that your protoc was ancient. This worked without issue:
import com.google.protobuf.gradle.id
plugins {
...
id("com.google.protobuf")
}
...
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.22.3"
}
plugins {
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:1.57.2"
}
}
generateProtoTasks {
all().forEach {
it.plugins {
id("grpc") { option("lite") }
id("java") { option("lite") }
}
}
}
}
dependencies {
...
implementation("io.grpc:grpc-protobuf-lite:1.57.2")
implementation("io.grpc:grpc-stub:1.57.2")
compileOnly("org.apache.tomcat:annotations-api:6.0.53")
}
Thanks you tips . now it can work .
But sometimes I need invalid cache and restart It can work make project no work.
Feel free to file an issue at https://github.com/google/protobuf-gradle-plugin for that. But you'd need to report the actual error messages, and ideally steps to reproduce. I'm aware of a history of issues where the cache is unable to be used, especially when sharing a cache across machines, but none where the cache is broken.
Is your feature request related to a problem?
Describe the solution you'd like
I want to use grpc-java in my android project with kotlin DSL but no java file generate.
I hava read those doc https://github.com/grpc/grpc-java/pull/5202 https://github.com/google/protobuf-gradle-plugin/blob/master/examples/exampleKotlinDslProject/build.gradle.kts no work!
can supply kotlin dsl example ?