Add build number to aapt2 version

This changes adds the build id to the version printed by "aapt2
version". This change also adds a field to the ResourceTable proto that
specifies the fingerprints of tools used to build the table.

Bug: 123663089
Test: manual
Change-Id: Ifaf33c1e506b68e9f1d921fdbeddf36485e65790
diff --git a/tools/aapt2/Android.bp b/tools/aapt2/Android.bp
index aca2be1..326c88d 100644
--- a/tools/aapt2/Android.bp
+++ b/tools/aapt2/Android.bp
@@ -57,6 +57,7 @@
         "libbase",
         "libprotobuf-cpp-lite",
         "libz",
+        "libbuildversion",
     ],
     stl: "libc++_static",
     group_static_libs: true,
diff --git a/tools/aapt2/Main.cpp b/tools/aapt2/Main.cpp
index 39eb987..213bdd2 100644
--- a/tools/aapt2/Main.cpp
+++ b/tools/aapt2/Main.cpp
@@ -46,12 +46,6 @@
 
 namespace aapt {
 
-// DO NOT UPDATE, this is more of a marketing version.
-static const char* sMajorVersion = "2";
-
-// Update minor version whenever a feature or flag is added.
-static const char* sMinorVersion = "19";
-
 /** Prints the version information of AAPT2. */
 class VersionCommand : public Command {
  public:
@@ -60,8 +54,7 @@
   }
 
   int Action(const std::vector<std::string>& /* args */) override {
-    std::cerr << StringPrintf("Android Asset Packaging Tool (aapt) %s:%s", sMajorVersion,
-                              sMinorVersion)
+    std::cerr << StringPrintf("%s %s", util::GetToolName(), util::GetToolFingerprint().c_str())
               << std::endl;
     return 0;
   }
diff --git a/tools/aapt2/Resources.proto b/tools/aapt2/Resources.proto
index a2fd7c6..ad068e3 100644
--- a/tools/aapt2/Resources.proto
+++ b/tools/aapt2/Resources.proto
@@ -41,6 +41,12 @@
   SourcePosition position = 2;
 }
 
+// The name and version fingerprint of a build tool.
+message ToolFingerprint {
+  string tool = 1;
+  string version = 2;
+}
+
 // Top level message representing a resource table.
 message ResourceTable {
   // The string pool containing source paths referenced throughout the resource table. This does
@@ -52,6 +58,9 @@
 
   // The <overlayable> declarations within the resource table.
   repeated Overlayable overlayable = 3;
+
+  // The version fingerprints of the tools that built the resource table.
+  repeated ToolFingerprint tool_fingerprint = 4;
 }
 
 // A package ID in the range [0x00, 0xff].
diff --git a/tools/aapt2/format/proto/ProtoSerialize.cpp b/tools/aapt2/format/proto/ProtoSerialize.cpp
index eb2b1a2..6564e0b 100644
--- a/tools/aapt2/format/proto/ProtoSerialize.cpp
+++ b/tools/aapt2/format/proto/ProtoSerialize.cpp
@@ -321,6 +321,9 @@
 void SerializeTableToPb(const ResourceTable& table, pb::ResourceTable* out_table,
                         IDiagnostics* diag) {
   StringPool source_pool;
+  pb::ToolFingerprint* pb_fingerprint = out_table->add_tool_fingerprint();
+  pb_fingerprint->set_tool(util::GetToolName());
+  pb_fingerprint->set_version(util::GetToolFingerprint());
 
   std::vector<Overlayable*> overlayables;
   for (const std::unique_ptr<ResourceTablePackage>& package : table.packages) {
diff --git a/tools/aapt2/util/Util.cpp b/tools/aapt2/util/Util.cpp
index 59b7fff..37ce65e 100644
--- a/tools/aapt2/util/Util.cpp
+++ b/tools/aapt2/util/Util.cpp
@@ -21,13 +21,15 @@
 #include <string>
 #include <vector>
 
+#include "android-base/stringprintf.h"
 #include "androidfw/StringPiece.h"
-#include "utils/Unicode.h"
+#include "build/version.h"
 
 #include "text/Unicode.h"
 #include "text/Utf8Iterator.h"
 #include "util/BigBuffer.h"
 #include "util/Maybe.h"
+#include "utils/Unicode.h"
 
 using ::aapt::text::Utf8Iterator;
 using ::android::StringPiece;
@@ -200,6 +202,24 @@
   return result;
 }
 
+const char* GetToolName() {
+  static const char* const sToolName = "Android Asset Packaging Tool (aapt)";
+  return sToolName;
+}
+
+std::string GetToolFingerprint() {
+  // DO NOT UPDATE, this is more of a marketing version.
+  static const char* const sMajorVersion = "2";
+
+  // Update minor version whenever a feature or flag is added.
+  static const char* const sMinorVersion = "19";
+
+  // The build id of aapt2 binary.
+  static const std::string sBuildId = android::build::GetBuildNumber();
+
+  return android::base::StringPrintf("%s.%s-%s", sMajorVersion, sMinorVersion, sBuildId.c_str());
+}
+
 static size_t ConsumeDigits(const char* start, const char* end) {
   const char* c = start;
   for (; c != end && *c >= '0' && *c <= '9'; c++) {
diff --git a/tools/aapt2/util/Util.h b/tools/aapt2/util/Util.h
index c6e8e6e..a956957 100644
--- a/tools/aapt2/util/Util.h
+++ b/tools/aapt2/util/Util.h
@@ -98,6 +98,12 @@
 Maybe<std::string> GetFullyQualifiedClassName(const android::StringPiece& package,
                                               const android::StringPiece& class_name);
 
+// Retrieves the formatted name of aapt2.
+const char* GetToolName();
+
+// Retrieves the build fingerprint of aapt2.
+std::string GetToolFingerprint();
+
 template <typename T>
 typename std::enable_if<std::is_arithmetic<T>::value, int>::type compare(const T& a, const T& b) {
   if (a < b) {