diff --git a/tools/aapt2/Android.bp b/tools/aapt2/Android.bp index aca2be1da33db..326c88d470645 100644 --- a/tools/aapt2/Android.bp +++ b/tools/aapt2/Android.bp @@ -57,6 +57,7 @@ cc_defaults { "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 39eb9879f86de..213bdd2372ec0 100644 --- a/tools/aapt2/Main.cpp +++ b/tools/aapt2/Main.cpp @@ -46,12 +46,6 @@ using ::android::base::StringPrintf; 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 @@ class VersionCommand : public Command { } int Action(const std::vector& /* 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 12cf65be05c7e..b2fc08423d348 100644 --- a/tools/aapt2/Resources.proto +++ b/tools/aapt2/Resources.proto @@ -41,6 +41,12 @@ message Source { 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 @@ message ResourceTable { // The 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 58e1e6ee35ca9..a54822b20302e 100644 --- a/tools/aapt2/format/proto/ProtoSerialize.cpp +++ b/tools/aapt2/format/proto/ProtoSerialize.cpp @@ -327,6 +327,9 @@ static void SerializeOverlayableItemToPb(const OverlayableItem& overlayable_item 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 overlayables; for (const std::unique_ptr& package : table.packages) { diff --git a/tools/aapt2/util/Util.cpp b/tools/aapt2/util/Util.cpp index 59b7fff0f9e3e..37ce65e4fe5b1 100644 --- a/tools/aapt2/util/Util.cpp +++ b/tools/aapt2/util/Util.cpp @@ -21,13 +21,15 @@ #include #include +#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 @@ Maybe GetFullyQualifiedClassName(const StringPiece& package, 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 c6e8e6ef7619c..a956957eace86 100644 --- a/tools/aapt2/util/Util.h +++ b/tools/aapt2/util/Util.h @@ -98,6 +98,12 @@ bool IsAndroidSplitName(const android::StringPiece& str); Maybe 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 std::enable_if::value, int>::type compare(const T& a, const T& b) { if (a < b) {