Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle can't find protobuf generated class (Android DataStore)

I introduced the dependency by referring to the documentation

implementation "androidx.datastore:datastore:1.0.0"

then defined the schema app/src/main/proto/.proto

syntax = "proto3";

option java_package = "com.freedom.android.config.work";
option java_multiple_files = true;

message WorkItemVO {
  
  bool enabled = 1;
  
  string title = 2;
  
  string repeat_interval = 3;
  string repeat_interval_timeUnit = 4;
  
  string last_update_time = 5;
  string last_update_result = 6;
}

after app build, But build/generated/source/proto/ did not generate WorkItemVO class files.

Can you tell me what I'm missing?

like image 227
SageJustus Avatar asked Jan 25 '26 14:01

SageJustus


1 Answers

Android dev guide is focusing on the direct dependencies and how to use proto store and do not mention the generation of the protobuf java classes. A complete example is provided on a codelab Working with Proto DataStore

In this codelab you can see that a specific configuration is needed on gradle:

plugins {
    ...
    id "com.google.protobuf" version "0.8.17"
}

dependencies {
    implementation  "androidx.datastore:datastore:1.0.0"
    implementation  "com.google.protobuf:protobuf-javalite:3.18.0"
    ...
}

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

    // Generates the java Protobuf-lite code for the Protobufs in this project. See
    // https://github.com/google/protobuf-gradle-plugin#customizing-protobuf-compilation
    // for more information.
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    option 'lite'
                }
            }
        }
    }
}

Note that the version numbers need to be updated.

For the ones like me that are using gradle version catalog with kotlin syntax you have to act on 3 files: lib.versions.yml

[versions]
protobuf-javalite = "3.23.3"
protobuf-plugin = "0.9.3"

[libraries]
protobuf-javalite = {module = "com.google.protobuf:protobuf-javalite", version.ref = "protobuf-javalite"}

[plugins]
protobuf = { id = "com.google.protobuf", version.ref = "protobuf-plugin"}

build.gradle.kts (project)

plugins {
  alias(libs.plugins.protobuf) apply false
}

build.gradle.kts (app)

plugins {
    ...
    alias(libs.plugins.protobuf)
}
dependencies {
   ...
    implementation(libs.protobuf.javalite)
}
protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.23.2"
    }

    // Generates the java Protobuf-lite code for the Protobufs in this project. See
    // https://github.com/google/protobuf-gradle-plugin#customizing-protobuf-compilation
    // for more information.
    generateProtoTasks {
        all().forEach { task ->
            task.builtins {
                create("java") {
                    option("lite")
                }
            }
        }
    }
}

Once done sync your gradle project and build. You should see your class properly created.

Hope it helps.

like image 70
arkrad87 Avatar answered Jan 28 '26 08:01

arkrad87



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!