blob: d3eb012a80e98a07e8d6b9bd854f8b3fc133fc4c [file] [log] [blame]
Adam Lesinski40e8eef2014-09-16 14:43:29 -07001/*
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"
Adam Lesinski42eea272015-01-15 17:01:39 -080026#include "SplitSelector.h"
Adam Lesinski40e8eef2014-09-16 14:43:29 -070027
28#include <androidfw/AssetManager.h>
29#include <androidfw/ResourceTypes.h>
30#include <utils/KeyedVector.h>
31#include <utils/Vector.h>
32
33using namespace android;
34
35namespace split {
36
37static void usage() {
38 fprintf(stderr,
39 "split-select --help\n"
Adam Lesinski42eea272015-01-15 17:01:39 -080040 "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"
Adam Lesinski40e8eef2014-09-16 14:43:29 -070042 "\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"
Adam Lesinski42eea272015-01-15 17:01:39 -080046 " --base <path/to/apk> Specifies the base APK, from which all Split APKs must be based off.\n"
Adam Lesinski40e8eef2014-09-16 14:43:29 -070047 " --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
56static 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
Adam Lesinski40e8eef2014-09-16 14:43:29 -070066Vector<SplitDescription> select(const SplitDescription& target, const Vector<SplitDescription>& splits) {
67 const SplitSelector selector(splits);
68 return selector.getBestSplits(target);
69}
70
Adam Lesinski42eea272015-01-15 17:01:39 -080071void generate(const KeyedVector<String8, Vector<SplitDescription> >& splits, const String8& base) {
Adam Lesinski40e8eef2014-09-16 14:43:29 -070072 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);
Adam Lesinski42eea272015-01-15 17:01:39 -080078 KeyedVector<SplitDescription, sp<Rule> > rules(selector.getRules());
Adam Lesinski40e8eef2014-09-16 14:43:29 -070079
Adam Lesinski42eea272015-01-15 17:01:39 -080080 bool first = true;
Adam Lesinski40e8eef2014-09-16 14:43:29 -070081 fprintf(stdout, "[\n");
82 for (size_t i = 0; i < apkSplitCount; i++) {
Adam Lesinski42eea272015-01-15 17:01:39 -080083 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
Adam Lesinski40e8eef2014-09-16 14:43:29 -070093 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);
Adam Lesinski42eea272015-01-15 17:01:39 -0800101 fprintf(stdout, " {\n \"path\": \"%s\",\n \"rules\": %s\n }",
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700102 splits.keyAt(i).string(),
Adam Lesinski42eea272015-01-15 17:01:39 -0800103 masterRule->toJson(2).string());
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700104 }
Adam Lesinski42eea272015-01-15 17:01:39 -0800105 fprintf(stdout, "\n]\n");
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700106}
107
108static 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
Adam Lesinski42eea272015-01-15 17:01:39 -0800116struct AppInfo {
117 int versionCode;
118 int minSdkVersion;
119 bool multiArch;
120};
121
122static 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 String8 minSdk8(xml.getStrings().string8ObjectAt(idx));
186 char* endPtr;
187 int minSdk = strtol(minSdk8.string(), &endPtr, 10);
188 if (endPtr != minSdk8.string() + minSdk8.size()) {
189 fprintf(stderr, "warning: failed to parse android:minSdkVersion '%s'\n",
190 minSdk8.string());
191 } else {
192 outInfo.minSdkVersion = minSdk;
193 }
194 } else {
195 fprintf(stderr, "warning: unrecognized value for android:minSdkVersion.\n");
196 }
197 }
198 }
199 }
200
201 delete asset;
202 return true;
203}
204
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700205static Vector<SplitDescription> extractSplitDescriptionsFromApk(const String8& path) {
206 AssetManager assetManager;
207 Vector<SplitDescription> splits;
208 int32_t cookie = 0;
209 if (!assetManager.addAssetPath(path, &cookie)) {
210 return splits;
211 }
212
213 const ResTable& res = assetManager.getResources(false);
214 if (res.getError() == NO_ERROR) {
215 Vector<ResTable_config> configs;
Adam Lesinski42eea272015-01-15 17:01:39 -0800216 res.getConfigurations(&configs, true);
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700217 const size_t configCount = configs.size();
218 for (size_t i = 0; i < configCount; i++) {
219 splits.add();
220 splits.editTop().config = configs[i];
221 }
222 }
223
224 AssetDir* dir = assetManager.openNonAssetDir(cookie, "lib");
225 if (dir != NULL) {
226 const size_t fileCount = dir->getFileCount();
227 for (size_t i = 0; i < fileCount; i++) {
228 splits.add();
229 Vector<String8> parts = AaptUtil::splitAndLowerCase(dir->getFileName(i), '-');
230 if (parseAbi(parts, 0, &splits.editTop()) < 0) {
231 fprintf(stderr, "Malformed library %s\n", dir->getFileName(i).string());
232 splits.pop();
233 }
234 }
235 delete dir;
236 }
237 return splits;
238}
239
240static int main(int argc, char** argv) {
241 // Skip over the first argument.
242 argc--;
243 argv++;
244
245 bool generateFlag = false;
246 String8 targetConfigStr;
247 Vector<String8> splitApkPaths;
Adam Lesinski42eea272015-01-15 17:01:39 -0800248 String8 baseApkPath;
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700249 while (argc > 0) {
250 const String8 arg(*argv);
251 if (arg == "--target") {
252 argc--;
253 argv++;
254 if (argc < 1) {
Adam Lesinski42eea272015-01-15 17:01:39 -0800255 fprintf(stderr, "error: missing parameter for --target.\n");
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700256 usage();
257 return 1;
258 }
259 targetConfigStr.setTo(*argv);
260 } else if (arg == "--split") {
261 argc--;
262 argv++;
263 if (argc < 1) {
Adam Lesinski42eea272015-01-15 17:01:39 -0800264 fprintf(stderr, "error: missing parameter for --split.\n");
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700265 usage();
266 return 1;
267 }
268 splitApkPaths.add(String8(*argv));
Adam Lesinski42eea272015-01-15 17:01:39 -0800269 } else if (arg == "--base") {
270 argc--;
271 argv++;
272 if (argc < 1) {
273 fprintf(stderr, "error: missing parameter for --base.\n");
274 usage();
275 return 1;
276 }
277
278 if (baseApkPath.size() > 0) {
279 fprintf(stderr, "error: multiple --base flags not allowed.\n");
280 usage();
281 return 1;
282 }
283 baseApkPath.setTo(*argv);
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700284 } else if (arg == "--generate") {
285 generateFlag = true;
286 } else if (arg == "--help") {
287 help();
288 return 0;
289 } else {
Adam Lesinski42eea272015-01-15 17:01:39 -0800290 fprintf(stderr, "error: unknown argument '%s'.\n", arg.string());
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700291 usage();
292 return 1;
293 }
294 argc--;
295 argv++;
296 }
297
298 if (!generateFlag && targetConfigStr == "") {
299 usage();
300 return 1;
301 }
302
Adam Lesinski42eea272015-01-15 17:01:39 -0800303 if (baseApkPath.size() == 0) {
304 fprintf(stderr, "error: missing --base argument.\n");
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700305 usage();
306 return 1;
307 }
308
Adam Lesinski42eea272015-01-15 17:01:39 -0800309 // Find out some details about the base APK.
310 AppInfo baseAppInfo;
311 if (!getAppInfo(baseApkPath, baseAppInfo)) {
312 fprintf(stderr, "error: unable to read base APK: '%s'.\n", baseApkPath.string());
313 return 1;
314 }
315
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700316 SplitDescription targetSplit;
317 if (!generateFlag) {
318 if (!SplitDescription::parse(targetConfigStr, &targetSplit)) {
Adam Lesinski42eea272015-01-15 17:01:39 -0800319 fprintf(stderr, "error: invalid --target config: '%s'.\n",
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700320 targetConfigStr.string());
321 usage();
322 return 1;
323 }
324
325 // We don't want to match on things that will change at run-time
326 // (orientation, w/h, etc.).
327 removeRuntimeQualifiers(&targetSplit.config);
328 }
329
Adam Lesinski42eea272015-01-15 17:01:39 -0800330 splitApkPaths.add(baseApkPath);
331
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700332 KeyedVector<String8, Vector<SplitDescription> > apkPathSplitMap;
333 KeyedVector<SplitDescription, String8> splitApkPathMap;
334 Vector<SplitDescription> splitConfigs;
335 const size_t splitCount = splitApkPaths.size();
336 for (size_t i = 0; i < splitCount; i++) {
337 Vector<SplitDescription> splits = extractSplitDescriptionsFromApk(splitApkPaths[i]);
338 if (splits.isEmpty()) {
Adam Lesinski42eea272015-01-15 17:01:39 -0800339 fprintf(stderr, "error: invalid --split path: '%s'. No splits found.\n",
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700340 splitApkPaths[i].string());
341 usage();
342 return 1;
343 }
344 apkPathSplitMap.replaceValueFor(splitApkPaths[i], splits);
345 const size_t apkSplitDescriptionCount = splits.size();
346 for (size_t j = 0; j < apkSplitDescriptionCount; j++) {
347 splitApkPathMap.replaceValueFor(splits[j], splitApkPaths[i]);
348 }
349 splitConfigs.appendVector(splits);
350 }
351
352 if (!generateFlag) {
353 Vector<SplitDescription> matchingConfigs = select(targetSplit, splitConfigs);
354 const size_t matchingConfigCount = matchingConfigs.size();
355 SortedVector<String8> matchingSplitPaths;
356 for (size_t i = 0; i < matchingConfigCount; i++) {
357 matchingSplitPaths.add(splitApkPathMap.valueFor(matchingConfigs[i]));
358 }
359
360 const size_t matchingSplitApkPathCount = matchingSplitPaths.size();
361 for (size_t i = 0; i < matchingSplitApkPathCount; i++) {
Adam Lesinski42eea272015-01-15 17:01:39 -0800362 if (matchingSplitPaths[i] != baseApkPath) {
363 fprintf(stdout, "%s\n", matchingSplitPaths[i].string());
364 }
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700365 }
366 } else {
Adam Lesinski42eea272015-01-15 17:01:39 -0800367 generate(apkPathSplitMap, baseApkPath);
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700368 }
369 return 0;
370}
371
372} // namespace split
373
374int main(int argc, char** argv) {
375 return split::main(argc, argv);
376}