blob: 73bfa19a051ac46d60af03cacb1aa3ab7bad9d4a [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 }",
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000102 splits.keyAt(i).c_str(), masterRule->toJson(2).c_str());
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700103 }
Adam Lesinski42eea272015-01-15 17:01:39 -0800104 fprintf(stdout, "\n]\n");
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700105}
106
107static void removeRuntimeQualifiers(ConfigDescription* outConfig) {
108 outConfig->imsi = 0;
109 outConfig->orientation = ResTable_config::ORIENTATION_ANY;
110 outConfig->screenWidth = ResTable_config::SCREENWIDTH_ANY;
111 outConfig->screenHeight = ResTable_config::SCREENHEIGHT_ANY;
112 outConfig->uiMode &= ResTable_config::UI_MODE_NIGHT_ANY;
113}
114
Adam Lesinski42eea272015-01-15 17:01:39 -0800115struct AppInfo {
116 int versionCode;
117 int minSdkVersion;
118 bool multiArch;
119};
120
121static bool getAppInfo(const String8& path, AppInfo& outInfo) {
122 memset(&outInfo, 0, sizeof(outInfo));
123
124 AssetManager assetManager;
125 int32_t cookie = 0;
126 if (!assetManager.addAssetPath(path, &cookie)) {
127 return false;
128 }
129
130 Asset* asset = assetManager.openNonAsset(cookie, "AndroidManifest.xml", Asset::ACCESS_BUFFER);
131 if (asset == NULL) {
132 return false;
133 }
134
135 ResXMLTree xml;
136 if (xml.setTo(asset->getBuffer(true), asset->getLength(), false) != NO_ERROR) {
137 delete asset;
138 return false;
139 }
140
141 const String16 kAndroidNamespace("http://schemas.android.com/apk/res/android");
142 const String16 kManifestTag("manifest");
143 const String16 kApplicationTag("application");
144 const String16 kUsesSdkTag("uses-sdk");
145 const String16 kVersionCodeAttr("versionCode");
146 const String16 kMultiArchAttr("multiArch");
147 const String16 kMinSdkVersionAttr("minSdkVersion");
148
149 ResXMLParser::event_code_t event;
150 while ((event = xml.next()) != ResXMLParser::BAD_DOCUMENT &&
151 event != ResXMLParser::END_DOCUMENT) {
152 if (event != ResXMLParser::START_TAG) {
153 continue;
154 }
155
156 size_t len;
157 const char16_t* name = xml.getElementName(&len);
158 String16 name16(name, len);
159 if (name16 == kManifestTag) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000160 ssize_t idx = xml.indexOfAttribute(kAndroidNamespace.c_str(), kAndroidNamespace.size(),
161 kVersionCodeAttr.c_str(), kVersionCodeAttr.size());
Adam Lesinski42eea272015-01-15 17:01:39 -0800162 if (idx >= 0) {
163 outInfo.versionCode = xml.getAttributeData(idx);
164 }
165
166 } else if (name16 == kApplicationTag) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000167 ssize_t idx = xml.indexOfAttribute(kAndroidNamespace.c_str(), kAndroidNamespace.size(),
168 kMultiArchAttr.c_str(), kMultiArchAttr.size());
Adam Lesinski42eea272015-01-15 17:01:39 -0800169 if (idx >= 0) {
170 outInfo.multiArch = xml.getAttributeData(idx) != 0;
171 }
172
173 } else if (name16 == kUsesSdkTag) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000174 ssize_t idx =
175 xml.indexOfAttribute(kAndroidNamespace.c_str(), kAndroidNamespace.size(),
176 kMinSdkVersionAttr.c_str(), kMinSdkVersionAttr.size());
Adam Lesinski42eea272015-01-15 17:01:39 -0800177 if (idx >= 0) {
178 uint16_t type = xml.getAttributeDataType(idx);
179 if (type >= Res_value::TYPE_FIRST_INT && type <= Res_value::TYPE_LAST_INT) {
180 outInfo.minSdkVersion = xml.getAttributeData(idx);
181 } else if (type == Res_value::TYPE_STRING) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000182 auto minSdk8 = xml.getStrings().string8ObjectAt(idx);
183 if (!minSdk8.has_value()) {
184 fprintf(stderr, "warning: failed to retrieve android:minSdkVersion.\n");
Adam Lesinski42eea272015-01-15 17:01:39 -0800185 } else {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000186 char *endPtr;
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000187 int minSdk = strtol(minSdk8->c_str(), &endPtr, 10);
188 if (endPtr != minSdk8->c_str() + minSdk8->size()) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000189 fprintf(stderr, "warning: failed to parse android:minSdkVersion '%s'\n",
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000190 minSdk8->c_str());
Ryan Mitchell80094e32020-11-16 23:08:18 +0000191 } else {
192 outInfo.minSdkVersion = minSdk;
193 }
Adam Lesinski42eea272015-01-15 17:01:39 -0800194 }
195 } else {
196 fprintf(stderr, "warning: unrecognized value for android:minSdkVersion.\n");
197 }
198 }
199 }
200 }
201
202 delete asset;
203 return true;
204}
205
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700206static Vector<SplitDescription> extractSplitDescriptionsFromApk(const String8& path) {
207 AssetManager assetManager;
208 Vector<SplitDescription> splits;
209 int32_t cookie = 0;
210 if (!assetManager.addAssetPath(path, &cookie)) {
211 return splits;
212 }
213
214 const ResTable& res = assetManager.getResources(false);
215 if (res.getError() == NO_ERROR) {
216 Vector<ResTable_config> configs;
Adam Lesinski42eea272015-01-15 17:01:39 -0800217 res.getConfigurations(&configs, true);
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700218 const size_t configCount = configs.size();
219 for (size_t i = 0; i < configCount; i++) {
220 splits.add();
221 splits.editTop().config = configs[i];
222 }
223 }
224
225 AssetDir* dir = assetManager.openNonAssetDir(cookie, "lib");
226 if (dir != NULL) {
227 const size_t fileCount = dir->getFileCount();
228 for (size_t i = 0; i < fileCount; i++) {
229 splits.add();
230 Vector<String8> parts = AaptUtil::splitAndLowerCase(dir->getFileName(i), '-');
231 if (parseAbi(parts, 0, &splits.editTop()) < 0) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000232 fprintf(stderr, "Malformed library %s\n", dir->getFileName(i).c_str());
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700233 splits.pop();
234 }
235 }
236 delete dir;
237 }
238 return splits;
239}
240
241static int main(int argc, char** argv) {
242 // Skip over the first argument.
243 argc--;
244 argv++;
245
246 bool generateFlag = false;
247 String8 targetConfigStr;
248 Vector<String8> splitApkPaths;
Adam Lesinski42eea272015-01-15 17:01:39 -0800249 String8 baseApkPath;
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700250 while (argc > 0) {
251 const String8 arg(*argv);
252 if (arg == "--target") {
253 argc--;
254 argv++;
255 if (argc < 1) {
Adam Lesinski42eea272015-01-15 17:01:39 -0800256 fprintf(stderr, "error: missing parameter for --target.\n");
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700257 usage();
258 return 1;
259 }
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +0000260 targetConfigStr = *argv;
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700261 } else if (arg == "--split") {
262 argc--;
263 argv++;
264 if (argc < 1) {
Adam Lesinski42eea272015-01-15 17:01:39 -0800265 fprintf(stderr, "error: missing parameter for --split.\n");
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700266 usage();
267 return 1;
268 }
269 splitApkPaths.add(String8(*argv));
Adam Lesinski42eea272015-01-15 17:01:39 -0800270 } else if (arg == "--base") {
271 argc--;
272 argv++;
273 if (argc < 1) {
274 fprintf(stderr, "error: missing parameter for --base.\n");
275 usage();
276 return 1;
277 }
278
279 if (baseApkPath.size() > 0) {
280 fprintf(stderr, "error: multiple --base flags not allowed.\n");
281 usage();
282 return 1;
283 }
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +0000284 baseApkPath = *argv;
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700285 } else if (arg == "--generate") {
286 generateFlag = true;
287 } else if (arg == "--help") {
288 help();
289 return 0;
290 } else {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000291 fprintf(stderr, "error: unknown argument '%s'.\n", arg.c_str());
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700292 usage();
293 return 1;
294 }
295 argc--;
296 argv++;
297 }
298
299 if (!generateFlag && targetConfigStr == "") {
300 usage();
301 return 1;
302 }
303
Adam Lesinski42eea272015-01-15 17:01:39 -0800304 if (baseApkPath.size() == 0) {
305 fprintf(stderr, "error: missing --base argument.\n");
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700306 usage();
307 return 1;
308 }
309
Adam Lesinski42eea272015-01-15 17:01:39 -0800310 // Find out some details about the base APK.
311 AppInfo baseAppInfo;
312 if (!getAppInfo(baseApkPath, baseAppInfo)) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000313 fprintf(stderr, "error: unable to read base APK: '%s'.\n", baseApkPath.c_str());
Adam Lesinski42eea272015-01-15 17:01:39 -0800314 return 1;
315 }
316
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700317 SplitDescription targetSplit;
318 if (!generateFlag) {
319 if (!SplitDescription::parse(targetConfigStr, &targetSplit)) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000320 fprintf(stderr, "error: invalid --target config: '%s'.\n", targetConfigStr.c_str());
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700321 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",
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000340 splitApkPaths[i].c_str());
Adam Lesinski40e8eef2014-09-16 14:43:29 -0700341 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) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000363 fprintf(stdout, "%s\n", matchingSplitPaths[i].c_str());
Adam Lesinski42eea272015-01-15 17:01:39 -0800364 }
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}