google / protobuf-gradle-plugin

Protobuf Plugin for Gradle
Other
1.77k stars 273 forks source link

Kotlin only proto Unresolved reference #756

Open mtrakal opened 4 months ago

mtrakal commented 4 months ago

version catalog

androidxDataStore = "1.1.1"
protobuf = "4.27.2"
protobufPlugin = "0.9.4"

androidx-dataStore-core = { group = "androidx.datastore", name = "datastore", version.ref = "androidxDataStore" }
protobuf-kotlin-lite = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protobuf" }
protobuf-protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protobuf" }

protobuf = { id = "com.google.protobuf", version.ref = "protobufPlugin" }
plugins {
// ...
    alias(libs.plugins.protobuf)
}

android {
    namespace = "com.sample.proto"
}

dependencies {
    implementation(libs.androidx.dataStore.core)
    implementation(libs.protobuf.kotlin.lite)
}

protobuf {
    protoc {
        artifact = libs.protobuf.protoc.get().toString()
    }
    generateProtoTasks {
        all().forEach { task ->
            task.builtins {
                create("kotlin") {
                    option("lite")
                }
            }
        }
    }
}

src/main/proto/sample.proto

syntax = "proto3";

option java_package = "com.sample.proto";
option java_multiple_files = true;

message Settings {
  int32 example_counter = 1;
}

generate file named: SettingsKt.kt

// Generated by the protocol buffer compiler. DO NOT EDIT!
// NO CHECKED-IN PROTOBUF GENCODE
// source: sample.proto

// Generated files should ignore deprecation warnings
@file:Suppress("DEPRECATION")
package com.sample.proto;

@kotlin.jvm.JvmName("-initializesettings")
public inline fun settings(block: com.sample.proto.SettingsKt.Dsl.() -> kotlin.Unit): com.sample.proto.Settings =
  com.sample.proto.SettingsKt.Dsl._create(com.sample.proto.Settings.newBuilder()).apply { block() }._build()
/**
 * Protobuf type `Settings`
 */
public object SettingsKt {
  @kotlin.OptIn(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)
  @com.google.protobuf.kotlin.ProtoDslMarker
  public class Dsl private constructor(
    private val _builder: com.sample.proto.Settings.Builder
  ) {
    public companion object {
      @kotlin.jvm.JvmSynthetic
      @kotlin.PublishedApi
      internal fun _create(builder: com.sample.proto.Settings.Builder): Dsl = Dsl(builder)
    }

    @kotlin.jvm.JvmSynthetic
    @kotlin.PublishedApi
    internal fun _build(): com.sample.proto.Settings = _builder.build()

    /**
     * <code>int32 example_counter = 1;</code>
     */
    public var exampleCounter: kotlin.Int
      @JvmName("getExampleCounter")
      get() = _builder.getExampleCounter()
      @JvmName("setExampleCounter")
      set(value) {
        _builder.setExampleCounter(value)
      }
    /**
     * `int32 example_counter = 1;`
     */
    public fun clearExampleCounter() {
      _builder.clearExampleCounter()
    }
  }
}
public inline fun com.sample.proto.Settings.copy(block: `com.sample.proto`.SettingsKt.Dsl.() -> kotlin.Unit): com.sample.proto.Settings =
  `com.sample.proto`.SettingsKt.Dsl._create(this.toBuilder()).apply { block() }._build()

What is wrong

Expected state

// Generated by the protocol buffer compiler. DO NOT EDIT!
// NO CHECKED-IN PROTOBUF GENCODE
// source: sample.proto

// Generated files should ignore deprecation warnings
@file:Suppress("DEPRECATION")
package com.sample.proto

@kotlin.jvm.JvmName("-initializesettings")
public inline fun settings(block: com.sample.proto.SettingsKt.Dsl.() -> kotlin.Unit): com.sample.proto.SettingsKt =
  com.sample.proto.SettingsKt.Dsl._create(???).apply { block() }._build()
/**
 * Protobuf type `Settings`
 */
public object SettingsKt {
  @kotlin.OptIn(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)
  @com.google.protobuf.kotlin.ProtoDslMarker
  public class Dsl private constructor(
    private val _builder: com.sample.proto.SettingsKt.Dsl
  ) {
    public companion object {
      @kotlin.jvm.JvmSynthetic
      @kotlin.PublishedApi
      internal fun _create(builder: com.sample.proto.SettingsKt.Dsl): Dsl = Dsl(builder)
    }

    @kotlin.jvm.JvmSynthetic
    @kotlin.PublishedApi
    internal fun _build(): com.sample.proto.SettingsKt = _builder._build()

    /**
     * <code>int32 example_counter = 1;</code>
     */
    public var exampleCounter: kotlin.Int
      @JvmName("getExampleCounter")
      get() = _builder.exampleCounter
      @JvmName("setExampleCounter")
      set(value) {
        _builder.exampleCounter = value
      }
    /**
     * `int32 example_counter = 1;`
     */
    public fun clearExampleCounter() {
      _builder.clearExampleCounter()
    }
  }
}
public inline fun com.sample.proto.SettingsKt.copy(block: `com.sample.proto`.SettingsKt.Dsl.() -> kotlin.Unit): com.sample.proto.SettingsKt =
  `com.sample.proto`.SettingsKt.Dsl._create(this.???).apply { block() }._build()

Workaround

Generate java sources:

task.builtins {
                create("java") {
                    option("lite")
                }

//                create("kotlin") {
//                    option("lite")
//                }
            }

It will generate java sources which is able to compile (and don't need to add com.google.protobuf:protobuf-java-lite, it generate with kotlin-lite version without any issue)