Merge "Allow overriding the compile SDK" into pi-dev
am: fd5166ddac

Change-Id: I53d265b82f4a0e9b86b1324900e133911a355104
diff --git a/tools/aapt2/cmd/Link.cpp b/tools/aapt2/cmd/Link.cpp
index a3b7664..b90e7b3 100644
--- a/tools/aapt2/cmd/Link.cpp
+++ b/tools/aapt2/cmd/Link.cpp
@@ -798,35 +798,39 @@
       return;
     }
 
-    xml::Attribute* attr = manifest_xml->root->FindAttribute(xml::kSchemaAndroid, "versionCode");
-    if (attr != nullptr) {
-      Maybe<std::string>& compile_sdk_version = options_.manifest_fixer_options.compile_sdk_version;
-      if (BinaryPrimitive* prim = ValueCast<BinaryPrimitive>(attr->compiled_value.get())) {
-        switch (prim->value.dataType) {
-          case Res_value::TYPE_INT_DEC:
-            compile_sdk_version = StringPrintf("%" PRId32, static_cast<int32_t>(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<std::string>& compile_sdk_version = options_.manifest_fixer_options.compile_sdk_version;
+        if (BinaryPrimitive* prim = ValueCast<BinaryPrimitive>(attr->compiled_value.get())) {
+          switch (prim->value.dataType) {
+            case Res_value::TYPE_INT_DEC:
+              compile_sdk_version = StringPrintf("%" PRId32, static_cast<int32_t>(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<String>(attr->compiled_value.get())) {
+          compile_sdk_version = *str->value;
+        } else {
+          compile_sdk_version = attr->value;
         }
-      } else if (String* str = ValueCast<String>(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<std::string>& compile_sdk_version_codename =
-          options_.manifest_fixer_options.compile_sdk_version_codename;
-      if (String* str = ValueCast<String>(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<std::string>& compile_sdk_version_codename =
+            options_.manifest_fixer_options.compile_sdk_version_codename;
+        if (String* str = ValueCast<String>(attr->compiled_value.get())) {
+          compile_sdk_version_codename = *str->value;
+        } else {
+          compile_sdk_version_codename = attr->value;
+        }
       }
     }
   }
@@ -2102,6 +2106,13 @@
           .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)