How to use protobuf-java in gradle with android

1,713 views
Skip to first unread message

Carlos Pages

unread,
Oct 9, 2019, 4:22:44 AM10/9/19
to Protocol Buffers
Hi,


( I'm not an Android expert so I apologize in advance for any mistake )

I know the recommendation is to use protobuf-lite but I need to convert my protobuf object to JSON, so I decided to use protobuf-java instead, and having access to the JsonFormat ( https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/util/JsonFormat ), if it's posible.

Could you guide me how to set it up in gradle.

I added the dependency 

dependencies {

...
implementation 'com.google.protobuf:protobuf-java:3.9.2'



But I'm having a bit more of trouble set it up the protobuf script:

protobuf {
    protoc
{ artifact = 'com.google.protobuf:protoc:3.6.1' }
    plugins
{
        javalite
{ artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" }
        grpc
{
            artifact
= "io.grpc:protoc-gen-grpc-java:${grpcVersion}" // CURRENT_GRPC_VERSION
        }
   
}
    generateProtoTasks
{
        all
().each { task ->
            task
.plugins {
                javalite
{}
                grpc
{ // Options added to --grpc_out
                    option 'lite'
                }
           
}
       
}
   
}
}

How can I convert it to use protobuf-java instead. 

I tried to replace the lite library for java, but with no success.

If you could point me to any documentation to manage the protobuf script in gradle, it would be very useful.

Thanks

Carlos Pages

unread,
Oct 9, 2019, 6:56:46 AM10/9/19
to Protocol Buffers
This is what I tried so far:

protobuf {
    protoc
{ artifact = "com.google.protobuf:protoc:$protobufVersion" }
    plugins
{

        grpc
{
            artifact
= "io.grpc:protoc-gen-grpc-java:${grpcVersion}" // CURRENT_GRPC_VERSION
        }
   
}
    generateProtoTasks
{
        all
().each { task ->
            task
.plugins {

                grpc
{ }
           
}
       
}
   
}
}


But the script doesn't generate the protobuf files, it only generates the grpc files.

Carlos Pages

unread,
Oct 10, 2019, 4:43:53 AM10/10/19
to Protocol Buffers
Ok I think I got it working, this is the solution I got, just if someone gets stack like me:


// GRPC
implementation "io.grpc:grpc-okhttp:${grpcVersion}" // CURRENT_GRPC_VERSION
implementation "io.grpc:grpc-stub:${grpcVersion}" // CURRENT_GRPC_VERSION
implementation "io.grpc:grpc-protobuf:$grpcVersion"

// Protobuf
implementation "com.google.protobuf:protobuf-java:$protobufVersion"
implementation "com.google.protobuf:protobuf-java-util:$protobufVersion"



protobuf
{
    protoc
{ artifact = "com.google.protobuf:protoc:$protobufVersion" }
    plugins
{

        grpc
{
            artifact
= "io.grpc:protoc-gen-grpc-java:${grpcVersion}" // CURRENT_GRPC_VERSION
        }
   
}
    generateProtoTasks
{
        all
().each { task ->

            task
.builtins {
                java
{ }
           
}
            task
.plugins {
                grpc
{ }
           
}
       
}
   
}
}

Now I can use the JsonFormat: 

val jsonPrinter = JsonFormat.printer().includingDefaultValueFields()


return jsonPrinter.print(myProtobufObject)


sources: 



Unfortunately this solution is not compatible with solutions older than Android Oreo:



On Wednesday, 9 October 2019 09:22:44 UTC+1, Carlos Pages wrote:
Reply all
Reply to author
Forward
0 new messages