Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 642fdce

Browse files
committed
Add --no-compile-sdk-metadata switch to AAPT2
This switch suppresses output of compile SDK-related attributes in AndroidManifest.xml, including android:compileSdkVersion and platformBuildVersion. Fixes: 278115511 Test: ManifestFixer_test Merged-In: I552e50802a328c2318c9f261e30beadcbca5fd29 Change-Id: I552e50802a328c2318c9f261e30beadcbca5fd29
1 parent 956e4f9 commit 642fdce

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

tools/aapt2/cmd/Link.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ class LinkCommand : public Command {
206206
AddOptionalFlag("--compile-sdk-version-name",
207207
"Version name to inject into the AndroidManifest.xml if none is present.",
208208
&options_.manifest_fixer_options.compile_sdk_version_codename);
209+
AddOptionalSwitch(
210+
"--no-compile-sdk-metadata",
211+
"Suppresses output of compile SDK-related attributes in AndroidManifest.xml,\n"
212+
"including android:compileSdkVersion and platformBuildVersion.",
213+
&options_.manifest_fixer_options.no_compile_sdk_metadata);
209214
AddOptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
210215
&shared_lib_);
211216
AddOptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib_);

tools/aapt2/link/ManifestFixer.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ bool ManifestFixer::Consume(IAaptContext* context, xml::XmlResource* doc) {
635635
root->InsertChild(0, std::move(uses_sdk));
636636
}
637637

638-
if (options_.compile_sdk_version) {
638+
if (!options_.no_compile_sdk_metadata && options_.compile_sdk_version) {
639639
xml::Attribute* attr = root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersion");
640640

641641
// Make sure we un-compile the value if it was set to something else.
@@ -647,10 +647,9 @@ bool ManifestFixer::Consume(IAaptContext* context, xml::XmlResource* doc) {
647647
// Make sure we un-compile the value if it was set to something else.
648648
attr->compiled_value = {};
649649
attr->value = options_.compile_sdk_version.value();
650-
651650
}
652651

653-
if (options_.compile_sdk_version_codename) {
652+
if (!options_.no_compile_sdk_metadata && options_.compile_sdk_version_codename) {
654653
xml::Attribute* attr =
655654
root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename");
656655

tools/aapt2/link/ManifestFixer.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ struct ManifestFixerOptions {
6868
std::optional<std::string> revision_code_default;
6969

7070
// The version of the framework being compiled against to set for 'android:compileSdkVersion' in
71-
// the <manifest> tag.
71+
// the <manifest> tag. Not used if no_compile_sdk_metadata is set.
7272
std::optional<std::string> compile_sdk_version;
7373

7474
// The version codename of the framework being compiled against to set for
75-
// 'android:compileSdkVersionCodename' in the <manifest> tag.
75+
// 'android:compileSdkVersionCodename' in the <manifest> tag. Not used if no_compile_sdk_metadata
76+
// is set.
7677
std::optional<std::string> compile_sdk_version_codename;
7778

7879
// Whether validation errors should be treated only as warnings. If this is 'true', then an
@@ -85,6 +86,9 @@ struct ManifestFixerOptions {
8586

8687
// Whether to replace the manifest version with the the command line version
8788
bool replace_version = false;
89+
90+
// Whether to suppress `android:compileSdkVersion*` and `platformBuildVersion*` attributes.
91+
bool no_compile_sdk_metadata = false;
8892
};
8993

9094
// Verifies that the manifest is correctly formed and inserts defaults where specified with

tools/aapt2/link/ManifestFixer_test.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,35 @@ TEST_F(ManifestFixerTest, InsertCompileSdkVersions) {
892892
EXPECT_THAT(attr->value, StrEq("P"));
893893
}
894894

895+
TEST_F(ManifestFixerTest, DoNotInsertCompileSdkVersions) {
896+
std::string input = R"(<manifest package="com.pkg" />)";
897+
ManifestFixerOptions options;
898+
options.no_compile_sdk_metadata = true;
899+
options.compile_sdk_version = {"28"};
900+
options.compile_sdk_version_codename = {"P"};
901+
902+
std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
903+
ASSERT_THAT(manifest, NotNull());
904+
905+
// There should be a declaration of kSchemaAndroid, even when the input
906+
// didn't have one.
907+
EXPECT_EQ(manifest->root->namespace_decls.size(), 1);
908+
EXPECT_EQ(manifest->root->namespace_decls[0].prefix, "android");
909+
EXPECT_EQ(manifest->root->namespace_decls[0].uri, xml::kSchemaAndroid);
910+
911+
xml::Attribute* attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersion");
912+
ASSERT_THAT(attr, IsNull());
913+
914+
attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename");
915+
ASSERT_THAT(attr, IsNull());
916+
917+
attr = manifest->root->FindAttribute("", "platformBuildVersionCode");
918+
ASSERT_THAT(attr, IsNull());
919+
920+
attr = manifest->root->FindAttribute("", "platformBuildVersionName");
921+
ASSERT_THAT(attr, IsNull());
922+
}
923+
895924
TEST_F(ManifestFixerTest, OverrideCompileSdkVersions) {
896925
std::string input = R"(
897926
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android"

0 commit comments

Comments
 (0)