| 1 |
/* |
| 2 |
* Copyright (C) 2014 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 <algorithm> |
| 18 |
#include <cstdio> |
| 19 |
|
| 20 |
#include "aapt/AaptUtil.h" |
| 21 |
|
| 22 |
#include "Grouper.h" |
| 23 |
#include "Rule.h" |
| 24 |
#include "RuleGenerator.h" |
| 25 |
#include "SplitDescription.h" |
| 26 |
#include "SplitSelector.h" |
| 27 |
|
| 28 |
#include <androidfw/AssetManager.h> |
| 29 |
#include <androidfw/ResourceTypes.h> |
| 30 |
#include <utils/KeyedVector.h> |
| 31 |
#include <utils/Vector.h> |
| 32 |
|
| 33 |
using namespace android; |
| 34 |
|
| 35 |
namespace split { |
| 36 |
|
| 37 |
static void usage() { |
| 38 |
fprintf(stderr, |
| 39 |
"split-select --help\n" |
| 40 |
"split-select --target <config> --base <path/to/apk> [--split <path/to/apk> [...]]\n" |
| 41 |
"split-select --generate --base <path/to/apk> [--split <path/to/apk> [...]]\n" |
| 42 |
"\n" |
| 43 |
" --help Displays more information about this program.\n" |
| 44 |
" --target <config> Performs the Split APK selection on the given configuration.\n" |
| 45 |
" --generate Generates the logic for selecting the Split APK, in JSON format.\n" |
| 46 |
" --base <path/to/apk> Specifies the base APK, from which all Split APKs must be based off.\n" |
| 47 |
" --split <path/to/apk> Includes a Split APK in the selection process.\n" |
| 48 |
"\n" |
| 49 |
" Where <config> is an extended AAPT resource qualifier of the form\n" |
| 50 |
" 'resource-qualifiers:extended-qualifiers', where 'resource-qualifiers' is an AAPT resource\n" |
| 51 |
" qualifier (ex: en-rUS-sw600dp-xhdpi), and 'extended-qualifiers' is an ordered list of one\n" |
| 52 |
" qualifier (or none) from each category:\n" |
| 53 |
" Architecture: armeabi, armeabi-v7a, arm64-v8a, x86, x86_64, mips\n"); |
| 54 |
} |
| 55 |
|
| 56 |
static void help() { |
| 57 |
usage(); |
| 58 |
fprintf(stderr, "\n" |
| 59 |
" Generates the logic for selecting a Split APK given some target Android device configuration.\n" |
| 60 |
" Using the flag --generate will emit a JSON encoded tree of rules that must be satisfied in order\n" |
| 61 |
" to install the given Split APK. Using the flag --target along with the device configuration\n" |
| 62 |
" will emit the set of Split APKs to install, following the same logic that would have been emitted\n" |
| 63 |
" via JSON.\n"); |
| 64 |
} |
| 65 |
|
| 66 |
Vector<SplitDescription> select(const SplitDescription& target, const Vector<SplitDescription>& splits) { |
| 67 |
const SplitSelector selector(splits); |
| 68 |
return selector.getBestSplits(target); |
| 69 |
} |
| 70 |
|
| 71 |
void generate(const KeyedVector<String8, Vector<SplitDescription> >& splits, const String8& base) { |
| 72 |
Vector<SplitDescription> allSplits; |
| 73 |
const size_t apkSplitCount = splits.size(); |
| 74 |
for (size_t i = 0; i < apkSplitCount; i++) { |
| 75 |
allSplits.appendVector(splits[i]); |
| 76 |
} |
| 77 |
const SplitSelector selector(allSplits); |
| 78 |
KeyedVector<SplitDescription, sp<Rule> > rules(selector.getRules()); |
| 79 |
|
| 80 |
bool first = true; |
| 81 |
fprintf(stdout, "[\n"); |
| 82 |
for (size_t i = 0; i < apkSplitCount; i++) { |
| 83 |
if (splits.keyAt(i) == base) { |
| 84 |
// Skip the base. |
| 85 |
continue; |
| 86 |
} |
| 87 |
|
| 88 |
if (!first) { |
| 89 |
fprintf(stdout, ",\n"); |
| 90 |
} |
| 91 |
first = false; |
| 92 |
|
| 93 |
sp<Rule> masterRule = new Rule(); |
| 94 |
masterRule->op = Rule::OR_SUBRULES; |
| 95 |
const Vector<SplitDescription>& splitDescriptions = splits[i]; |
| 96 |
const size_t splitDescriptionCount = splitDescriptions.size(); |
| 97 |
for (size_t j = 0; j < splitDescriptionCount; j++) { |
| 98 |
masterRule->subrules.add(rules.valueFor(splitDescriptions[j])); |
| 99 |
} |
| 100 |
masterRule = Rule::simplify(masterRule); |
| 101 |
fprintf(stdout, " {\n \"path\": \"%s\",\n \"rules\": %s\n }", |
| 102 |
splits.keyAt(i).string(), |
| 103 |
masterRule->toJson(2).string()); |
| 104 |
} |
| 105 |
fprintf(stdout, "\n]\n"); |
| 106 |
} |
| 107 |
|
| 108 |
static void removeRuntimeQualifiers(ConfigDescription* outConfig) { |
| 109 |
outConfig->imsi = 0; |
| 110 |
outConfig->orientation = ResTable_config::ORIENTATION_ANY; |
| 111 |
outConfig->screenWidth = ResTable_config::SCREENWIDTH_ANY; |
| 112 |
outConfig->screenHeight = ResTable_config::SCREENHEIGHT_ANY; |
| 113 |
outConfig->uiMode &= ResTable_config::UI_MODE_NIGHT_ANY; |
| 114 |
} |
| 115 |
|
| 116 |
struct AppInfo { |
| 117 |
int versionCode; |
| 118 |
int minSdkVersion; |
| 119 |
bool multiArch; |
| 120 |
}; |
| 121 |
|
| 122 |
static bool getAppInfo(const String8& path, AppInfo& outInfo) { |
| 123 |
memset(&outInfo, 0, sizeof(outInfo)); |
| 124 |
|
| 125 |
AssetManager assetManager; |
| 126 |
int32_t cookie = 0; |
| 127 |
if (!assetManager.addAssetPath(path, &cookie)) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
Asset* asset = assetManager.openNonAsset(cookie, "AndroidManifest.xml", Asset::ACCESS_BUFFER); |
| 132 |
if (asset == NULL) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
ResXMLTree xml; |
| 137 |
if (xml.setTo(asset->getBuffer(true), asset->getLength(), false) != NO_ERROR) { |
| 138 |
delete asset; |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
const String16 kAndroidNamespace("http://schemas.android.com/apk/res/android"); |
| 143 |
const String16 kManifestTag("manifest"); |
| 144 |
const String16 kApplicationTag("application"); |
| 145 |
const String16 kUsesSdkTag("uses-sdk"); |
| 146 |
const String16 kVersionCodeAttr("versionCode"); |
| 147 |
const String16 kMultiArchAttr("multiArch"); |
| 148 |
const String16 kMinSdkVersionAttr("minSdkVersion"); |
| 149 |
|
| 150 |
ResXMLParser::event_code_t event; |
| 151 |
while ((event = xml.next()) != ResXMLParser::BAD_DOCUMENT && |
| 152 |
event != ResXMLParser::END_DOCUMENT) { |
| 153 |
if (event != ResXMLParser::START_TAG) { |
| 154 |
continue; |
| 155 |
} |
| 156 |
|
| 157 |
size_t len; |
| 158 |
const char16_t* name = xml.getElementName(&len); |
| 159 |
String16 name16(name, len); |
| 160 |
if (name16 == kManifestTag) { |
| 161 |
ssize_t idx = xml.indexOfAttribute( |
| 162 |
kAndroidNamespace.string(), kAndroidNamespace.size(), |
| 163 |
kVersionCodeAttr.string(), kVersionCodeAttr.size()); |
| 164 |
if (idx >= 0) { |
| 165 |
outInfo.versionCode = xml.getAttributeData(idx); |
| 166 |
} |
| 167 |
|
| 168 |
} else if (name16 == kApplicationTag) { |
| 169 |
ssize_t idx = xml.indexOfAttribute( |
| 170 |
kAndroidNamespace.string(), kAndroidNamespace.size(), |
| 171 |
kMultiArchAttr.string(), kMultiArchAttr.size()); |
| 172 |
if (idx >= 0) { |
| 173 |
outInfo.multiArch = xml.getAttributeData(idx) != 0; |
| 174 |
} |
| 175 |
|
| 176 |
} else if (name16 == kUsesSdkTag) { |
| 177 |
ssize_t idx = xml.indexOfAttribute( |
| 178 |
kAndroidNamespace.string(), kAndroidNamespace.size(), |
| 179 |
kMinSdkVersionAttr.string(), kMinSdkVersionAttr.size()); |
| 180 |
if (idx >= 0) { |
| 181 |
uint16_t type = xml.getAttributeDataType(idx); |
| 182 |
if (type >= Res_value::TYPE_FIRST_INT && type <= Res_value::TYPE_LAST_INT) { |
| 183 |
outInfo.minSdkVersion = xml.getAttributeData(idx); |
| 184 |
} else if (type == Res_value::TYPE_STRING) { |
| 185 |
auto minSdk8 = xml.getStrings().string8ObjectAt(idx); |
| 186 |
if (!minSdk8.has_value()) { |
| 187 |
fprintf(stderr, "warning: failed to retrieve android:minSdkVersion.\n"); |
| 188 |
} else { |
| 189 |
char *endPtr; |
| 190 |
int minSdk = strtol(minSdk8->string(), &endPtr, 10); |
| 191 |
if (endPtr != minSdk8->string() + minSdk8->size()) { |
| 192 |
fprintf(stderr, "warning: failed to parse android:minSdkVersion '%s'\n", |
| 193 |
minSdk8->string()); |
| 194 |
} else { |
| 195 |
outInfo.minSdkVersion = minSdk; |
| 196 |
} |
| 197 |
} |
| 198 |
} else { |
| 199 |
fprintf(stderr, "warning: unrecognized value for android:minSdkVersion.\n"); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
delete asset; |
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
static Vector<SplitDescription> extractSplitDescriptionsFromApk(const String8& path) { |
| 210 |
AssetManager assetManager; |
| 211 |
Vector<SplitDescription> splits; |
| 212 |
int32_t cookie = 0; |
| 213 |
if (!assetManager.addAssetPath(path, &cookie)) { |
| 214 |
return splits; |
| 215 |
} |
| 216 |
|
| 217 |
const ResTable& res = assetManager.getResources(false); |
| 218 |
if (res.getError() == NO_ERROR) { |
| 219 |
Vector<ResTable_config> configs; |
| 220 |
res.getConfigurations(&configs, true); |
| 221 |
const size_t configCount = configs.size(); |
| 222 |
for (size_t i = 0; i < configCount; i++) { |
| 223 |
splits.add(); |
| 224 |
splits.editTop().config = configs[i]; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
AssetDir* dir = assetManager.openNonAssetDir(cookie, "lib"); |
| 229 |
if (dir != NULL) { |
| 230 |
const size_t fileCount = dir->getFileCount(); |
| 231 |
for (size_t i = 0; i < fileCount; i++) { |
| 232 |
splits.add(); |
| 233 |
Vector<String8> parts = AaptUtil::splitAndLowerCase(dir->getFileName(i), '-'); |
| 234 |
if (parseAbi(parts, 0, &splits.editTop()) < 0) { |
| 235 |
fprintf(stderr, "Malformed library %s\n", dir->getFileName(i).string()); |
| 236 |
splits.pop(); |
| 237 |
} |
| 238 |
} |
| 239 |
delete dir; |
| 240 |
} |
| 241 |
return splits; |
| 242 |
} |
| 243 |
|
| 244 |
static int main(int argc, char** argv) { |
| 245 |
// Skip over the first argument. |
| 246 |
argc--; |
| 247 |
argv++; |
| 248 |
|
| 249 |
bool generateFlag = false; |
| 250 |
String8 targetConfigStr; |
| 251 |
Vector<String8> splitApkPaths; |
| 252 |
String8 baseApkPath; |
| 253 |
while (argc > 0) { |
| 254 |
const String8 arg(*argv); |
| 255 |
if (arg == "--target") { |
| 256 |
argc--; |
| 257 |
argv++; |
| 258 |
if (argc < 1) { |
| 259 |
fprintf(stderr, "error: missing parameter for --target.\n"); |
| 260 |
usage(); |
| 261 |
return 1; |
| 262 |
} |
| 263 |
targetConfigStr.setTo(*argv); |
| 264 |
} else if (arg == "--split") { |
| 265 |
argc--; |
| 266 |
argv++; |
| 267 |
if (argc < 1) { |
| 268 |
fprintf(stderr, "error: missing parameter for --split.\n"); |
| 269 |
usage(); |
| 270 |
return 1; |
| 271 |
} |
| 272 |
splitApkPaths.add(String8(*argv)); |
| 273 |
} else if (arg == "--base") { |
| 274 |
argc--; |
| 275 |
argv++; |
| 276 |
if (argc < 1) { |
| 277 |
fprintf(stderr, "error: missing parameter for --base.\n"); |
| 278 |
usage(); |
| 279 |
return 1; |
| 280 |
} |
| 281 |
|
| 282 |
if (baseApkPath.size() > 0) { |
| 283 |
fprintf(stderr, "error: multiple --base flags not allowed.\n"); |
| 284 |
usage(); |
| 285 |
return 1; |
| 286 |
} |
| 287 |
baseApkPath.setTo(*argv); |
| 288 |
} else if (arg == "--generate") { |
| 289 |
generateFlag = true; |
| 290 |
} else if (arg == "--help") { |
| 291 |
help(); |
| 292 |
return 0; |
| 293 |
} else { |
| 294 |
fprintf(stderr, "error: unknown argument '%s'.\n", arg.string()); |
| 295 |
usage(); |
| 296 |
return 1; |
| 297 |
} |
| 298 |
argc--; |
| 299 |
argv++; |
| 300 |
} |
| 301 |
|
| 302 |
if (!generateFlag && targetConfigStr == "") { |
| 303 |
usage(); |
| 304 |
return 1; |
| 305 |
} |
| 306 |
|
| 307 |
if (baseApkPath.size() == 0) { |
| 308 |
fprintf(stderr, "error: missing --base argument.\n"); |
| 309 |
usage(); |
| 310 |
return 1; |
| 311 |
} |
| 312 |
|
| 313 |
// Find out some details about the base APK. |
| 314 |
AppInfo baseAppInfo; |
| 315 |
if (!getAppInfo(baseApkPath, baseAppInfo)) { |
| 316 |
fprintf(stderr, "error: unable to read base APK: '%s'.\n", baseApkPath.string()); |
| 317 |
return 1; |
| 318 |
} |
| 319 |
|
| 320 |
SplitDescription targetSplit; |
| 321 |
if (!generateFlag) { |
| 322 |
if (!SplitDescription::parse(targetConfigStr, &targetSplit)) { |
| 323 |
fprintf(stderr, "error: invalid --target config: '%s'.\n", |
| 324 |
targetConfigStr.string()); |
| 325 |
usage(); |
| 326 |
return 1; |
| 327 |
} |
| 328 |
|
| 329 |
// We don't want to match on things that will change at run-time |
| 330 |
// (orientation, w/h, etc.). |
| 331 |
removeRuntimeQualifiers(&targetSplit.config); |
| 332 |
} |
| 333 |
|
| 334 |
splitApkPaths.add(baseApkPath); |
| 335 |
|
| 336 |
KeyedVector<String8, Vector<SplitDescription> > apkPathSplitMap; |
| 337 |
KeyedVector<SplitDescription, String8> splitApkPathMap; |
| 338 |
Vector<SplitDescription> splitConfigs; |
| 339 |
const size_t splitCount = splitApkPaths.size(); |
| 340 |
for (size_t i = 0; i < splitCount; i++) { |
| 341 |
Vector<SplitDescription> splits = extractSplitDescriptionsFromApk(splitApkPaths[i]); |
| 342 |
if (splits.isEmpty()) { |
| 343 |
fprintf(stderr, "error: invalid --split path: '%s'. No splits found.\n", |
| 344 |
splitApkPaths[i].string()); |
| 345 |
usage(); |
| 346 |
return 1; |
| 347 |
} |
| 348 |
apkPathSplitMap.replaceValueFor(splitApkPaths[i], splits); |
| 349 |
const size_t apkSplitDescriptionCount = splits.size(); |
| 350 |
for (size_t j = 0; j < apkSplitDescriptionCount; j++) { |
| 351 |
splitApkPathMap.replaceValueFor(splits[j], splitApkPaths[i]); |
| 352 |
} |
| 353 |
splitConfigs.appendVector(splits); |
| 354 |
} |
| 355 |
|
| 356 |
if (!generateFlag) { |
| 357 |
Vector<SplitDescription> matchingConfigs = select(targetSplit, splitConfigs); |
| 358 |
const size_t matchingConfigCount = matchingConfigs.size(); |
| 359 |
SortedVector<String8> matchingSplitPaths; |
| 360 |
for (size_t i = 0; i < matchingConfigCount; i++) { |
| 361 |
matchingSplitPaths.add(splitApkPathMap.valueFor(matchingConfigs[i])); |
| 362 |
} |
| 363 |
|
| 364 |
const size_t matchingSplitApkPathCount = matchingSplitPaths.size(); |
| 365 |
for (size_t i = 0; i < matchingSplitApkPathCount; i++) { |
| 366 |
if (matchingSplitPaths[i] != baseApkPath) { |
| 367 |
fprintf(stdout, "%s\n", matchingSplitPaths[i].string()); |
| 368 |
} |
| 369 |
} |
| 370 |
} else { |
| 371 |
generate(apkPathSplitMap, baseApkPath); |
| 372 |
} |
| 373 |
return 0; |
| 374 |
} |
| 375 |
|
| 376 |
} // namespace split |
| 377 |
|
| 378 |
int main(int argc, char** argv) { |
| 379 |
return split::main(argc, argv); |
| 380 |
} |