From b9ccb8a49b47844c97bb3adab24ca0197120e319 Mon Sep 17 00:00:00 2001 From: Rhed Jao Date: Mon, 30 Nov 2020 21:42:16 +0800 Subject: [PATCH] aapt2: Add a new flag '--revision-code' A new flag to inject revisionCode into the manifest for cts splits tests. Bug: 174338944 Test: atest aapt2_tests Change-Id: If5a089f37233f53af3012ca5eab17fab21eafd9c --- tools/aapt2/cmd/Link.h | 5 ++- tools/aapt2/link/ManifestFixer.cpp | 10 +++++ tools/aapt2/link/ManifestFixer.h | 4 ++ tools/aapt2/link/ManifestFixer_test.cpp | 60 +++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/tools/aapt2/cmd/Link.h b/tools/aapt2/cmd/Link.h index 852b1244cd6e1..768b4b2c7bfd3 100644 --- a/tools/aapt2/cmd/Link.h +++ b/tools/aapt2/cmd/Link.h @@ -190,8 +190,11 @@ class LinkCommand : public Command { AddOptionalFlag("--version-name", "Version name to inject into the AndroidManifest.xml if none is present.", &options_.manifest_fixer_options.version_name_default); + AddOptionalFlag("--revision-code", + "Revision code (integer) to inject into the AndroidManifest.xml if none is\n" + "present.", &options_.manifest_fixer_options.revision_code_default); AddOptionalSwitch("--replace-version", - "If --version-code and/or --version-name are specified, these\n" + "If --version-code, --version-name, and/or --revision-code are specified, these\n" "values will replace any value already in the manifest. By\n" "default, nothing is changed if the manifest already defines\n" "these attributes.", diff --git a/tools/aapt2/link/ManifestFixer.cpp b/tools/aapt2/link/ManifestFixer.cpp index c03661ca23664..8abd9dec56be3 100644 --- a/tools/aapt2/link/ManifestFixer.cpp +++ b/tools/aapt2/link/ManifestFixer.cpp @@ -367,6 +367,16 @@ bool ManifestFixer::BuildRules(xml::XmlActionExecutor* executor, } } + if (options_.revision_code_default) { + if (options_.replace_version) { + el->RemoveAttribute(xml::kSchemaAndroid, "revisionCode"); + } + if (el->FindAttribute(xml::kSchemaAndroid, "revisionCode") == nullptr) { + el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "revisionCode", + options_.revision_code_default.value()}); + } + } + return true; }); diff --git a/tools/aapt2/link/ManifestFixer.h b/tools/aapt2/link/ManifestFixer.h index ec4367b450fb3..34ad8d586df1a 100644 --- a/tools/aapt2/link/ManifestFixer.h +++ b/tools/aapt2/link/ManifestFixer.h @@ -60,6 +60,10 @@ struct ManifestFixerOptions { // replace_version is set. Maybe version_code_major_default; + // The revision code to set if 'android:revisionCode' is not defined in or if + // replace_version is set. + Maybe revision_code_default; + // The version of the framework being compiled against to set for 'android:compileSdkVersion' in // the tag. Maybe compile_sdk_version; diff --git a/tools/aapt2/link/ManifestFixer_test.cpp b/tools/aapt2/link/ManifestFixer_test.cpp index 781ff7b51637c..432f10bdab972 100644 --- a/tools/aapt2/link/ManifestFixer_test.cpp +++ b/tools/aapt2/link/ManifestFixer_test.cpp @@ -445,6 +445,66 @@ TEST_F(ManifestFixerTest, ReplaceVersionNameAndCode) { EXPECT_THAT(attr->value, StrEq("0x20000000")); } +TEST_F(ManifestFixerTest, UseDefaultRevisionCode) { + ManifestFixerOptions options; + options.revision_code_default = std::string("0x10000000"); + + std::unique_ptr doc = VerifyWithOptions(R"EOF( + )EOF", + options); + ASSERT_THAT(doc, NotNull()); + + xml::Element* manifest_el = doc->root.get(); + ASSERT_THAT(manifest_el, NotNull()); + + xml::Attribute* attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode"); + ASSERT_THAT(attr, NotNull()); + EXPECT_THAT(attr->value, StrEq("0x10000000")); +} + +TEST_F(ManifestFixerTest, DontUseDefaultRevisionCode) { + ManifestFixerOptions options; + options.revision_code_default = std::string("0x10000000"); + + std::unique_ptr doc = VerifyWithOptions(R"EOF( + )EOF", + options); + ASSERT_THAT(doc, NotNull()); + + xml::Element* manifest_el = doc->root.get(); + ASSERT_THAT(manifest_el, NotNull()); + + xml::Attribute* attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode"); + ASSERT_THAT(attr, NotNull()); + EXPECT_THAT(attr->value, StrEq("0x00000002")); +} + +TEST_F(ManifestFixerTest, ReplaceRevisionCode) { + ManifestFixerOptions options; + options.replace_version = true; + options.revision_code_default = std::string("0x10000000"); + + std::unique_ptr doc = VerifyWithOptions(R"EOF( + )EOF", + options); + ASSERT_THAT(doc, NotNull()); + + xml::Element* manifest_el = doc->root.get(); + ASSERT_THAT(manifest_el, NotNull()); + + xml::Attribute* attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode"); + ASSERT_THAT(attr, NotNull()); + EXPECT_THAT(attr->value, StrEq("0x10000000")); +} + TEST_F(ManifestFixerTest, ReplaceVersionName) { ManifestFixerOptions options; options.replace_version = true;