Protobuf "Unresolved reference" with generated classes in Android Studio using Kotlin

Viewed 1176

it's my first time diving in with protobuf and jetpack's DataStore but I've encountered an issue which is quite confusing.

I've created my .proto file in src/main/java/proto, it's really simple:

syntax = "proto3";

option java_package = "com.test.appname.proto";
option java_multiple_files = true;

message WalkInfo {
  float distance = 1;
  bool run = 2;
}

Then I've written in kotlin a serializer class for some data in my app.

object WalkInfoSerializer : Serializer<Walker.WalkInfo>{
        override val defaultValue: Walker.WalkInfo
            get() = WalkInfo.getDefaultInstance()

        override fun readFrom(input: InputStream): Walker.WalkInfo {
            try {
                return WalkInfo.parseFrom(input)
            } catch (exception: InvalidProtocolBufferException) {
                throw CorruptionException("Cannot read proto.", exception)
            }

        }

        override fun writeTo(t: Walker.WalkInfo, output: OutputStream) {
            t.writeTo(output)
        }

    }

I've also set up my build.gradle file like this:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id "com.google.protobuf" version "0.8.14"

}
...
dependencies {
    ...
    //DataStore
    implementation "androidx.datastore:datastore:1.0.0-alpha05"
    implementation 'com.google.protobuf:protobuf-javalite:3.14.0'
}

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

    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    option 'lite'
                }
            }
        }
    }
}

Everything works fine, the classes seem to be generated correctly and they even pop up in android studio's autocomplete. The problem is that I can't get the project to compile as I can't seem to find a way to resolve the "Unresolved Reference" for the methods generated (like getDefaultInstance(), parseFrom(), writeTo() Even by writing the full class path it won't work.

android studio autocomplete

Am I missing something? I've tried to play around with build.gradle and the proto file with some settings I found in their documentation but I still couldn't get it to work

Thank you!

1 Answers

Went to sleep, woke up, and knew the answer to my problems. Dumb brain managed to do 1 + 1 during the night.

I realized that if the proto classes are being generated, I should not be defining them myself in kotlin code.

I had a WalkInfo message that generated a WalkInfo class, but I also had a WalkInfo class already with some methods in it. This is what was confusing the compiler.

After further research I realized that classes generated by proto are not even meant to be extended, they are supposed to be just data containers.

What I ended up doing is renaming my message to WalkInfoStorage while also keeping my WalkInfo kotlin class, I'll then be handling generating WalkInfo instances from the serialized data

Related