To register a Java-based publisher to the Configurable Publisher Registry,
you must provide a serialized protobuf FileDescriptorSet containing the
definition of the published protobuf message. For more information, see
Self-describing Messages. However, on-device, only the "lite" version of
the Java protobuf library is available, which lacks the necessary protobuf
reflection capabilities.
To solve this, you can use the Java protobuf descriptor generator tool described on this page to generate the necessary metadata at build time. The tool is a binary you can either build and run manually, or use automatically as part of your Android build.
This tool generates a minimal Java class containing the serialized
FileDescriptorSet representing a protobuf file (with a .proto extension) and
its dependencies. We recommend integrating it into your build using a genrule
in Android.bp. The following example shows how to generate an Example.java
file containing a serialized FileDescriptorSet from an Example.proto file:
genrule {
name: "example_descriptors_gen",
tools: ["sdv_telemetry_sdk_descriptor_generator_java"],
srcs: ["Example.proto"],
out: ["Example.java"],
cmd: "$(location sdv_telemetry_sdk_descriptor_generator_java) " +
"--proto_file $(location Example.proto) " +
"--proto_path $$(dirname $(location Example.proto)) " +
"--message_full_name com.example.ExampleMessage " +
"--output_file $(out) " +
"--output_package com.example " +
"--output_class Example "
}
java_library {
name: "example_descriptors_lib",
srcs: [":example_descriptors_gen"],
}
You can include the resulting java_library in your app and use it in
conjunction with the Configurable Publisher Registry library of the
Telemetry SDK to register your publisher.
You can also build and run the tool manually:
mm sdv_telemetry_sdk_descriptor_generator_java # Build
sdv_telemetry_sdk_descriptor_generator_java --help # Run