|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 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.facebook.buck.android.dex; |
| 18 | + |
| 19 | +import com.android.tools.r8.CompilationFailedException; |
| 20 | +import com.google.common.collect.ImmutableSet; |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.nio.file.Paths; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.Optional; |
| 27 | +import java.util.logging.Logger; // NOPMD |
| 28 | + |
| 29 | +/** |
| 30 | + * Main entry point for executing {@link com.android.tools.r8.D8Command} calls. |
| 31 | + * |
| 32 | + * <p>Expected usage: {@code this_binary <output_dex_file> <files_to_dex_path> <android_jar_path> |
| 33 | + * options}. |
| 34 | + */ |
| 35 | +public class D8ExecutableMain { |
| 36 | + |
| 37 | + private static final Logger LOG = Logger.getLogger(D8ExecutableMain.class.getName()); |
| 38 | + |
| 39 | + public static void main(String[] args) throws IOException { |
| 40 | + if (args.length < 3) { |
| 41 | + LOG.severe( |
| 42 | + "Must specify an output dex file, a file containing the files to dex, and an android jar path, plus any other optional parameters"); |
| 43 | + System.exit(1); |
| 44 | + } |
| 45 | + |
| 46 | + Path outputDexFile = Paths.get(args[0]); |
| 47 | + Path filesToDexPath = Paths.get(args[1]); |
| 48 | + ImmutableSet<Path> filesToDex = |
| 49 | + Files.readAllLines(filesToDexPath).stream() |
| 50 | + .map(Paths::get) |
| 51 | + .collect(ImmutableSet.toImmutableSet()); |
| 52 | + Path androidJarPath = Paths.get(args[2]); |
| 53 | + |
| 54 | + Optional<Path> primaryDexClassNamesPath = Optional.empty(); |
| 55 | + Optional<Path> referencedResourcesPath = Optional.empty(); |
| 56 | + ImmutableSet.Builder<D8Options> d8OptionsBuilder = ImmutableSet.builder(); |
| 57 | + Collection<Path> classpathFiles = null; |
| 58 | + Optional<String> bucketId = Optional.empty(); |
| 59 | + Optional<Integer> minSdkVersion = Optional.empty(); |
| 60 | + |
| 61 | + for (int argsConsumed = 3; argsConsumed < args.length; argsConsumed++) { |
| 62 | + String arg = args[argsConsumed]; |
| 63 | + |
| 64 | + switch (arg) { |
| 65 | + case "--intermediate": |
| 66 | + d8OptionsBuilder.add(D8Options.INTERMEDIATE); |
| 67 | + break; |
| 68 | + case "--no-optimize": |
| 69 | + d8OptionsBuilder.add(D8Options.NO_OPTIMIZE); |
| 70 | + break; |
| 71 | + case "--force-jumbo": |
| 72 | + d8OptionsBuilder.add(D8Options.FORCE_JUMBO); |
| 73 | + break; |
| 74 | + case "--no-desugar": |
| 75 | + d8OptionsBuilder.add(D8Options.NO_DESUGAR); |
| 76 | + break; |
| 77 | + case "--primary-dex-class-names-path": |
| 78 | + primaryDexClassNamesPath = Optional.of(Paths.get(args[++argsConsumed])); |
| 79 | + break; |
| 80 | + case "--referenced-resources-path": |
| 81 | + referencedResourcesPath = Optional.of(Paths.get(args[++argsConsumed])); |
| 82 | + break; |
| 83 | + case "--classpathFiles": |
| 84 | + Path classpathFilesPath = Paths.get(args[++argsConsumed]); |
| 85 | + classpathFiles = |
| 86 | + Files.readAllLines(classpathFilesPath).stream() |
| 87 | + .map(Paths::get) |
| 88 | + .collect(ImmutableSet.toImmutableSet()); |
| 89 | + break; |
| 90 | + case "--bucket-id": |
| 91 | + bucketId = Optional.of(args[++argsConsumed]); |
| 92 | + break; |
| 93 | + case "--min-sdk-version": |
| 94 | + minSdkVersion = Optional.of(Integer.parseInt(args[++argsConsumed])); |
| 95 | + break; |
| 96 | + default: |
| 97 | + throw new RuntimeException("Unknown arg: " + arg); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + try { |
| 102 | + Collection<String> referencedResources = |
| 103 | + D8Utils.runD8Command( |
| 104 | + new D8Utils.D8DiagnosticsHandler(), |
| 105 | + outputDexFile, |
| 106 | + filesToDex, |
| 107 | + d8OptionsBuilder.build(), |
| 108 | + primaryDexClassNamesPath, |
| 109 | + androidJarPath, |
| 110 | + classpathFiles, |
| 111 | + bucketId, |
| 112 | + minSdkVersion); |
| 113 | + |
| 114 | + if (referencedResourcesPath.isPresent()) { |
| 115 | + Files.write(referencedResourcesPath.get(), referencedResources); |
| 116 | + } |
| 117 | + } catch (CompilationFailedException e) { |
| 118 | + throw new IOException(e); |
| 119 | + } |
| 120 | + |
| 121 | + System.exit(0); |
| 122 | + } |
| 123 | +} |
0 commit comments