Lauro182
Lauro182

Reputation: 1697

Protobuf - toByteArray method not there on my class

First of all, yes, I have read this thread, Protobuf - Missing toByteArray()?, that didn't solve my issue.

I installed protobuf following this exact steps described here: https://gist.github.com/sofyanhadia/37787e5ed098c97919b8c593f0ec44d8.

My proto file is very simple:

syntax = "proto3";
package com.mypackage.protobuf;

message MensajeSMS {
  string telefono = 1;
  string mensaje = 2;
}

I go to my proto route and protocompile it using:

protoc --java_out=/home/myhome/myproject/myprotoclass/ MensajeSMS.proto 

The class is indeed created, it has several methods, but not all specified here: https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/MessageLite

Like some guy says it should be:

"The toByteArray() method is certainly there as it's defined in the Message interface for all message types: https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/MessageLite.html"

Said here: https://github.com/google/protobuf/issues/2643

So, I'm clearly doing something wrong. How can I make protoc to generate my class with toByteArray method?

EDIT

Here's the code:

MensajeSMS xxx = MensajeSMS.newBuilder()
                            .setTelefono(mensajeSMS.getTelefono())
                            .setMensaje(mensajeSMS.getMensaje())
                            .build();

byte[] mybyte = xxx.toByteArray(); //Here I get cannot resolve method

Upvotes: 1

Views: 1233

Answers (1)

Lauro182
Lauro182

Reputation: 1697

So, I found the answer, and it wasn't code related. More data was indeed needed, I thought that everything else I was doing wasn't relevant.

The problem is I was compiling my project into a jar and using it in another project, BUT I was compiling using

./gradlew build 

and not

./gradlew buidl fatJar 

which is what I need to include all dependencies in my jar.

I just hope this helps someone elses. My bad.

Upvotes: 1

Related Questions