[aapt2] Ensure that attributes have declared namespaces when fixing manifest
aapt::ManifestFixer might add attributes from
http://schemas.android.com/apk/res/android when none existed previously.
When that happens, something needs to ensure that the schema has a
declared xmlns prefix, otherwise this:
https://github.com/bazelbuild/bazel/blob/bdb3e97c47188ed4e5b472a3b7393e9588c93011/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java#L560
will crash. It's a bit odd that Bazel converts back and forth between
protobuf and XML, but as long as the fields exist in Resources.proto,
they should be set.
Bug: 133004752
Change-Id: I73fb3f713be34c9f81753fde2fd24c4524656b7d
Tested: aapt2_tests
diff --git a/tools/aapt2/link/ManifestFixer_test.cpp b/tools/aapt2/link/ManifestFixer_test.cpp
index 3f1ee36..3af06f5 100644
--- a/tools/aapt2/link/ManifestFixer_test.cpp
+++ b/tools/aapt2/link/ManifestFixer_test.cpp
@@ -727,8 +727,7 @@
}
TEST_F(ManifestFixerTest, InsertCompileSdkVersions) {
- std::string input = R"(
- <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android" />)";
+ std::string input = R"(<manifest package="com.pkg" />)";
ManifestFixerOptions options;
options.compile_sdk_version = {"28"};
options.compile_sdk_version_codename = {"P"};
@@ -736,6 +735,12 @@
std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
ASSERT_THAT(manifest, NotNull());
+ // There should be a declaration of kSchemaAndroid, even when the input
+ // didn't have one.
+ EXPECT_EQ(manifest->root->namespace_decls.size(), 1);
+ EXPECT_EQ(manifest->root->namespace_decls[0].prefix, "android");
+ EXPECT_EQ(manifest->root->namespace_decls[0].uri, xml::kSchemaAndroid);
+
xml::Attribute* attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersion");
ASSERT_THAT(attr, NotNull());
EXPECT_THAT(attr->value, StrEq("28"));
@@ -782,6 +787,27 @@
EXPECT_THAT(attr->value, StrEq("P"));
}
+TEST_F(ManifestFixerTest, AndroidPrefixAlreadyUsed) {
+ std::string input =
+ R"(<manifest package="com.pkg"
+ xmlns:android="http://schemas.android.com/apk/prv/res/android"
+ android:private_attr="foo" />)";
+ ManifestFixerOptions options;
+ options.compile_sdk_version = {"28"};
+ options.compile_sdk_version_codename = {"P"};
+
+ std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
+ ASSERT_THAT(manifest, NotNull());
+
+ // Make sure that we don't redefine "android".
+ EXPECT_EQ(manifest->root->namespace_decls.size(), 2);
+ EXPECT_EQ(manifest->root->namespace_decls[0].prefix, "android");
+ EXPECT_EQ(manifest->root->namespace_decls[0].uri,
+ "http://schemas.android.com/apk/prv/res/android");
+ EXPECT_EQ(manifest->root->namespace_decls[1].prefix, "android0");
+ EXPECT_EQ(manifest->root->namespace_decls[1].uri, xml::kSchemaAndroid);
+}
+
TEST_F(ManifestFixerTest, UnexpectedElementsInManifest) {
std::string input = R"(
<manifest xmlns:android="http://schemas.android.com/apk/res/android"