Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 1 | // Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | package dx; |
| 5 | |
| 6 | import java.io.File; |
| 7 | import java.io.IOException; |
| 8 | import org.gradle.api.Action; |
| 9 | import org.gradle.api.DefaultTask; |
| 10 | import org.gradle.api.UncheckedIOException; |
| 11 | import org.gradle.api.file.FileTree; |
| 12 | import org.gradle.api.tasks.TaskAction; |
| 13 | import org.gradle.process.ExecSpec; |
| 14 | import utils.Utils; |
| 15 | |
| 16 | public class Dx extends DefaultTask { |
| 17 | |
| 18 | private FileTree source; |
| 19 | private File destination; |
| 20 | private File dxExecutable; |
| 21 | private boolean debug; |
| 22 | |
| 23 | public FileTree getSource() { |
| 24 | return source; |
| 25 | } |
| 26 | |
| 27 | public void setSource(FileTree source) { |
| 28 | this.source = source; |
| 29 | getInputs().file(source); |
| 30 | } |
| 31 | |
| 32 | public File getDestination() { |
| 33 | return destination; |
| 34 | } |
| 35 | |
| 36 | public void setDestination(File destination) { |
| 37 | this.destination = destination; |
| 38 | File classesFile = destination.toPath().resolve("classes.dex").toFile(); |
| 39 | // The output from running DX is classes.dex in the destination directory. |
| 40 | // TODO(sgjesse): Handle multidex? |
| 41 | getOutputs().file(classesFile); |
| 42 | } |
| 43 | |
| 44 | public File getDxExecutable() { |
| 45 | return dxExecutable; |
| 46 | } |
| 47 | |
| 48 | public void setDxExecutable(File dxExecutable) { |
| 49 | this.dxExecutable = dxExecutable; |
| 50 | } |
| 51 | |
| 52 | public boolean isDebug() { |
| 53 | return debug; |
| 54 | } |
| 55 | |
| 56 | public void setDebug(boolean debug) { |
| 57 | this.debug = debug; |
| 58 | } |
| 59 | |
| 60 | @TaskAction |
| 61 | void exec() { |
| 62 | getProject().exec(new Action<ExecSpec>() { |
| 63 | @Override |
| 64 | public void execute(ExecSpec execSpec) { |
| 65 | try { |
| 66 | if (dxExecutable == null) { |
| 67 | dxExecutable = new File("tools/" + Utils.toolsDir() + "/dx/bin/dx"); |
| 68 | } |
| 69 | execSpec.setExecutable(dxExecutable); |
| 70 | execSpec.args("--dex"); |
| 71 | execSpec.args("--output"); |
| 72 | execSpec.args(destination.getCanonicalPath()); |
| 73 | if (isDebug()) { |
| 74 | execSpec.args("--debug"); |
| 75 | } |
| 76 | execSpec.args(source.getFiles()); |
| 77 | } catch (IOException e) { |
| 78 | throw new UncheckedIOException(e); |
| 79 | } |
| 80 | } |
| 81 | }); |
| 82 | } |
| 83 | } |