Java Proto Names
This document contains information on what the fully-qualified Java name of a proto is, based on the different proto options. This name corresponds to the package you need to import to use that message.
NOTE: The java_package
and java_alt_api_package
options are interpreted
relative to the API indicated by java_api_version
. For example, if
java_api_version
is 1, then the proto1 package will be java_package
and the
proto2 package (the “alternative” API) will be java_alt_api_package
. And if
java_api_version
is 2, then java_package
determines the proto2 package and
java_alt_api_package
determines the proto1 package.
Immutable API Message Names
The names for protos generated by the immutable API (java_proto_library
BUILD
target) are listed in the following table.
java_api_version | java_multiple_files | java_alt_api_package | java_package | java_outer_classname | Generated full message name |
---|---|---|---|---|---|
1 | true | Defined | - | $java_alt_api_package.$message | |
1 | true | Not defined | Not defined | com.google.protos.$package.proto2api.$message | |
1 | true | Not defined | Defined | $java_package.proto2api.$message | |
1 | false | Defined | - | Not defined | $java_alt_api_package.$derived_outer_class.$message |
1 | false | Defined | - | Defined | $java_alt_api_package.$java_outer_classname.$message |
1 | false | Not defined | Not defined | Not defined | com.google.protos.$package.proto2api.$derived_outer_class.$message |
1 | false | Not defined | Not defined | Defined | com.google.protos.$package.proto2api.$java_outer_classname.$message |
1 | false | Not defined | Defined | Not defined | $java_package.proto2api.$derived_outer_class.$message |
1 | false | Not defined | Defined | Defined | $java_package.proto2api.$java_outer_classname.$message |
2 | true | - | Not defined | - | com.google.protos.$package.$message |
2 | true | - | Defined | - | $java_package.$message |
2 | false | - | Not defined | Not defined | com.google.protos.$package.$derived_outer_class.$message |
2 | false | - | Not defined | Defined | com.google.protos.$package.$java_outer_classname.$message |
2 | false | - | Defined | Not defined | $java_package.$derived_outer_class.$message |
2 | false | - | Defined | Defined | $java_package.$java_outer_classname.$message |
Legend
- means either setting or not setting the option will not change the generated full message name.
$message
is the actual name of the proto message.$package
is the name of the proto package. This is the name specified by thepackage
directive in the proto file, which is usually at the top of the file.$derived_outer_class
is a name generated from the proto file name. Generally it’s computed by removing punctuation from the file name and converting it to CamelCase. For example, if the proto isfoo_bar.proto
, the$derived_outer_class
value isFooBar
.If the generated class name would be the same as one of the messages defined in the proto file,
derived_outer_class
hasOuterClass
appended to it. For example, if the proto isfoo_bar.proto
and contains aFooBar
message, the$derived_outer_class
value isFooBarOuterClass
. The same is true when using the v1 API, whether or not the class name would be the same as one of the messages defined.All other
$names
are the values of the corresponding proto2 file options defined in the proto file.
Recommendation
The recommended option to use is:
option java_multiple_files = true;
With java_multiple_files = true
, the generated Java class for each message
will be placed in a separate .java
file. This makes it much easier to move
messages from one .proto
file to another. There is also an outer Java class
generated for the .proto
file itself. (The legend above explains how this
outer class name is generated.)
The java_api_version
option defaults to 2
, but you can manually set it to
1
when necessary.