ScalaPBOptions and plain Java code generation

431 views
Skip to first unread message

Sören Brunk

unread,
Jun 6, 2016, 5:25:48 AM6/6/16
to ScalaPB
Hi,

I'm using ScalaPB to generate code for messages shared between a Scala backend and an Android app. Works great so far, thanks for creating ScalaPB!

My setup is as follows: I have a multi-module project with a sub-project containing the protobuf files, another one that generates Scala code using ScalaPB from these files and a third one that generates Java code from the same protobuf files using plain sbt-protobuf. Reasons for this split are that I need to generate Java 1.7 bytecode for Android and don't pull any Scala deps. I can post my build.sbt setup if that makes it more clear.

Now I'd like to add customizations on the Scala side (base traits and custom types). I tried to import scalapb.proto and add scalapb options but got compile errors on the Java side because it obviously generates code referring to scalapb packages.

Any idea how to use scalapb options but still be able to generate Java code ignoring those options?

Thanks for your help,
Sören

Nadav Samet

unread,
Jun 6, 2016, 9:25:00 AM6/6/16
to Sören Brunk, ScalaPB
I suggest to add a copy of scalapb.proto to the protos directory of your project, and remove references to the one that ships with ScalaPB. This way the generator will generate Java code for it, and the generated Java code will not have this unmet dependency.

-Nadav

--
You received this message because you are subscribed to the Google Groups "ScalaPB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalapb+u...@googlegroups.com.
To post to this group, send email to sca...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalapb/6726b197-cdbf-4d5f-81d6-9222a3a21fa3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-Nadav

Sören Brunk

unread,
Jun 9, 2016, 10:52:07 AM6/9/16
to ScalaPB, soeren...@gmail.com
Tried your suggestion, but now it complained about missing proto files from protocol buffers itself that are imported  in scalapb.proto

I tried to figure out what ScalaPB does and ended up importing prototbuf-java like so:
"com.google.protobuf" % "protobuf-java" % "3.0.0-beta-3" % JavaPB.protobufConfig

But now I'm getting these errors:

google/protobuf/descriptor.proto:386:3: Expected "required", "optional", or "repeated".
google/protobuf/descriptor.proto:386:12: Expected field name.
scalapb/scalapb.proto: Import "google/protobuf/descriptor.proto" was not found or had errors.
scalapb/scalapb.proto:32:8: "google.protobuf.FileOptions" is not defined.
scalapb/scalapb.proto:46:8: "google.protobuf.MessageOptions" is not defined.
scalapb/scalapb.proto:56:8: "google.protobuf.FieldOptions" is not defined.

Any idea?
Thanks a lot,
Sören

Nadav Samet

unread,
Jun 9, 2016, 11:08:07 AM6/9/16
to Sören Brunk, ScalaPB
Are you using protoc v33? (it's fine to keep syntax = "proto2" for each file). This is needed for ScalaPB 0.5.29 and beyond. Earlier versions would work with protoc v2.6.x. This is an incompitability introduced in protobuf-java 3.0.0-beta-3.

-Nadav

--
You received this message because you are subscribed to the Google Groups "ScalaPB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalapb+u...@googlegroups.com.
To post to this group, send email to sca...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
-Nadav

Sören Brunk

unread,
Jun 9, 2016, 12:18:29 PM6/9/16
to ScalaPB, soeren...@gmail.com
Ah getting closer. Thanks for the hint. I had tried different versions but now I got it working with ScalaPB 0.5.27, protoc v261 and with a dependency on protobuf-java 3.0.0-beta-3 in the java module.

With protoc v3 I'm getting compile errors in the generated Scalapb.java.

I will create a minimal example project for more experimentation soon and post a link here. This might also be a good addition for the examples directory if you're interested.

Sören Brunk

unread,
Jun 9, 2016, 12:20:46 PM6/9/16
to ScalaPB, soeren...@gmail.com
I mean protobuf-java 3.0.0-beta-2 of course. With beta-3 I got the errors mentioned earlier.

Sören Brunk

unread,
Jun 10, 2016, 4:15:33 AM6/10/16
to ScalaPB, soeren...@gmail.com
Here is a minimal working example project: https://github.com/sbrunk/scalapb-java-example

As said, switching to protobuf-java 3.0.0-beta-3 breaks Java code generation.

Sören Brunk

unread,
Jun 10, 2016, 5:04:11 PM6/10/16
to ScalaPB
Got it working now with protoc v3 and therefore with ScalaPB 0.5.29. Had just forgotten to explicitly set protobuf to version 3 in the Java module. I have updated the example project accordingly.
Reply all
Reply to author
Forward
0 new messages