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.
4package dx;
5
6import java.io.File;
7import java.io.IOException;
8import org.gradle.api.Action;
9import org.gradle.api.DefaultTask;
10import org.gradle.api.UncheckedIOException;
11import org.gradle.api.file.FileTree;
12import org.gradle.api.tasks.TaskAction;
13import org.gradle.process.ExecSpec;
14import utils.Utils;
15
16public 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            String dxExecutableName = Utils.toolsDir().equals("windows") ? "dx.bat" : "dx";
68            dxExecutable = new File("tools/" + Utils.toolsDir() + "/dx/bin/" + dxExecutableName);
69          }
70          execSpec.setExecutable(dxExecutable);
71          execSpec.args("--dex");
72          execSpec.args("--output");
73          execSpec.args(destination.getCanonicalPath());
74          if (isDebug()) {
75            execSpec.args("--debug");
76          }
77          execSpec.args(source.getFiles());
78        } catch (IOException e) {
79          throw new UncheckedIOException(e);
80        }
81      }
82    });
83  }
84}