diff --git a/tools/aapt2/cmd/Link.cpp b/tools/aapt2/cmd/Link.cpp index a3b7664818873..b90e7b3042f91 100644 --- a/tools/aapt2/cmd/Link.cpp +++ b/tools/aapt2/cmd/Link.cpp @@ -798,35 +798,39 @@ class LinkCommand { return; } - xml::Attribute* attr = manifest_xml->root->FindAttribute(xml::kSchemaAndroid, "versionCode"); - if (attr != nullptr) { - Maybe& compile_sdk_version = options_.manifest_fixer_options.compile_sdk_version; - if (BinaryPrimitive* prim = ValueCast(attr->compiled_value.get())) { - switch (prim->value.dataType) { - case Res_value::TYPE_INT_DEC: - compile_sdk_version = StringPrintf("%" PRId32, static_cast(prim->value.data)); - break; - case Res_value::TYPE_INT_HEX: - compile_sdk_version = StringPrintf("%" PRIx32, prim->value.data); - break; - default: - break; + if (!options_.manifest_fixer_options.compile_sdk_version) { + xml::Attribute* attr = manifest_xml->root->FindAttribute(xml::kSchemaAndroid, "versionCode"); + if (attr != nullptr) { + Maybe& compile_sdk_version = options_.manifest_fixer_options.compile_sdk_version; + if (BinaryPrimitive* prim = ValueCast(attr->compiled_value.get())) { + switch (prim->value.dataType) { + case Res_value::TYPE_INT_DEC: + compile_sdk_version = StringPrintf("%" PRId32, static_cast(prim->value.data)); + break; + case Res_value::TYPE_INT_HEX: + compile_sdk_version = StringPrintf("%" PRIx32, prim->value.data); + break; + default: + break; + } + } else if (String* str = ValueCast(attr->compiled_value.get())) { + compile_sdk_version = *str->value; + } else { + compile_sdk_version = attr->value; } - } else if (String* str = ValueCast(attr->compiled_value.get())) { - compile_sdk_version = *str->value; - } else { - compile_sdk_version = attr->value; } } - attr = manifest_xml->root->FindAttribute(xml::kSchemaAndroid, "versionName"); - if (attr != nullptr) { - Maybe& compile_sdk_version_codename = - options_.manifest_fixer_options.compile_sdk_version_codename; - if (String* str = ValueCast(attr->compiled_value.get())) { - compile_sdk_version_codename = *str->value; - } else { - compile_sdk_version_codename = attr->value; + if (!options_.manifest_fixer_options.compile_sdk_version_codename) { + xml::Attribute* attr = manifest_xml->root->FindAttribute(xml::kSchemaAndroid, "versionName"); + if (attr != nullptr) { + Maybe& compile_sdk_version_codename = + options_.manifest_fixer_options.compile_sdk_version_codename; + if (String* str = ValueCast(attr->compiled_value.get())) { + compile_sdk_version_codename = *str->value; + } else { + compile_sdk_version_codename = attr->value; + } } } } @@ -2102,6 +2106,13 @@ int Link(const std::vector& args, IDiagnostics* diagnostics) { .OptionalFlag("--version-name", "Version name to inject into the AndroidManifest.xml if none is present.", &options.manifest_fixer_options.version_name_default) + .OptionalFlag("--compile-sdk-version-code", + "Version code (integer) to inject into the AndroidManifest.xml if none is\n" + "present.", + &options.manifest_fixer_options.compile_sdk_version) + .OptionalFlag("--compile-sdk-version-name", + "Version name to inject into the AndroidManifest.xml if none is present.", + &options.manifest_fixer_options.compile_sdk_version_codename) .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library.", &shared_lib) .OptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib)