Debian Codegraph @

sdk-common/src/main/java/com/android/ide/common/build/SplitOutputMatcher.java

raw
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 package com.android.ide.common.build;
18
19 import com.android.annotations.NonNull;
20 import com.android.annotations.Nullable;
21 import com.android.annotations.VisibleForTesting;
22 import com.android.build.FilterData;
23 import com.android.build.OutputFile;
24 import com.android.build.VariantOutput;
25 import com.android.builder.testing.api.DeviceConfigProvider;
26 import com.android.ide.common.process.ProcessException;
27 import com.android.ide.common.process.ProcessExecutor;
28 import com.android.resources.Density;
29 import com.google.common.base.Joiner;
30 import com.google.common.base.Splitter;
31 import com.google.common.base.Strings;
32 import com.google.common.collect.ImmutableList;
33 import com.google.common.collect.Lists;
34
35 import java.io.File;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.Comparator;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Set;
43
44 /**
45 * Helper class to help with installation of multi-output variants.
46 */
47 public class SplitOutputMatcher {
48
49
50 /**
51 * Determines and return the list of APKs to use based on given device density and abis.
52 *
53 * if there are pure splits, use the split-select tool otherwise revert to store logic.
54 *
55 * @param processExecutor an executor to execute native processes.
56 * @param splitSelectExe the split select tool optionally.
57 * @param deviceConfigProvider the device configuration.
58 * @param outputs the tested variant outpts.
59 * @param variantAbiFilters a list of abi filters applied to the variant. This is used in place
60 * of the outputs, if there is a single output with no abi filters.
61 * If the list is null or empty, then the variant does not restrict ABI
62 * packaging.
63 * @return the list of APK files to install.
64 * @throws ProcessException
65 */
66 @NonNull
67 public static List<File> computeBestOutput(
68 @NonNull ProcessExecutor processExecutor,
69 @Nullable File splitSelectExe,
70 @NonNull DeviceConfigProvider deviceConfigProvider,
71 @NonNull List<? extends VariantOutput> outputs,
72 @Nullable Collection<String> variantAbiFilters) throws ProcessException {
73
74
75 // build the list of APKs.
76 List<String> splitApksPath = new ArrayList<String>();
77 OutputFile mainApk = null;
78 for (VariantOutput output : outputs) {
79 for (OutputFile outputFile : output.getOutputs()) {
80 if (!outputFile.getOutputFile().getAbsolutePath().equals(
81 output.getMainOutputFile().getOutputFile().getAbsolutePath())) {
82
83 splitApksPath.add(outputFile.getOutputFile().getAbsolutePath());
84 }
85 }
86 mainApk = output.getMainOutputFile();
87 }
88 if (mainApk == null) {
89 throw new RuntimeException(
90 "Cannot retrieve the main APK from variant outputs");
91 }
92 if (splitApksPath.isEmpty()) {
93 List<File> apkFiles = new ArrayList<File>();
94
95 // now look for a matching output file
96 List<OutputFile> outputFiles = SplitOutputMatcher.computeBestOutput(
97 outputs,
98 variantAbiFilters,
99 deviceConfigProvider.getDensity(),
100 deviceConfigProvider.getAbis());
101 for (OutputFile outputFile : outputFiles) {
102 apkFiles.add(outputFile.getOutputFile());
103 }
104 return apkFiles;
105 } else {
106 if (splitSelectExe == null) {
107 throw new RuntimeException(
108 "Pure splits installation requires build tools 22 or above");
109 }
110 return computeBestOutput(processExecutor, splitSelectExe, deviceConfigProvider,
111 mainApk.getOutputFile(), splitApksPath);
112 }
113 }
114
115 /**
116 * Determines and return the list of APKs to use based on given device density and abis.
117 *
118 * if there are pure splits, use the split-select tool otherwise revert to store logic.
119 *
120 * @param processExecutor an executor to execute native processes.
121 * @param splitSelectExe the split select tool optionally.
122 * @param deviceConfigProvider the device configuration.
123 * @param mainApk the main apk file.
124 * @param splitApksPath the list of split apks path.
125 * @return the list of APK files to install.
126 * @throws ProcessException
127 */
128 @NonNull
129 public static List<File> computeBestOutput(
130 @NonNull ProcessExecutor processExecutor,
131 @NonNull File splitSelectExe,
132 @NonNull DeviceConfigProvider deviceConfigProvider,
133 @NonNull File mainApk,
134 @NonNull Collection<String> splitApksPath) throws ProcessException {
135
136 // build the list of APKs.
137 if (splitApksPath.isEmpty()) {
138 return ImmutableList.of(mainApk);
139 } else {
140 List<File> apkFiles = new ArrayList<File>();
141
142 Set<String> resultApksPath = new HashSet<String>();
143 for (String abi : deviceConfigProvider.getAbis()) {
144 String deviceConfiguration =
145 prepareConfigFormatMccMnc(deviceConfigProvider.getConfigFor(abi));
146 resultApksPath.addAll(SplitSelectTool.splitSelect(
147 processExecutor,
148 splitSelectExe,
149 deviceConfiguration,
150 mainApk.getAbsolutePath(),
151 splitApksPath));
152 }
153 for (String resultApkPath : resultApksPath) {
154 apkFiles.add(new File(resultApkPath));
155 }
156 // and add back the main APK.
157 apkFiles.add(mainApk);
158 return apkFiles;
159 }
160 }
161
162 /**
163 * Determines and return the list of APKs to use based on given device density and abis.
164 *
165 * This uses the same logic as the store, using two passes:
166 * First, find all the compatible outputs.
167 * Then take the one with the highest versionCode.
168 *
169 * @param outputs the outputs to choose from.
170 * @param variantAbiFilters a list of abi filters applied to the variant. This is used in place
171 * of the outputs, if there is a single output with no abi filters.
172 * If the list is null, then the variant does not restrict ABI
173 * packaging.
174 * @param deviceDensity the density of the device.
175 * @param deviceAbis a list of ABIs supported by the device.
176 * @return the list of APKs to install or null if none are compatible.
177 */
178 @NonNull
179 public static List<OutputFile> computeBestOutput(
180 @NonNull List<? extends VariantOutput> outputs,
181 @Nullable Collection<String> variantAbiFilters,
182 int deviceDensity,
183 @NonNull List<String> deviceAbis) {
184 Density densityEnum = Density.getEnum(deviceDensity);
185
186 String densityValue;
187 if (densityEnum == null) {
188 densityValue = null;
189 } else {
190 densityValue = densityEnum.getResourceValue();
191 }
192
193 // gather all compatible matches.
194 List<VariantOutput> matches = Lists.newArrayList();
195
196 // find a matching output.
197 for (VariantOutput variantOutput : outputs) {
198 for (OutputFile output : variantOutput.getOutputs()) {
199 String densityFilter = getFilter(output, OutputFile.DENSITY);
200 String abiFilter = getFilter(output, OutputFile.ABI);
201
202 if (densityFilter != null && !densityFilter.equals(densityValue)) {
203 continue;
204 }
205
206 if (abiFilter != null && !deviceAbis.contains(abiFilter)) {
207 continue;
208 }
209 matches.add(variantOutput);
210 break;
211 }
212 }
213
214 if (matches.isEmpty()) {
215 return ImmutableList.of();
216 }
217
218 VariantOutput match = Collections.max(matches,
219 (splitOutput, splitOutput2) -> {
220 int rc = splitOutput.getVersionCode() - splitOutput2.getVersionCode();
221 if (rc != 0) {
222 return rc;
223 }
224 int abiOrder1 = getAbiPreferenceOrder(splitOutput, deviceAbis);
225 int abiOrder2 = getAbiPreferenceOrder(splitOutput2, deviceAbis);
226 return abiOrder1 - abiOrder2;
227 });
228
229 OutputFile mainOutputFile = match.getMainOutputFile();
230 return isMainApkCompatibleWithDevice(mainOutputFile, variantAbiFilters, deviceAbis)
231 ? ImmutableList.of(mainOutputFile)
232 : ImmutableList.of();
233 }
234
235 /**
236 * Return the preference score of a VariantOutput for the deviceAbi list.
237 *
238 * Higher score means a better match. Scores returned by different call are only comparable if
239 * the specified deviceAbi is the same.
240 */
241 private static int getAbiPreferenceOrder(VariantOutput variantOutput, List<String> deviceAbi) {
242 String abiFilter = getFilter(variantOutput.getMainOutputFile(), OutputFile.ABI);
243 if (Strings.isNullOrEmpty(abiFilter)) {
244 // Null or empty imply a universal APK, which would return the second highest score.
245 return deviceAbi.size() - 1;
246 }
247 int match = deviceAbi.indexOf(abiFilter);
248 if (match == 0) {
249 // We want to select the output that matches the first deviceAbi. The filtered output
250 // is preferred over universal APK if it matches the first deviceAbi as they are likely
251 // to take a shorter time to build.
252 match = deviceAbi.size(); // highest possible score for the specified deviceAbi.
253 } else if (match > 0) {
254 // Universal APK may contain the best match even though it is not guaranteed, that's
255 // why it is preferred over a filtered output that does not match the best ABI.
256 match = deviceAbi.size() - match - 1;
257 }
258 return match;
259 }
260
261 private static boolean isMainApkCompatibleWithDevice(
262 OutputFile mainOutputFile,
263 Collection<String> variantAbiFilters,
264 Collection<String> deviceAbis) {
265 // so far, we are not dealing with the pure split files...
266 if (getFilter(mainOutputFile, OutputFile.ABI) == null
267 && variantAbiFilters != null
268 && !variantAbiFilters.isEmpty()) {
269 // if we have a match that has no abi filter, and we have variant-level filters, then
270 // we need to make sure that the variant filters are compatible with the device abis.
271 for (String abi : deviceAbis) {
272 if (variantAbiFilters.contains(abi)) {
273 return true;
274 }
275 }
276 return false;
277 }
278 return true;
279 }
280
281 @Nullable
282 private static String getFilter(@NonNull OutputFile outputFile, @NonNull String filterType) {
283 for (FilterData filterData : outputFile.getFilters()) {
284 if (filterData.getFilterType().equals(filterType)) {
285 return filterData.getIdentifier();
286 }
287 }
288 return null;
289 }
290
291 /**
292 * Preparing the configuration string according to the format expected by the split-select
293 * tool. This should not make sure that the configuration string is valid, but just fix
294 * this know issue.
295 *
296 * <p>Devices having api level 21 have a config format that is not compatible with split-select.
297 * The problem is that the split-select tool expects mcc310-mnc260, while the am get-config
298 * command will output 310mcc-260mnc-... (mcc is always before mnc, and both are optional). That
299 * is why the string is processed to match the expected format for these two dimensions.
300 * The rest of the config string is not changed.
301 *
302 * @param deviceConfig device configuration to process
303 * @return device configuration in format expected by the split-select tool
304 */
305 @VisibleForTesting
306 public static String prepareConfigFormatMccMnc(@NonNull String deviceConfig) {
307 Iterable<String> configParts = Splitter.on("-").split(deviceConfig);
308 List<String> outputParts = Lists.newArrayList();
309
310 // we should fix only the first 2 parts of the config, as mcc and mnc
311 // are found only there (they can also be omitted from the config)
312 int processed = 0;
313 for (String part: configParts) {
314 // both mcc and mnc parts are 6 characters long, also start with 3 digits
315 boolean matchingFormat =
316 part.length() == 6
317 && Character.isDigit(part.charAt(0))
318 && Character.isDigit(part.charAt(1))
319 && Character.isDigit(part.charAt(2));
320
321 if (processed == 0 && matchingFormat && part.endsWith("mcc")) {
322 processed = 1;
323 outputParts.add(fixSingleConfigDimension(part, "mcc"));
324 } else if (processed < 2 && matchingFormat && part.endsWith("mnc")) {
325 processed = 2;
326 outputParts.add(fixSingleConfigDimension(part, "mnc"));
327 } else {
328 outputParts.add(part);
329 }
330 }
331
332 return Joiner.on("-").join(outputParts);
333 }
334
335 private static String fixSingleConfigDimension(
336 @NonNull String configDimension, @NonNull String dimensionName) {
337 int nameStartIndex = configDimension.lastIndexOf(dimensionName);
338 if (nameStartIndex > 0) {
339 // put the dimension name first, than the rest
340 return dimensionName + configDimension.substring(0, nameStartIndex);
341 } else {
342 // no fix needed
343 return configDimension;
344 }
345 }
346 }