Enable using lite protos for Android.
Lite is the new preferred proto runtime on Android, for two reasons:
- It lends itself to better proguard optimizing
- There is only one flavor, so there is no risk in including
proto libraries with overlapping definitions that use different
flavors
This CL adds a generate_nano argument to the proto_java_library build
rule. This argument is default false, so new proto libraries will use
lite by default. However, existing libraries will be migrated in a
follow-up CL, so this change sets generate_nano to true for those
libraries.
It also adds the android_library rule that contains the runtime
library for lite protos.
For an example conversion CL for a proto target, see:
https://chromium-review.googlesource.com/c/chromium/src/+/757103
Bug: 782237
Change-Id: I8100e70c38d41add9068e493ca2a5822f7025213
Reviewed-on: https://chromium-review.googlesource.com/757134
Commit-Queue: agrieve <agrieve@chromium.org>
Reviewed-by: Jochen Eisinger <jochen@chromium.org>
Reviewed-by: Tommy Nyquist <nyquist@chromium.org>
Reviewed-by: agrieve <agrieve@chromium.org>
Reviewed-by: Doug Steedman <dougsteed@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517268}
CrOS-Libchrome-Original-Commit: cc12c7a9522169c079a0c0c6ad446088f6cbea1d
diff --git a/build/protoc_java.py b/build/protoc_java.py
index 46fa820..426d684 100755
--- a/build/protoc_java.py
+++ b/build/protoc_java.py
@@ -33,6 +33,8 @@
help="Path to output directory for java files.")
parser.add_option("--srcjar", help="Path to output srcjar.")
parser.add_option("--stamp", help="File to touch on success.")
+ parser.add_option("--lite",
+ help="Use to generate lite protos.", action='store_true')
options, args = parser.parse_args(argv)
build_utils.CheckOptions(options, parser, ['protoc', 'proto_path'])
@@ -41,10 +43,22 @@
return 1
with build_utils.TempDir() as temp_dir:
- # Specify arguments to the generator.
- generator_args = ['optional_field_style=reftypes',
- 'store_unknown_fields=true']
- out_arg = '--javanano_out=' + ','.join(generator_args) + ':' + temp_dir
+ if options.lite:
+ out_arg = '--java_out=' + temp_dir
+
+ # Check if all proto files (which are listed in the args) are opting to
+ # use the lite runtime, otherwise we'd have to include the much heavier
+ # regular proto runtime in Chrome.
+ for proto_file in args:
+ if not 'LITE_RUNTIME' in open(proto_file).read():
+ raise Exception(
+ 'Chrome only supports lite protos. Please add "optimize_for = '
+ 'LITE_RUNTIME" to your proto file to enable the lite runtime.')
+ else:
+ # Specify arguments to the generator.
+ generator_args = ['optional_field_style=reftypes',
+ 'store_unknown_fields=true']
+ out_arg = '--javanano_out=' + ','.join(generator_args) + ':' + temp_dir
# Generate Java files using protoc.
build_utils.CheckOutput(
[options.protoc, '--proto_path', options.proto_path, out_arg]