Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 28ed7b9c authored by Rhed Jao's avatar Rhed Jao Committed by Android (Google) Code Review
Browse files

Merge "aapt2: Add a new flag '--revision-code'"

parents 645a8573 b9ccb8a4
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -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.",
+10 −0
Original line number Diff line number Diff line
@@ -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;
  });

+4 −0
Original line number Diff line number Diff line
@@ -60,6 +60,10 @@ struct ManifestFixerOptions {
  // replace_version is set.
  Maybe<std::string> version_code_major_default;

  // The revision code to set if 'android:revisionCode' is not defined in <manifest> or if
  // replace_version is set.
  Maybe<std::string> revision_code_default;

  // The version of the framework being compiled against to set for 'android:compileSdkVersion' in
  // the <manifest> tag.
  Maybe<std::string> compile_sdk_version;
+60 −0
Original line number Diff line number Diff line
@@ -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<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                package="android"
                android:versionCode="0x00000001" />)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<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                  package="android"
                  android:versionCode="0x00000001"
                  android:revisionCode="0x00000002" />)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<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                  package="android"
                  android:versionCode="0x00000001"
                  android:revisionCode="0x00000002" />)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;