blob: d35102e406f77ea563c5beb393292e9ba5d35a30 [file] [log] [blame]
Jake Wharton2d7aab82019-09-13 10:24:26 -04001// 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.
4package smali;
5
6import static java.util.stream.Collectors.toList;
7
Søren Gjessea599e652023-05-03 14:35:06 +02008import com.android.tools.smali.smali.Smali;
9import com.android.tools.smali.smali.SmaliOptions;
Jake Wharton2d7aab82019-09-13 10:24:26 -040010import java.io.File;
11import java.io.IOException;
12import java.io.UncheckedIOException;
13import java.util.List;
Jake Wharton2d7aab82019-09-13 10:24:26 -040014import javax.inject.Inject;
15import org.gradle.api.DefaultTask;
16import org.gradle.api.file.FileTree;
Morten Krogh-Jespersen39a92522023-03-01 20:17:44 +010017import org.gradle.api.file.RegularFileProperty;
18import org.gradle.api.provider.SetProperty;
Jake Wharton2d7aab82019-09-13 10:24:26 -040019import org.gradle.api.tasks.InputFiles;
20import org.gradle.api.tasks.OutputFile;
21import org.gradle.api.tasks.TaskAction;
Morten Krogh-Jespersen39a92522023-03-01 20:17:44 +010022import org.gradle.workers.WorkAction;
23import org.gradle.workers.WorkParameters;
Jake Wharton2d7aab82019-09-13 10:24:26 -040024import org.gradle.workers.WorkerExecutor;
Jake Wharton2d7aab82019-09-13 10:24:26 -040025
26public class SmaliTask extends DefaultTask {
27
28 private final WorkerExecutor workerExecutor;
29
30 private FileTree source;
31 private File destination;
32
33 @Inject
34 public SmaliTask(WorkerExecutor workerExecutor) {
35 this.workerExecutor = workerExecutor;
36 }
37
38 @InputFiles
39 public FileTree getSource() {
40 return source;
41 }
42
43 public void setSource(FileTree source) {
44 this.source = source;
45 }
46
47 @OutputFile
48 public File getDestination() {
49 return destination;
50 }
51
52 public void setDestination(File destination) {
53 this.destination = destination;
54 }
55
56 @TaskAction
57 void exec() {
Morten Krogh-Jespersen39a92522023-03-01 20:17:44 +010058 workerExecutor
59 .noIsolation()
60 .submit(
61 RunSmali.class,
62 parameters -> {
63 parameters.getSources().set(source.getFiles());
64 parameters.getDestination().set(destination);
65 });
Jake Wharton2d7aab82019-09-13 10:24:26 -040066 }
67
Morten Krogh-Jespersen39a92522023-03-01 20:17:44 +010068 public interface RunSmaliParameters extends WorkParameters {
Jake Wharton2d7aab82019-09-13 10:24:26 -040069
Morten Krogh-Jespersen39a92522023-03-01 20:17:44 +010070 SetProperty<File> getSources();
71
72 RegularFileProperty getDestination();
73 }
74
75 public abstract static class RunSmali implements WorkAction<RunSmaliParameters> {
Jake Wharton2d7aab82019-09-13 10:24:26 -040076
77 @Override
Morten Krogh-Jespersen39a92522023-03-01 20:17:44 +010078 public void execute() {
Jake Wharton2d7aab82019-09-13 10:24:26 -040079 try {
Morten Krogh-Jespersen39a92522023-03-01 20:17:44 +010080 RunSmaliParameters parameters = getParameters();
81 List<String> fileNames =
82 parameters.getSources().get().stream().map(File::toString).collect(toList());
Jake Wharton2d7aab82019-09-13 10:24:26 -040083 SmaliOptions options = new SmaliOptions();
Morten Krogh-Jespersen39a92522023-03-01 20:17:44 +010084 options.outputDexFile = parameters.getDestination().getAsFile().get().getCanonicalPath();
Jake Wharton2d7aab82019-09-13 10:24:26 -040085 Smali.assemble(options, fileNames);
86 } catch (IOException e) {
87 throw new UncheckedIOException(e);
88 }
89 }
90 }
91}