grpc / grpc-kotlin

Kotlin gRPC implementation. HTTP/2 based RPC
https://grpc.io/docs/languages/kotlin
Apache License 2.0
1.18k stars 160 forks source link

Grpc generator generating files from Proto with Kt suffix but with full of syntax errors, #449

Closed bartalusCsaba closed 8 months ago

bartalusCsaba commented 8 months ago

Hello. I am trying to use the grcp library to resolve the proto file generating. My problem is that the files are generated but is no dependencies for the real classes which is defined in proto file. I mean if int the proto there is a HelloRequest than I have a HelloRequestKt.kt file but if I access it it has unimported dependencies as HelloRequest which was not generetad... I don't understand what is happening.

Here is my build.gradle (:app module):

`

  plugins {
      id("com.android.application")
      id("org.jetbrains.kotlin.android")
      id("com.google.protobuf") version "0.9.4"
      id("kotlin-kapt")
      id("com.google.dagger.hilt.android")
  }

android {
    namespace = "hu.geri.android_networking_protocols_sample"
    compileSdk = 34

    defaultConfig {
        applicationId = "hu.geri.android_networking_protocols_sample"
        minSdk = 26
        targetSdk = 33
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.4.3"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
            excludes += "META-INF/INDEX.LIST"
            excludes += "META-INF/io.netty.versions.properties"
        }
    }
}

dependencies {

    implementation("androidx.core:core-ktx:1.9.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
    implementation("androidx.activity:activity-compose:1.8.0")
    implementation(platform("androidx.compose:compose-bom:2023.03.00"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")
    implementation(platform("androidx.compose:compose-bom:2023.03.00"))
    implementation(platform("androidx.compose:compose-bom:2023.03.00"))
    implementation(platform("androidx.compose:compose-bom:2023.03.00"))
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")

    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")

    // HILT
    implementation("com.google.dagger:hilt-android:2.44")
    kapt("com.google.dagger:hilt-android-compiler:2.44")

    // RETROFIT
    implementation("com.google.code.gson:gson:2.9.0")
    implementation("com.squareup.retrofit2:retrofit:2.9.0")
    implementation("com.squareup.retrofit2:converter-gson:2.9.0")
    implementation("com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.1")

    // gRPC
    implementation("io.grpc:grpc-netty:1.57.2")
    implementation("io.grpc:grpc-protobuf:1.57.2")
    implementation("io.grpc:grpc-stub:1.57.2")
    implementation("io.grpc:grpc-kotlin-stub:1.4.0")
    implementation("com.google.protobuf:protobuf-kotlin:3.24.1")
    implementation("com.google.protobuf:protobuf-java:3.24.1")
}

kotlin {
    jvmToolchain(17)
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().all {
    kotlinOptions {
        freeCompilerArgs = listOf("-opt-in=kotlin.RequiresOptIn")
    }
}

protobuf {

    protoc {
        artifact = "com.google.protobuf:protoc:3.24.1"
    }

    plugins {
        create("grpc") {
            artifact = "io.grpc:protoc-gen-grpc-java:1.57.2"
        }
        create("grpckt") {
            artifact = "io.grpc:protoc-gen-grpc-kotlin:1.4.0:jdk8@jar"
        }
    }

    generateProtoTasks {
        all().forEach {
            it.plugins {
                create("grpc")
                create("grpckt")
            }
            it.builtins {
                create("kotlin")
            }
        }
    }
}

`

Here is my proto file: `

syntax = "proto3";

option java_multiple_files = true;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

`

Here are my generated files. Also the unimported and ungenerated file which the generated files are using.

Screenshot 2023-10-27 140041 Screenshot 2023-10-27 140102

Can somebody explain what I am doing wrong?

Ps.: This is blocker for me because the compiler doesn't make the build because of the syntax errors.

jamesward commented 8 months ago

In an Android project I think that the protobuf Gradle Plugin isn't automatically configuring the java protoc plugin. Can you try to add it:

it.builtins {
    create("kotlin")
    create("java") {
        option("lite")
    }
}
jamesward commented 8 months ago

Note that you should see a build/generated/source/proto/debug/java directory (after build).

bartalusCsaba commented 8 months ago

Right this was the issue. Thank you so much. Finally works like a charm.