Home | History | Annotate | Download | only in cmd
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "cmd/Util.h"
     18 
     19 #include <vector>
     20 
     21 #include "android-base/logging.h"
     22 
     23 #include "ConfigDescription.h"
     24 #include "Locale.h"
     25 #include "ResourceUtils.h"
     26 #include "ValueVisitor.h"
     27 #include "split/TableSplitter.h"
     28 #include "util/Maybe.h"
     29 #include "util/Util.h"
     30 
     31 using ::android::StringPiece;
     32 
     33 namespace aapt {
     34 
     35 Maybe<uint16_t> ParseTargetDensityParameter(const StringPiece& arg, IDiagnostics* diag) {
     36   ConfigDescription preferred_density_config;
     37   if (!ConfigDescription::Parse(arg, &preferred_density_config)) {
     38     diag->Error(DiagMessage() << "invalid density '" << arg << "' for --preferred-density option");
     39     return {};
     40   }
     41 
     42   // Clear the version that can be automatically added.
     43   preferred_density_config.sdkVersion = 0;
     44 
     45   if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
     46       ConfigDescription::CONFIG_DENSITY) {
     47     diag->Error(DiagMessage() << "invalid preferred density '" << arg << "'. "
     48                               << "Preferred density must only be a density value");
     49     return {};
     50   }
     51   return preferred_density_config.density;
     52 }
     53 
     54 bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag, std::string* out_path,
     55                          SplitConstraints* out_split) {
     56   CHECK(diag != nullptr);
     57   CHECK(out_path != nullptr);
     58   CHECK(out_split != nullptr);
     59 
     60 #ifdef _WIN32
     61   const char sSeparator = ';';
     62 #else
     63   const char sSeparator = ':';
     64 #endif
     65 
     66   std::vector<std::string> parts = util::Split(arg, sSeparator);
     67   if (parts.size() != 2) {
     68     diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
     69     diag->Note(DiagMessage() << "should be --split path/to/output.apk" << sSeparator
     70                              << "<config>[,<config>...].");
     71     return false;
     72   }
     73 
     74   *out_path = parts[0];
     75   for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
     76     ConfigDescription config;
     77     if (!ConfigDescription::Parse(config_str, &config)) {
     78       diag->Error(DiagMessage() << "invalid config '" << config_str << "' in split parameter '"
     79                                 << arg << "'");
     80       return false;
     81     }
     82     out_split->configs.insert(config);
     83   }
     84   return true;
     85 }
     86 
     87 std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
     88                                                            IDiagnostics* diag) {
     89   std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>();
     90   for (const std::string& config_arg : args) {
     91     for (const StringPiece& config_str : util::Tokenize(config_arg, ',')) {
     92       ConfigDescription config;
     93       LocaleValue lv;
     94       if (lv.InitFromFilterString(config_str)) {
     95         lv.WriteTo(&config);
     96       } else if (!ConfigDescription::Parse(config_str, &config)) {
     97         diag->Error(DiagMessage() << "invalid config '" << config_str << "' for -c option");
     98         return {};
     99       }
    100 
    101       if (config.density != 0) {
    102         diag->Warn(DiagMessage() << "ignoring density '" << config << "' for -c option");
    103       } else {
    104         filter->AddConfig(config);
    105       }
    106     }
    107   }
    108   return std::move(filter);
    109 }
    110 
    111 // Adjust the SplitConstraints so that their SDK version is stripped if it
    112 // is less than or equal to the minSdk. Otherwise the resources that have had
    113 // their SDK version stripped due to minSdk won't ever match.
    114 std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
    115     int min_sdk, const std::vector<SplitConstraints>& split_constraints) {
    116   std::vector<SplitConstraints> adjusted_constraints;
    117   adjusted_constraints.reserve(split_constraints.size());
    118   for (const SplitConstraints& constraints : split_constraints) {
    119     SplitConstraints constraint;
    120     for (const ConfigDescription& config : constraints.configs) {
    121       if (config.sdkVersion <= min_sdk) {
    122         constraint.configs.insert(config.CopyWithoutSdkVersion());
    123       } else {
    124         constraint.configs.insert(config);
    125       }
    126     }
    127     adjusted_constraints.push_back(std::move(constraint));
    128   }
    129   return adjusted_constraints;
    130 }
    131 
    132 static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
    133   return xml::AaptAttribute(Attribute(), id);
    134 }
    135 
    136 static xml::NamespaceDecl CreateAndroidNamespaceDecl() {
    137   xml::NamespaceDecl decl;
    138   decl.prefix = "android";
    139   decl.uri = xml::kSchemaAndroid;
    140   return decl;
    141 }
    142 
    143 // Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
    144 // replacing nonconforming characters with underscores.
    145 //
    146 // See frameworks/base/core/java/android/content/pm/PackageParser.java which
    147 // checks this at runtime.
    148 static std::string MakePackageSafeName(const std::string &name) {
    149   std::string result(name);
    150   bool first = true;
    151   for (char &c : result) {
    152     if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
    153       first = false;
    154       continue;
    155     }
    156     if (!first) {
    157       if (c >= '0' && c <= '9') {
    158         continue;
    159       }
    160     }
    161 
    162     c = '_';
    163     first = false;
    164   }
    165   return result;
    166 }
    167 
    168 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
    169                                                         const SplitConstraints& constraints) {
    170   const ResourceId kVersionCode(0x0101021b);
    171   const ResourceId kRevisionCode(0x010104d5);
    172   const ResourceId kHasCode(0x0101000c);
    173 
    174   std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
    175   manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
    176   manifest_el->name = "manifest";
    177   manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
    178 
    179   if (app_info.version_code) {
    180     const uint32_t version_code = app_info.version_code.value();
    181     manifest_el->attributes.push_back(xml::Attribute{
    182         xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
    183         CreateAttributeWithId(kVersionCode),
    184         util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
    185   }
    186 
    187   if (app_info.revision_code) {
    188     const uint32_t revision_code = app_info.revision_code.value();
    189     manifest_el->attributes.push_back(xml::Attribute{
    190         xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
    191         CreateAttributeWithId(kRevisionCode),
    192         util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
    193   }
    194 
    195   std::stringstream split_name;
    196   if (app_info.split_name) {
    197     split_name << app_info.split_name.value() << ".";
    198   }
    199   std::vector<std::string> sanitized_config_names;
    200   for (const auto &config : constraints.configs) {
    201     sanitized_config_names.push_back(MakePackageSafeName(config.toString().string()));
    202   }
    203   split_name << "config." << util::Joiner(sanitized_config_names, "_");
    204 
    205   manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
    206 
    207   if (app_info.split_name) {
    208     manifest_el->attributes.push_back(
    209         xml::Attribute{"", "configForSplit", app_info.split_name.value()});
    210   }
    211 
    212   // Splits may contain more configurations than originally desired (fall-back densities, etc.).
    213   // This makes programmatic discovery of split targeting difficult. Encode the original
    214   // split constraints intended for this split.
    215   std::stringstream target_config_str;
    216   target_config_str << util::Joiner(constraints.configs, ",");
    217   manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
    218 
    219   std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
    220   application_el->name = "application";
    221   application_el->attributes.push_back(
    222       xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
    223                      util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
    224 
    225   manifest_el->AppendChild(std::move(application_el));
    226 
    227   std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
    228   doc->root = std::move(manifest_el);
    229   return doc;
    230 }
    231 
    232 static Maybe<std::string> ExtractCompiledString(const xml::Attribute& attr,
    233                                                 std::string* out_error) {
    234   if (attr.compiled_value != nullptr) {
    235     const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
    236     if (compiled_str != nullptr) {
    237       if (!compiled_str->value->empty()) {
    238         return *compiled_str->value;
    239       } else {
    240         *out_error = "compiled value is an empty string";
    241         return {};
    242       }
    243     }
    244     *out_error = "compiled value is not a string";
    245     return {};
    246   }
    247 
    248   // Fallback to the plain text value if there is one.
    249   if (!attr.value.empty()) {
    250     return attr.value;
    251   }
    252   *out_error = "value is an empty string";
    253   return {};
    254 }
    255 
    256 static Maybe<uint32_t> ExtractCompiledInt(const xml::Attribute& attr, std::string* out_error) {
    257   if (attr.compiled_value != nullptr) {
    258     const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
    259     if (compiled_prim != nullptr) {
    260       if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
    261           compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
    262         return compiled_prim->value.data;
    263       }
    264     }
    265     *out_error = "compiled value is not an integer";
    266     return {};
    267   }
    268 
    269   // Fallback to the plain text value if there is one.
    270   Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
    271   if (integer) {
    272     return integer;
    273   }
    274   std::stringstream error_msg;
    275   error_msg << "'" << attr.value << "' is not a valid integer";
    276   *out_error = error_msg.str();
    277   return {};
    278 }
    279 
    280 static Maybe<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
    281   if (attr.compiled_value != nullptr) {
    282     const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
    283     if (compiled_prim != nullptr) {
    284       if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
    285           compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
    286         return compiled_prim->value.data;
    287       }
    288       *out_error = "compiled value is not an integer or string";
    289       return {};
    290     }
    291 
    292     const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
    293     if (compiled_str != nullptr) {
    294       Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
    295       if (sdk_version) {
    296         return sdk_version;
    297       }
    298 
    299       *out_error = "compiled string value is not a valid SDK version";
    300       return {};
    301     }
    302     *out_error = "compiled value is not an integer or string";
    303     return {};
    304   }
    305 
    306   // Fallback to the plain text value if there is one.
    307   Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
    308   if (sdk_version) {
    309     return sdk_version;
    310   }
    311   std::stringstream error_msg;
    312   error_msg << "'" << attr.value << "' is not a valid SDK version";
    313   *out_error = error_msg.str();
    314   return {};
    315 }
    316 
    317 Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
    318                                                 IDiagnostics* diag) {
    319   // Make sure the first element is <manifest> with package attribute.
    320   const xml::Element* manifest_el = xml_res.root.get();
    321   if (manifest_el == nullptr) {
    322     return {};
    323   }
    324 
    325   AppInfo app_info;
    326 
    327   if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
    328     diag->Error(DiagMessage(xml_res.file.source) << "root tag must be <manifest>");
    329     return {};
    330   }
    331 
    332   const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
    333   if (!package_attr) {
    334     diag->Error(DiagMessage(xml_res.file.source) << "<manifest> must have a 'package' attribute");
    335     return {};
    336   }
    337 
    338   std::string error_msg;
    339   Maybe<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
    340   if (!maybe_package) {
    341     diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
    342                 << "invalid package name: " << error_msg);
    343     return {};
    344   }
    345   app_info.package = maybe_package.value();
    346 
    347   if (const xml::Attribute* version_code_attr =
    348           manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
    349     Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
    350     if (!maybe_code) {
    351       diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
    352                   << "invalid android:versionCode: " << error_msg);
    353       return {};
    354     }
    355     app_info.version_code = maybe_code.value();
    356   }
    357 
    358   if (const xml::Attribute* revision_code_attr =
    359           manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
    360     Maybe<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
    361     if (!maybe_code) {
    362       diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
    363                   << "invalid android:revisionCode: " << error_msg);
    364       return {};
    365     }
    366     app_info.revision_code = maybe_code.value();
    367   }
    368 
    369   if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
    370     Maybe<std::string> maybe_split_name = ExtractCompiledString(*split_name_attr, &error_msg);
    371     if (!maybe_split_name) {
    372       diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
    373                   << "invalid split name: " << error_msg);
    374       return {};
    375     }
    376     app_info.split_name = maybe_split_name.value();
    377   }
    378 
    379   if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
    380     if (const xml::Attribute* min_sdk =
    381             uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
    382       Maybe<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
    383       if (!maybe_sdk) {
    384         diag->Error(DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
    385                     << "invalid android:minSdkVersion: " << error_msg);
    386         return {};
    387       }
    388       app_info.min_sdk_version = maybe_sdk.value();
    389     }
    390   }
    391   return app_info;
    392 }
    393 
    394 }  // namespace aapt
    395