AAPT2: Version code major flag

Adds flag --version-code-major that injects android:versionCodeMajor
into the manifest if none is present or when overriding.

Bug: 109883459
Test: aapt2_tests
Change-Id: I10b27abb8fa8a99a41976c42c4d684b99978b4f9
This commit is contained in:
Ryan Mitchell
2018-07-31 14:59:25 -07:00
parent 0b015de77e
commit 704090e02a
4 changed files with 177 additions and 96 deletions

View File

@@ -159,8 +159,10 @@ class LinkCommand : public Command {
&options_.manifest_fixer_options.target_sdk_version_default); &options_.manifest_fixer_options.target_sdk_version_default);
AddOptionalFlag("--version-code", AddOptionalFlag("--version-code",
"Version code (integer) to inject into the AndroidManifest.xml if none is\n" "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
"present.", "present.", &options_.manifest_fixer_options.version_code_default);
&options_.manifest_fixer_options.version_code_default); AddOptionalFlag("--version-code-major",
"Version code major (integer) to inject into the AndroidManifest.xml if none is\n"
"present.", &options_.manifest_fixer_options.version_code_major_default);
AddOptionalFlag("--version-name", AddOptionalFlag("--version-name",
"Version name to inject into the AndroidManifest.xml if none is present.", "Version name to inject into the AndroidManifest.xml if none is present.",
&options_.manifest_fixer_options.version_name_default); &options_.manifest_fixer_options.version_name_default);

View File

