Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin only proto Unresolved reference #756

Open
mtrakal opened this issue Jul 10, 2024 · 0 comments
Open

Kotlin only proto Unresolved reference #756

mtrakal opened this issue Jul 10, 2024 · 0 comments

Comments

@mtrakal
Copy link

mtrakal commented Jul 10, 2024

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

  • main issue is naming Class inside file: SettingsKt instead of: Settings or
  • inside the file references are to file: com.sample.proto.Settings and not to: com.sample.proto.SettingsKt

Expected state

  • generate kotlin code, which is possible to compile (sample is still not, just reference to proper class names):
// 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant