xref: /aosp_15_r20/frameworks/base/tools/split-select/Main.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
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 
usage()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 
help()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 
select(const SplitDescription & target,const Vector<SplitDescription> & splits)66 Vector<SplitDescription> select(const SplitDescription& target, const Vector<SplitDescription>& splits) {
67     const SplitSelector selector(splits);
68     return selector.getBestSplits(target);
69 }
70 
generate(const KeyedVector<String8,Vector<SplitDescription>> & splits,const String8 & base)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).c_str(), masterRule->toJson(2).c_str());
103     }
104     fprintf(stdout, "\n]\n");
105 }
106 
removeRuntimeQualifiers(ConfigDescription * outConfig)107 static 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 
115 struct AppInfo {
116     int versionCode;
117     int minSdkVersion;
118     bool multiArch;
119 };
120 
getAppInfo(const String8 & path,AppInfo & outInfo)121 static 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) {
160             ssize_t idx = xml.indexOfAttribute(kAndroidNamespace.c_str(), kAndroidNamespace.size(),
161                                                kVersionCodeAttr.c_str(), kVersionCodeAttr.size());
162             if (idx >= 0) {
163                 outInfo.versionCode = xml.getAttributeData(idx);
164             }
165 
166         } else if (name16 == kApplicationTag) {
167             ssize_t idx = xml.indexOfAttribute(kAndroidNamespace.c_str(), kAndroidNamespace.size(),
168                                                kMultiArchAttr.c_str(), kMultiArchAttr.size());
169             if (idx >= 0) {
170                 outInfo.multiArch = xml.getAttributeData(idx) != 0;
171             }
172 
173         } else if (name16 == kUsesSdkTag) {
174             ssize_t idx =
175                     xml.indexOfAttribute(kAndroidNamespace.c_str(), kAndroidNamespace.size(),
176                                          kMinSdkVersionAttr.c_str(), kMinSdkVersionAttr.size());
177             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) {
182                     auto minSdk8 = xml.getStrings().string8ObjectAt(idx);
183                     if (!minSdk8.has_value()) {
184                         fprintf(stderr, "warning: failed to retrieve android:minSdkVersion.\n");
185                     } else {
186                         char *endPtr;
187                         int minSdk = strtol(minSdk8->c_str(), &endPtr, 10);
188                         if (endPtr != minSdk8->c_str() + minSdk8->size()) {
189                             fprintf(stderr, "warning: failed to parse android:minSdkVersion '%s'\n",
190                                     minSdk8->c_str());
191                         } else {
192                             outInfo.minSdkVersion = minSdk;
193                         }
194                     }
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 
extractSplitDescriptionsFromApk(const String8 & path)206 static 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;
217         res.getConfigurations(&configs, true);
218         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) {
232                 fprintf(stderr, "Malformed library %s\n", dir->getFileName(i).c_str());
233                 splits.pop();
234             }
235         }
236         delete dir;
237     }
238     return splits;
239 }
240 
main(int argc,char ** argv)241 static 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;
249     String8 baseApkPath;
250     while (argc > 0) {
251         const String8 arg(*argv);
252         if (arg == "--target") {
253             argc--;
254             argv++;
255             if (argc < 1) {
256                 fprintf(stderr, "error: missing parameter for --target.\n");
257                 usage();
258                 return 1;
259             }
260             targetConfigStr = *argv;
261         } else if (arg == "--split") {
262             argc--;
263             argv++;
264             if (argc < 1) {
265                 fprintf(stderr, "error: missing parameter for --split.\n");
266                 usage();
267                 return 1;
268             }
269             splitApkPaths.add(String8(*argv));
270         } 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             }
284             baseApkPath = *argv;
285         } else if (arg == "--generate") {
286             generateFlag = true;
287         } else if (arg == "--help") {
288             help();
289             return 0;
290         } else {
291             fprintf(stderr, "error: unknown argument '%s'.\n", arg.c_str());
292             usage();
293             return 1;
294         }
295         argc--;
296         argv++;
297     }
298 
299     if (!generateFlag && targetConfigStr == "") {
300         usage();
301         return 1;
302     }
303 
304     if (baseApkPath.size() == 0) {
305         fprintf(stderr, "error: missing --base argument.\n");
306         usage();
307         return 1;
308     }
309 
310     // Find out some details about the base APK.
311     AppInfo baseAppInfo;
312     if (!getAppInfo(baseApkPath, baseAppInfo)) {
313         fprintf(stderr, "error: unable to read base APK: '%s'.\n", baseApkPath.c_str());
314         return 1;
315     }
316 
317     SplitDescription targetSplit;
318     if (!generateFlag) {
319         if (!SplitDescription::parse(targetConfigStr, &targetSplit)) {
320             fprintf(stderr, "error: invalid --target config: '%s'.\n", targetConfigStr.c_str());
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 
330     splitApkPaths.add(baseApkPath);
331 
332     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()) {
339             fprintf(stderr, "error: invalid --split path: '%s'. No splits found.\n",
340                     splitApkPaths[i].c_str());
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++) {
362             if (matchingSplitPaths[i] != baseApkPath) {
363                 fprintf(stdout, "%s\n", matchingSplitPaths[i].c_str());
364             }
365         }
366     } else {
367         generate(apkPathSplitMap, baseApkPath);
368     }
369     return 0;
370 }
371 
372 } // namespace split
373 
main(int argc,char ** argv)374 int main(int argc, char** argv) {
375     return split::main(argc, argv);
376 }
377