@@ -282,6 +282,17 @@ bool ManifestFixer::BuildRules(xml::XmlActionExecutor* executor,
} }
} }
if (options_.version_code_major_default) {
if (options_.replace_version) {
el->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor");
}
if (el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor") == nullptr) {
el->attributes.push_back(
xml::Attribute{xml::kSchemaAndroid, "versionCodeMajor",
options_.version_code_major_default.value()});
}
}
if (el->FindAttribute("", "platformBuildVersionCode") == nullptr) { if (el->FindAttribute("", "platformBuildVersionCode") == nullptr) {
auto versionCode = el->FindAttribute(xml::kSchemaAndroid, "versionCode"); auto versionCode = el->FindAttribute(xml::kSchemaAndroid, "versionCode");
if (versionCode != nullptr) { if (versionCode != nullptr) {

View File

@@ -52,6 +52,10 @@ struct ManifestFixerOptions {
// replace_version is set. // replace_version is set.
Maybe<std::string> version_code_default; Maybe<std::string> version_code_default;
// The version code to set if 'android:versionCodeMajor' is not defined in <manifest> or if
// replace_version is set.
Maybe<std::string> version_code_major_default;
// The version of the framework being compiled against to set for 'android:compileSdkVersion' in // The version of the framework being compiled against to set for 'android:compileSdkVersion' in
// the <manifest> tag. // the <manifest> tag.
Maybe<std::string> compile_sdk_version; Maybe<std::string> compile_sdk_version;

View File

@@ -329,6 +329,7 @@ TEST_F(ManifestFixerTest, UseDefaultVersionNameAndCode) {
ManifestFixerOptions options; ManifestFixerOptions options;
options.version_name_default = std::string("Beta"); options.version_name_default = std::string("Beta");
options.version_code_default = std::string("0x10000000"); options.version_code_default = std::string("0x10000000");
options.version_code_major_default = std::string("0x20000000");
std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF( std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
@@ -347,136 +348,199 @@ TEST_F(ManifestFixerTest, UseDefaultVersionNameAndCode) {
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode"); attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
ASSERT_THAT(attr, NotNull()); ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x10000000")); EXPECT_THAT(attr->value, StrEq("0x10000000"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x20000000"));
} }
TEST_F(ManifestFixerTest, DontUseDefaultVersionNameAndCode) { TEST_F(ManifestFixerTest, DontUseDefaultVersionNameAndCode) {
ManifestFixerOptions options; ManifestFixerOptions options;
options.version_name_default = std::string("Beta"); options.version_name_default = std::string("Beta");
options.version_code_default = std::string("0x10000000"); options.version_code_default = std::string("0x10000000");
options.version_code_major_default = std::string("0x20000000");
std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF( std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android" package="android"
android:versionCode="0x20000000" android:versionCode="0x00000001"
android:versionName="Alpha" />)EOF", android:versionCodeMajor="0x00000002"
options); android:versionName="Alpha" />)EOF",
ASSERT_THAT(doc, NotNull()); options);
ASSERT_THAT(doc, NotNull());
xml::Element* manifest_el = doc->root.get(); xml::Element* manifest_el = doc->root.get();
ASSERT_THAT(manifest_el, NotNull()); ASSERT_THAT(manifest_el, NotNull());
xml::Attribute* attr = xml::Attribute* attr =
manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName"); manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
ASSERT_THAT(attr, NotNull()); ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("Alpha")); EXPECT_THAT(attr->value, StrEq("Alpha"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode"); attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
ASSERT_THAT(attr, NotNull()); ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x20000000")); EXPECT_THAT(attr->value, StrEq("0x00000001"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x00000002"));
} }
TEST_F(ManifestFixerTest, ReplaceVersionNameAndCode) { TEST_F(ManifestFixerTest, ReplaceVersionNameAndCode) {
ManifestFixerOptions options; ManifestFixerOptions options;
options.replace_version = true; options.replace_version = true;
options.version_name_default = std::string("Beta"); options.version_name_default = std::string("Beta");
options.version_code_default = std::string("0x10000000"); options.version_code_default = std::string("0x10000000");
options.version_code_major_default = std::string("0x20000000");
std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF( std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android" package="android"
android:versionCode="0x20000000" android:versionCode="0x00000001"
android:versionName="Alpha" />)EOF", android:versionCodeMajor="0x00000002"
options); android:versionName="Alpha" />)EOF",
ASSERT_THAT(doc, NotNull()); options);
ASSERT_THAT(doc, NotNull());
xml::Element* manifest_el = doc->root.get(); xml::Element* manifest_el = doc->root.get();
ASSERT_THAT(manifest_el, NotNull()); ASSERT_THAT(manifest_el, NotNull());
xml::Attribute* attr = xml::Attribute* attr =
manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName"); manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
ASSERT_THAT(attr, NotNull()); ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("Beta")); EXPECT_THAT(attr->value, StrEq("Beta"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode"); attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
ASSERT_THAT(attr, NotNull()); ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x10000000")); EXPECT_THAT(attr->value, StrEq("0x10000000"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x20000000"));
} }
TEST_F(ManifestFixerTest, ReplaceVersionName) { TEST_F(ManifestFixerTest, ReplaceVersionName) {
ManifestFixerOptions options; ManifestFixerOptions options;
options.replace_version = true; options.replace_version = true;
options.version_name_default = std::string("Beta"); options.version_name_default = std::string("Beta");
std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android"
android:versionCode="0x20000000"
android:versionName="Alpha" />)EOF",
options);
ASSERT_THAT(doc, NotNull());
xml::Element* manifest_el = doc->root.get(); std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
ASSERT_THAT(manifest_el, NotNull()); <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android"
android:versionCode="0x00000001"
android:versionCodeMajor="0x00000002"
android:versionName="Alpha" />)EOF",
options);
ASSERT_THAT(doc, NotNull());
xml::Attribute* attr = xml::Element* manifest_el = doc->root.get();
manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName"); ASSERT_THAT(manifest_el, NotNull());
ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("Beta"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode"); xml::Attribute* attr =
ASSERT_THAT(attr, NotNull()); manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
EXPECT_THAT(attr->value, StrEq("0x20000000")); ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("Beta"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x00000001"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x00000002"));
} }
TEST_F(ManifestFixerTest, ReplaceVersionCode) { TEST_F(ManifestFixerTest, ReplaceVersionCode) {
ManifestFixerOptions options; ManifestFixerOptions options;
options.replace_version = true; options.replace_version = true;
options.version_code_default = std::string("0x10000000"); options.version_code_default = std::string("0x10000000");
std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF( 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:versionCodeMajor="0x00000002"
android:versionName="Alpha" />)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, "versionName");
ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("Alpha"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x10000000"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x00000002"));
}
TEST_F(ManifestFixerTest, ReplaceVersionCodeMajor) {
ManifestFixerOptions options;
options.replace_version = true;
options.version_code_major_default = std::string("0x20000000");
std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android" package="android"
android:versionCode="0x20000000" android:versionCode="0x00000001"
android:versionName="Alpha" />)EOF", android:versionCodeMajor="0x00000002"
options); android:versionName="Alpha" />)EOF",
ASSERT_THAT(doc, NotNull()); options);
ASSERT_THAT(doc, NotNull());
xml::Element* manifest_el = doc->root.get(); xml::Element* manifest_el = doc->root.get();
ASSERT_THAT(manifest_el, NotNull()); ASSERT_THAT(manifest_el, NotNull());
xml::Attribute* attr = xml::Attribute* attr =
manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName"); manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
ASSERT_THAT(attr, NotNull()); ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("Alpha")); EXPECT_THAT(attr->value, StrEq("Alpha"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode"); attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
ASSERT_THAT(attr, NotNull()); ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x10000000")); EXPECT_THAT(attr->value, StrEq("0x00000001"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x20000000"));
} }
TEST_F(ManifestFixerTest, DontReplaceVersionNameOrCode) { TEST_F(ManifestFixerTest, DontReplaceVersionNameOrCode) {
ManifestFixerOptions options; ManifestFixerOptions options;
options.replace_version = true; options.replace_version = true;
std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF( std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android" package="android"
android:versionCode="0x20000000" android:versionCode="0x00000001"
android:versionName="Alpha" />)EOF", android:versionCodeMajor="0x00000002"
options); android:versionName="Alpha" />)EOF",
ASSERT_THAT(doc, NotNull()); options);
ASSERT_THAT(doc, NotNull());
xml::Element* manifest_el = doc->root.get(); xml::Element* manifest_el = doc->root.get();
ASSERT_THAT(manifest_el, NotNull()); ASSERT_THAT(manifest_el, NotNull());
xml::Attribute* attr = xml::Attribute* attr =
manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName"); manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
ASSERT_THAT(attr, NotNull()); ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("Alpha")); EXPECT_THAT(attr->value, StrEq("Alpha"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode"); attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
ASSERT_THAT(attr, NotNull()); ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x20000000")); EXPECT_THAT(attr->value, StrEq("0x00000001"));
attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("0x00000002"));
} }
TEST_F(ManifestFixerTest, EnsureManifestAttributesAreTyped) { TEST_F(ManifestFixerTest, EnsureManifestAttributesAreTyped) {