| 1 |
/* |
| 2 |
* Copyright (C) 2015 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.ide.common.process.BaseProcessOutputHandler; |
| 21 |
import com.android.ide.common.process.ProcessException; |
| 22 |
import com.android.ide.common.process.ProcessExecutor; |
| 23 |
import com.android.ide.common.process.ProcessInfoBuilder; |
| 24 |
import com.android.ide.common.process.ProcessOutput; |
| 25 |
import com.android.utils.LineCollector; |
| 26 |
|
| 27 |
import java.io.File; |
| 28 |
import java.util.ArrayList; |
| 29 |
import java.util.Collection; |
| 30 |
import java.util.List; |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Abstraction to the split-select tool. |
| 35 |
*/ |
| 36 |
public class SplitSelectTool { |
| 37 |
|
| 38 |
/** |
| 39 |
* Runs the native split-select tool given the main APK, the list of pure split APKs and the |
| 40 |
* targeted device characteristics. The tool should select the minimum set of the split APKs |
| 41 |
* that should be installed in that device. |
| 42 |
* |
| 43 |
* @param processExecutor a reusable process executor instance. |
| 44 |
* @param splitSelectExec the pointer to the split-select tool in this machine. |
| 45 |
* @param deviceConfig the targed device |
| 46 |
* @param mainApkPath the path the main application APK. |
| 47 |
* @param splitApksPath the path to all the pure split APKs. |
| 48 |
* @return the set of APK to successfully install the application on the targeted device. |
| 49 |
* @throws ProcessException |
| 50 |
*/ |
| 51 |
@NonNull |
| 52 |
public static List<String> splitSelect( |
| 53 |
@NonNull ProcessExecutor processExecutor, |
| 54 |
@NonNull File splitSelectExec, |
| 55 |
@NonNull String deviceConfig, |
| 56 |
@NonNull String mainApkPath, |
| 57 |
@NonNull Collection<String> splitApksPath) throws ProcessException { |
| 58 |
|
| 59 |
ProcessInfoBuilder processBuilder = new ProcessInfoBuilder(); |
| 60 |
processBuilder.setExecutable(splitSelectExec); |
| 61 |
|
| 62 |
processBuilder.addArgs("--target", deviceConfig); |
| 63 |
|
| 64 |
// specify the main APK parameter |
| 65 |
processBuilder.addArgs("--base", mainApkPath); |
| 66 |
|
| 67 |
// and the splits... |
| 68 |
for (String apkPath : splitApksPath) { |
| 69 |
processBuilder.addArgs("--split", apkPath); |
| 70 |
} |
| 71 |
SplitSelectOutputHandler outputHandler = |
| 72 |
new SplitSelectOutputHandler(); |
| 73 |
|
| 74 |
processExecutor.execute(processBuilder.createProcess(), outputHandler) |
| 75 |
.rethrowFailure() |
| 76 |
.assertNormalExitValue(); |
| 77 |
|
| 78 |
return outputHandler.getResultApks(); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
private static class SplitSelectOutputHandler extends BaseProcessOutputHandler { |
| 83 |
|
| 84 |
private final List<String> resultApks = new ArrayList<String>(); |
| 85 |
|
| 86 |
@NonNull |
| 87 |
public List<String> getResultApks() { |
| 88 |
return resultApks; |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public void handleOutput(@NonNull ProcessOutput processOutput) throws ProcessException { |
| 93 |
if (processOutput instanceof BaseProcessOutput) { |
| 94 |
BaseProcessOutput impl = (BaseProcessOutput) processOutput; |
| 95 |
LineCollector lineCollector = new LineCollector(); |
| 96 |
impl.processStandardOutputLines(lineCollector); |
| 97 |
resultApks.addAll(lineCollector.getResult()); |
| 98 |
String stderr = impl.getErrorOutputAsString(); |
| 99 |
if (!stderr.isEmpty()) { |
| 100 |
throw new RuntimeException("split-select:" + stderr); |
| 101 |
} |
| 102 |
} else { |
| 103 |
throw new IllegalArgumentException( |
| 104 |
"processOutput was not created by this handler."); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |