Jake Wharton | 2d7aab8 | 2019-09-13 10:24:26 -0400 | [diff] [blame] | 1 | // Copyright (c) 2019, 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 smali; |
| 5 | |
| 6 | import static java.util.stream.Collectors.toList; |
| 7 | |
| 8 | import java.io.File; |
| 9 | import java.io.IOException; |
| 10 | import java.io.UncheckedIOException; |
| 11 | import java.util.List; |
| 12 | import java.util.Set; |
| 13 | import javax.inject.Inject; |
| 14 | import org.gradle.api.DefaultTask; |
| 15 | import org.gradle.api.file.FileTree; |
| 16 | import org.gradle.api.tasks.InputFiles; |
| 17 | import org.gradle.api.tasks.OutputFile; |
| 18 | import org.gradle.api.tasks.TaskAction; |
| 19 | import org.gradle.workers.IsolationMode; |
| 20 | import org.gradle.workers.WorkerExecutor; |
| 21 | import org.jf.smali.Smali; |
| 22 | import org.jf.smali.SmaliOptions; |
| 23 | |
| 24 | public class SmaliTask extends DefaultTask { |
| 25 | |
| 26 | private final WorkerExecutor workerExecutor; |
| 27 | |
| 28 | private FileTree source; |
| 29 | private File destination; |
| 30 | |
| 31 | @Inject |
| 32 | public SmaliTask(WorkerExecutor workerExecutor) { |
| 33 | this.workerExecutor = workerExecutor; |
| 34 | } |
| 35 | |
| 36 | @InputFiles |
| 37 | public FileTree getSource() { |
| 38 | return source; |
| 39 | } |
| 40 | |
| 41 | public void setSource(FileTree source) { |
| 42 | this.source = source; |
| 43 | } |
| 44 | |
| 45 | @OutputFile |
| 46 | public File getDestination() { |
| 47 | return destination; |
| 48 | } |
| 49 | |
| 50 | public void setDestination(File destination) { |
| 51 | this.destination = destination; |
| 52 | } |
| 53 | |
| 54 | @TaskAction |
| 55 | void exec() { |
| 56 | workerExecutor.submit(RunSmali.class, config -> { |
| 57 | config.setIsolationMode(IsolationMode.NONE); |
| 58 | config.params(source.getFiles(), destination); |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | public static class RunSmali implements Runnable { |
| 63 | private final Set<File> sources; |
| 64 | private final File destination; |
| 65 | |
| 66 | @Inject |
| 67 | public RunSmali(Set<File> sources, File destination) { |
| 68 | this.sources = sources; |
| 69 | this.destination = destination; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void run() { |
| 74 | try { |
| 75 | List<String> fileNames = sources.stream().map(File::toString).collect(toList()); |
| 76 | SmaliOptions options = new SmaliOptions(); |
| 77 | options.outputDexFile = destination.getCanonicalPath(); |
| 78 | Smali.assemble(options, fileNames); |
| 79 | } catch (IOException e) { |
| 80 | throw new UncheckedIOException(e); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |