Util.cpp revision 8780eb6e4918ae24fb1ae74d631042c32e41dc3d
1d0f492db038c6210c1138865d816bfb134376538Adam Lesinski/* 2d0f492db038c6210c1138865d816bfb134376538Adam Lesinski * Copyright (C) 2017 The Android Open Source Project 3d0f492db038c6210c1138865d816bfb134376538Adam Lesinski * 4d0f492db038c6210c1138865d816bfb134376538Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License"); 5d0f492db038c6210c1138865d816bfb134376538Adam Lesinski * you may not use this file except in compliance with the License. 6d0f492db038c6210c1138865d816bfb134376538Adam Lesinski * You may obtain a copy of the License at 7d0f492db038c6210c1138865d816bfb134376538Adam Lesinski * 8d0f492db038c6210c1138865d816bfb134376538Adam Lesinski * http://www.apache.org/licenses/LICENSE-2.0 9d0f492db038c6210c1138865d816bfb134376538Adam Lesinski * 10d0f492db038c6210c1138865d816bfb134376538Adam Lesinski * Unless required by applicable law or agreed to in writing, software 11d0f492db038c6210c1138865d816bfb134376538Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS, 12d0f492db038c6210c1138865d816bfb134376538Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d0f492db038c6210c1138865d816bfb134376538Adam Lesinski * See the License for the specific language governing permissions and 14d0f492db038c6210c1138865d816bfb134376538Adam Lesinski * limitations under the License. 15d0f492db038c6210c1138865d816bfb134376538Adam Lesinski */ 16d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 17d0f492db038c6210c1138865d816bfb134376538Adam Lesinski#include "cmd/Util.h" 18d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 19d0f492db038c6210c1138865d816bfb134376538Adam Lesinski#include <vector> 20d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 21d0f492db038c6210c1138865d816bfb134376538Adam Lesinski#include "android-base/logging.h" 22d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 23d0f492db038c6210c1138865d816bfb134376538Adam Lesinski#include "ConfigDescription.h" 24d0f492db038c6210c1138865d816bfb134376538Adam Lesinski#include "Locale.h" 25d0f492db038c6210c1138865d816bfb134376538Adam Lesinski#include "ResourceUtils.h" 26d0f492db038c6210c1138865d816bfb134376538Adam Lesinski#include "ValueVisitor.h" 27d0f492db038c6210c1138865d816bfb134376538Adam Lesinski#include "split/TableSplitter.h" 28d0f492db038c6210c1138865d816bfb134376538Adam Lesinski#include "util/Maybe.h" 29d0f492db038c6210c1138865d816bfb134376538Adam Lesinski#include "util/Util.h" 30d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 316b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinskiusing ::android::StringPiece; 32d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 33d0f492db038c6210c1138865d816bfb134376538Adam Lesinskinamespace aapt { 34d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 35d0f492db038c6210c1138865d816bfb134376538Adam LesinskiMaybe<uint16_t> ParseTargetDensityParameter(const StringPiece& arg, IDiagnostics* diag) { 36d0f492db038c6210c1138865d816bfb134376538Adam Lesinski ConfigDescription preferred_density_config; 37d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (!ConfigDescription::Parse(arg, &preferred_density_config)) { 38d0f492db038c6210c1138865d816bfb134376538Adam Lesinski diag->Error(DiagMessage() << "invalid density '" << arg << "' for --preferred-density option"); 39d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 40d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 41d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 42d0f492db038c6210c1138865d816bfb134376538Adam Lesinski // Clear the version that can be automatically added. 43d0f492db038c6210c1138865d816bfb134376538Adam Lesinski preferred_density_config.sdkVersion = 0; 44d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 45d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) != 46d0f492db038c6210c1138865d816bfb134376538Adam Lesinski ConfigDescription::CONFIG_DENSITY) { 47d0f492db038c6210c1138865d816bfb134376538Adam Lesinski diag->Error(DiagMessage() << "invalid preferred density '" << arg << "'. " 48d0f492db038c6210c1138865d816bfb134376538Adam Lesinski << "Preferred density must only be a density value"); 49d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 50d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 51d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return preferred_density_config.density; 52d0f492db038c6210c1138865d816bfb134376538Adam Lesinski} 53d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 54d0f492db038c6210c1138865d816bfb134376538Adam Lesinskibool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag, std::string* out_path, 55d0f492db038c6210c1138865d816bfb134376538Adam Lesinski SplitConstraints* out_split) { 56d0f492db038c6210c1138865d816bfb134376538Adam Lesinski CHECK(diag != nullptr); 57d0f492db038c6210c1138865d816bfb134376538Adam Lesinski CHECK(out_path != nullptr); 58d0f492db038c6210c1138865d816bfb134376538Adam Lesinski CHECK(out_split != nullptr); 59d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 60db091577fff5bcddac703bc8ac4f932fd53e0621Adam Lesinski#ifdef _WIN32 61db091577fff5bcddac703bc8ac4f932fd53e0621Adam Lesinski const char sSeparator = ';'; 62db091577fff5bcddac703bc8ac4f932fd53e0621Adam Lesinski#else 63db091577fff5bcddac703bc8ac4f932fd53e0621Adam Lesinski const char sSeparator = ':'; 64db091577fff5bcddac703bc8ac4f932fd53e0621Adam Lesinski#endif 65db091577fff5bcddac703bc8ac4f932fd53e0621Adam Lesinski 66db091577fff5bcddac703bc8ac4f932fd53e0621Adam Lesinski std::vector<std::string> parts = util::Split(arg, sSeparator); 67d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (parts.size() != 2) { 68d0f492db038c6210c1138865d816bfb134376538Adam Lesinski diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'"); 69db091577fff5bcddac703bc8ac4f932fd53e0621Adam Lesinski diag->Note(DiagMessage() << "should be --split path/to/output.apk" << sSeparator 70db091577fff5bcddac703bc8ac4f932fd53e0621Adam Lesinski << "<config>[,<config>...]."); 71d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return false; 72d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 73d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 74d0f492db038c6210c1138865d816bfb134376538Adam Lesinski *out_path = parts[0]; 75d0f492db038c6210c1138865d816bfb134376538Adam Lesinski for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) { 76d0f492db038c6210c1138865d816bfb134376538Adam Lesinski ConfigDescription config; 77d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (!ConfigDescription::Parse(config_str, &config)) { 78d0f492db038c6210c1138865d816bfb134376538Adam Lesinski diag->Error(DiagMessage() << "invalid config '" << config_str << "' in split parameter '" 79d0f492db038c6210c1138865d816bfb134376538Adam Lesinski << arg << "'"); 80d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return false; 81d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 82d0f492db038c6210c1138865d816bfb134376538Adam Lesinski out_split->configs.insert(config); 83d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 84d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return true; 85d0f492db038c6210c1138865d816bfb134376538Adam Lesinski} 86d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 87d0f492db038c6210c1138865d816bfb134376538Adam Lesinskistd::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args, 88d0f492db038c6210c1138865d816bfb134376538Adam Lesinski IDiagnostics* diag) { 89d0f492db038c6210c1138865d816bfb134376538Adam Lesinski std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>(); 90d0f492db038c6210c1138865d816bfb134376538Adam Lesinski for (const std::string& config_arg : args) { 91d0f492db038c6210c1138865d816bfb134376538Adam Lesinski for (const StringPiece& config_str : util::Tokenize(config_arg, ',')) { 92d0f492db038c6210c1138865d816bfb134376538Adam Lesinski ConfigDescription config; 93d0f492db038c6210c1138865d816bfb134376538Adam Lesinski LocaleValue lv; 94d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (lv.InitFromFilterString(config_str)) { 95d0f492db038c6210c1138865d816bfb134376538Adam Lesinski lv.WriteTo(&config); 96d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } else if (!ConfigDescription::Parse(config_str, &config)) { 97d0f492db038c6210c1138865d816bfb134376538Adam Lesinski diag->Error(DiagMessage() << "invalid config '" << config_str << "' for -c option"); 98d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 99d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 100d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 101d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (config.density != 0) { 102d0f492db038c6210c1138865d816bfb134376538Adam Lesinski diag->Warn(DiagMessage() << "ignoring density '" << config << "' for -c option"); 103d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } else { 104d0f492db038c6210c1138865d816bfb134376538Adam Lesinski filter->AddConfig(config); 105d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 106d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 107d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 108d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return std::move(filter); 109d0f492db038c6210c1138865d816bfb134376538Adam Lesinski} 110d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 111d0f492db038c6210c1138865d816bfb134376538Adam Lesinski// Adjust the SplitConstraints so that their SDK version is stripped if it 112d0f492db038c6210c1138865d816bfb134376538Adam Lesinski// is less than or equal to the minSdk. Otherwise the resources that have had 113d0f492db038c6210c1138865d816bfb134376538Adam Lesinski// their SDK version stripped due to minSdk won't ever match. 114d0f492db038c6210c1138865d816bfb134376538Adam Lesinskistd::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk( 115d0f492db038c6210c1138865d816bfb134376538Adam Lesinski int min_sdk, const std::vector<SplitConstraints>& split_constraints) { 116d0f492db038c6210c1138865d816bfb134376538Adam Lesinski std::vector<SplitConstraints> adjusted_constraints; 117d0f492db038c6210c1138865d816bfb134376538Adam Lesinski adjusted_constraints.reserve(split_constraints.size()); 118d0f492db038c6210c1138865d816bfb134376538Adam Lesinski for (const SplitConstraints& constraints : split_constraints) { 119d0f492db038c6210c1138865d816bfb134376538Adam Lesinski SplitConstraints constraint; 120d0f492db038c6210c1138865d816bfb134376538Adam Lesinski for (const ConfigDescription& config : constraints.configs) { 121d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (config.sdkVersion <= min_sdk) { 122d0f492db038c6210c1138865d816bfb134376538Adam Lesinski constraint.configs.insert(config.CopyWithoutSdkVersion()); 123d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } else { 124d0f492db038c6210c1138865d816bfb134376538Adam Lesinski constraint.configs.insert(config); 125d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 126d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 127d0f492db038c6210c1138865d816bfb134376538Adam Lesinski adjusted_constraints.push_back(std::move(constraint)); 128d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 129d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return adjusted_constraints; 130d0f492db038c6210c1138865d816bfb134376538Adam Lesinski} 131d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 132d0f492db038c6210c1138865d816bfb134376538Adam Lesinskistatic xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) { 133c744ae8aca97edfb2422598ea620e8219449fa9bAdam Lesinski return xml::AaptAttribute(Attribute(), id); 134d0f492db038c6210c1138865d816bfb134376538Adam Lesinski} 135d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 1366b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinskistatic xml::NamespaceDecl CreateAndroidNamespaceDecl() { 1376b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinski xml::NamespaceDecl decl; 1386b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinski decl.prefix = "android"; 1396b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinski decl.uri = xml::kSchemaAndroid; 1406b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinski return decl; 1416b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinski} 1426b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinski 143b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chaistatic std::string MakePackageSafeName(const std::string &name) { 144b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai std::string result(name); 145b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai for (char &c : result) { 146b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai if (c == '-') { 147b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai c = '_'; 148b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai } 149b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai } 150b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai return result; 151b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai} 152b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai 153d0f492db038c6210c1138865d816bfb134376538Adam Lesinskistd::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info, 154d0f492db038c6210c1138865d816bfb134376538Adam Lesinski const SplitConstraints& constraints) { 155d0f492db038c6210c1138865d816bfb134376538Adam Lesinski const ResourceId kVersionCode(0x0101021b); 156d0f492db038c6210c1138865d816bfb134376538Adam Lesinski const ResourceId kRevisionCode(0x010104d5); 157d0f492db038c6210c1138865d816bfb134376538Adam Lesinski const ResourceId kHasCode(0x0101000c); 158d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 159d0f492db038c6210c1138865d816bfb134376538Adam Lesinski std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>(); 1606b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinski manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl()); 161d0f492db038c6210c1138865d816bfb134376538Adam Lesinski manifest_el->name = "manifest"; 162d0f492db038c6210c1138865d816bfb134376538Adam Lesinski manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package}); 163d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 164d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (app_info.version_code) { 165d0f492db038c6210c1138865d816bfb134376538Adam Lesinski const uint32_t version_code = app_info.version_code.value(); 166d0f492db038c6210c1138865d816bfb134376538Adam Lesinski manifest_el->attributes.push_back(xml::Attribute{ 167d0f492db038c6210c1138865d816bfb134376538Adam Lesinski xml::kSchemaAndroid, "versionCode", std::to_string(version_code), 168d0f492db038c6210c1138865d816bfb134376538Adam Lesinski CreateAttributeWithId(kVersionCode), 169d0f492db038c6210c1138865d816bfb134376538Adam Lesinski util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)}); 170d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 171d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 172d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (app_info.revision_code) { 173d0f492db038c6210c1138865d816bfb134376538Adam Lesinski const uint32_t revision_code = app_info.revision_code.value(); 174d0f492db038c6210c1138865d816bfb134376538Adam Lesinski manifest_el->attributes.push_back(xml::Attribute{ 175d0f492db038c6210c1138865d816bfb134376538Adam Lesinski xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code), 176d0f492db038c6210c1138865d816bfb134376538Adam Lesinski CreateAttributeWithId(kRevisionCode), 177d0f492db038c6210c1138865d816bfb134376538Adam Lesinski util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)}); 178d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 179d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 180d0f492db038c6210c1138865d816bfb134376538Adam Lesinski std::stringstream split_name; 181d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (app_info.split_name) { 182d0f492db038c6210c1138865d816bfb134376538Adam Lesinski split_name << app_info.split_name.value() << "."; 183d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 184b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai std::vector<std::string> sanitized_config_names; 185b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai for (const auto &config : constraints.configs) { 186b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai sanitized_config_names.push_back(MakePackageSafeName(config.toString().string())); 187b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai } 188b8f078c9a0c9f32ad84c475c26299ecb00962ffaDonald Chai split_name << "config." << util::Joiner(sanitized_config_names, "_"); 189d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 190d0f492db038c6210c1138865d816bfb134376538Adam Lesinski manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()}); 191d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 192d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (app_info.split_name) { 193d0f492db038c6210c1138865d816bfb134376538Adam Lesinski manifest_el->attributes.push_back( 194d0f492db038c6210c1138865d816bfb134376538Adam Lesinski xml::Attribute{"", "configForSplit", app_info.split_name.value()}); 195d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 196d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 1976b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinski // Splits may contain more configurations than originally desired (fall-back densities, etc.). 1986b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinski // This makes programmatic discovery of split targeting difficult. Encode the original 1993d6323961c46b608dfb842f170b40b8b18082cfbAdam Lesinski // split constraints intended for this split. 2003d6323961c46b608dfb842f170b40b8b18082cfbAdam Lesinski std::stringstream target_config_str; 2013d6323961c46b608dfb842f170b40b8b18082cfbAdam Lesinski target_config_str << util::Joiner(constraints.configs, ","); 2023d6323961c46b608dfb842f170b40b8b18082cfbAdam Lesinski manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()}); 2033d6323961c46b608dfb842f170b40b8b18082cfbAdam Lesinski 204d0f492db038c6210c1138865d816bfb134376538Adam Lesinski std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>(); 205d0f492db038c6210c1138865d816bfb134376538Adam Lesinski application_el->name = "application"; 206d0f492db038c6210c1138865d816bfb134376538Adam Lesinski application_el->attributes.push_back( 207d0f492db038c6210c1138865d816bfb134376538Adam Lesinski xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode), 208d0f492db038c6210c1138865d816bfb134376538Adam Lesinski util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)}); 209d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 210d0f492db038c6210c1138865d816bfb134376538Adam Lesinski manifest_el->AppendChild(std::move(application_el)); 2116b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinski 2126b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinski std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>(); 2136b372991296c9f2bd6f8f8847dcd23d50970d06dAdam Lesinski doc->root = std::move(manifest_el); 214d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return doc; 215d0f492db038c6210c1138865d816bfb134376538Adam Lesinski} 216d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 2178780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinskistatic Maybe<std::string> ExtractCompiledString(const xml::Attribute& attr, 2188780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski std::string* out_error) { 2198780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski if (attr.compiled_value != nullptr) { 2208780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski const String* compiled_str = ValueCast<String>(attr.compiled_value.get()); 221d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (compiled_str != nullptr) { 222d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (!compiled_str->value->empty()) { 223d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return *compiled_str->value; 224d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } else { 225d0f492db038c6210c1138865d816bfb134376538Adam Lesinski *out_error = "compiled value is an empty string"; 226d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 227d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 228d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 229d0f492db038c6210c1138865d816bfb134376538Adam Lesinski *out_error = "compiled value is not a string"; 230d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 231d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 232d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 233d0f492db038c6210c1138865d816bfb134376538Adam Lesinski // Fallback to the plain text value if there is one. 2348780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski if (!attr.value.empty()) { 2358780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski return attr.value; 236d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 237d0f492db038c6210c1138865d816bfb134376538Adam Lesinski *out_error = "value is an empty string"; 238d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 239d0f492db038c6210c1138865d816bfb134376538Adam Lesinski} 240d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 2418780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinskistatic Maybe<uint32_t> ExtractCompiledInt(const xml::Attribute& attr, std::string* out_error) { 2428780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski if (attr.compiled_value != nullptr) { 2438780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get()); 244d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (compiled_prim != nullptr) { 245d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT && 246d0f492db038c6210c1138865d816bfb134376538Adam Lesinski compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) { 247d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return compiled_prim->value.data; 248d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 249d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 250d0f492db038c6210c1138865d816bfb134376538Adam Lesinski *out_error = "compiled value is not an integer"; 251d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 252d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 253d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 254d0f492db038c6210c1138865d816bfb134376538Adam Lesinski // Fallback to the plain text value if there is one. 2558780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr.value); 256d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (integer) { 257d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return integer; 258d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 259d0f492db038c6210c1138865d816bfb134376538Adam Lesinski std::stringstream error_msg; 2608780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski error_msg << "'" << attr.value << "' is not a valid integer"; 261d0f492db038c6210c1138865d816bfb134376538Adam Lesinski *out_error = error_msg.str(); 262d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 263d0f492db038c6210c1138865d816bfb134376538Adam Lesinski} 264d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 2658780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinskistatic Maybe<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) { 2668780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski if (attr.compiled_value != nullptr) { 2678780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get()); 268d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (compiled_prim != nullptr) { 269d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT && 270d0f492db038c6210c1138865d816bfb134376538Adam Lesinski compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) { 271d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return compiled_prim->value.data; 272d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 273d0f492db038c6210c1138865d816bfb134376538Adam Lesinski *out_error = "compiled value is not an integer or string"; 274d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 275d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 276d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 2778780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski const String* compiled_str = ValueCast<String>(attr.compiled_value.get()); 278d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (compiled_str != nullptr) { 279d0f492db038c6210c1138865d816bfb134376538Adam Lesinski Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value); 280d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (sdk_version) { 281d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return sdk_version; 282d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 283d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 284d0f492db038c6210c1138865d816bfb134376538Adam Lesinski *out_error = "compiled string value is not a valid SDK version"; 285d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 286d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 287d0f492db038c6210c1138865d816bfb134376538Adam Lesinski *out_error = "compiled value is not an integer or string"; 288d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 289d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 290d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 291d0f492db038c6210c1138865d816bfb134376538Adam Lesinski // Fallback to the plain text value if there is one. 2928780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value); 293d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (sdk_version) { 294d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return sdk_version; 295d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 296d0f492db038c6210c1138865d816bfb134376538Adam Lesinski std::stringstream error_msg; 2978780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski error_msg << "'" << attr.value << "' is not a valid SDK version"; 298d0f492db038c6210c1138865d816bfb134376538Adam Lesinski *out_error = error_msg.str(); 299d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 300d0f492db038c6210c1138865d816bfb134376538Adam Lesinski} 301d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 3028780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam LesinskiMaybe<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res, 3038780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski IDiagnostics* diag) { 304d0f492db038c6210c1138865d816bfb134376538Adam Lesinski // Make sure the first element is <manifest> with package attribute. 3058780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski const xml::Element* manifest_el = xml_res.root.get(); 306d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (manifest_el == nullptr) { 307d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 308d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 309d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 310d0f492db038c6210c1138865d816bfb134376538Adam Lesinski AppInfo app_info; 311d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 312d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") { 3138780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski diag->Error(DiagMessage(xml_res.file.source) << "root tag must be <manifest>"); 314d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 315d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 316d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 3178780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package"); 318d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (!package_attr) { 3198780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski diag->Error(DiagMessage(xml_res.file.source) << "<manifest> must have a 'package' attribute"); 320d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 321d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 322d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 323d0f492db038c6210c1138865d816bfb134376538Adam Lesinski std::string error_msg; 3248780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski Maybe<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg); 325d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (!maybe_package) { 3268780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number)) 327d0f492db038c6210c1138865d816bfb134376538Adam Lesinski << "invalid package name: " << error_msg); 328d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 329d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 330d0f492db038c6210c1138865d816bfb134376538Adam Lesinski app_info.package = maybe_package.value(); 331d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 3328780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski if (const xml::Attribute* version_code_attr = 333d0f492db038c6210c1138865d816bfb134376538Adam Lesinski manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) { 3348780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg); 335d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (!maybe_code) { 3368780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number)) 337d0f492db038c6210c1138865d816bfb134376538Adam Lesinski << "invalid android:versionCode: " << error_msg); 338d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 339d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 340d0f492db038c6210c1138865d816bfb134376538Adam Lesinski app_info.version_code = maybe_code.value(); 341d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 342d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 3438780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski if (const xml::Attribute* revision_code_attr = 344d0f492db038c6210c1138865d816bfb134376538Adam Lesinski manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) { 3458780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski Maybe<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg); 346d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (!maybe_code) { 3478780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number)) 348d0f492db038c6210c1138865d816bfb134376538Adam Lesinski << "invalid android:revisionCode: " << error_msg); 349d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 350d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 351d0f492db038c6210c1138865d816bfb134376538Adam Lesinski app_info.revision_code = maybe_code.value(); 352d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 353d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 3548780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) { 3558780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski Maybe<std::string> maybe_split_name = ExtractCompiledString(*split_name_attr, &error_msg); 356d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (!maybe_split_name) { 3578780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number)) 358d0f492db038c6210c1138865d816bfb134376538Adam Lesinski << "invalid split name: " << error_msg); 359d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 360d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 361d0f492db038c6210c1138865d816bfb134376538Adam Lesinski app_info.split_name = maybe_split_name.value(); 362d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 363d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 3648780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) { 3658780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski if (const xml::Attribute* min_sdk = 366d0f492db038c6210c1138865d816bfb134376538Adam Lesinski uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) { 3678780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski Maybe<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg); 368d0f492db038c6210c1138865d816bfb134376538Adam Lesinski if (!maybe_sdk) { 3698780eb6e4918ae24fb1ae74d631042c32e41dc3dAdam Lesinski diag->Error(DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number)) 370d0f492db038c6210c1138865d816bfb134376538Adam Lesinski << "invalid android:minSdkVersion: " << error_msg); 371d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return {}; 372d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 373d0f492db038c6210c1138865d816bfb134376538Adam Lesinski app_info.min_sdk_version = maybe_sdk.value(); 374d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 375d0f492db038c6210c1138865d816bfb134376538Adam Lesinski } 376d0f492db038c6210c1138865d816bfb134376538Adam Lesinski return app_info; 377d0f492db038c6210c1138865d816bfb134376538Adam Lesinski} 378d0f492db038c6210c1138865d816bfb134376538Adam Lesinski 379d0f492db038c6210c1138865d816bfb134376538Adam Lesinski} // namespace aapt 380