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 | |
Søren Gjesse | a599e65 | 2023-05-03 14:35:06 +0200 | [diff] [blame^] | 8 | import com.android.tools.smali.smali.Smali; |
| 9 | import com.android.tools.smali.smali.SmaliOptions; |
Jake Wharton | 2d7aab8 | 2019-09-13 10:24:26 -0400 | [diff] [blame] | 10 | import java.io.File; |
| 11 | import java.io.IOException; |
| 12 | import java.io.UncheckedIOException; |
| 13 | import java.util.List; |
Jake Wharton | 2d7aab8 | 2019-09-13 10:24:26 -0400 | [diff] [blame] | 14 | import javax.inject.Inject; |
| 15 | import org.gradle.api.DefaultTask; |
| 16 | import org.gradle.api.file.FileTree; |
Morten Krogh-Jespersen | 39a9252 | 2023-03-01 20:17:44 +0100 | [diff] [blame] | 17 | import org.gradle.api.file.RegularFileProperty; |
| 18 | import org.gradle.api.provider.SetProperty; |
Jake Wharton | 2d7aab8 | 2019-09-13 10:24:26 -0400 | [diff] [blame] | 19 | import org.gradle.api.tasks.InputFiles; |
| 20 | import org.gradle.api.tasks.OutputFile; |
| 21 | import org.gradle.api.tasks.TaskAction; |
Morten Krogh-Jespersen | 39a9252 | 2023-03-01 20:17:44 +0100 | [diff] [blame] | 22 | import org.gradle.workers.WorkAction; |
| 23 | import org.gradle.workers.WorkParameters; |
Jake Wharton | 2d7aab8 | 2019-09-13 10:24:26 -0400 | [diff] [blame] | 24 | import org.gradle.workers.WorkerExecutor; |
Jake Wharton | 2d7aab8 | 2019-09-13 10:24:26 -0400 | [diff] [blame] | 25 | |
| 26 | public 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-Jespersen | 39a9252 | 2023-03-01 20:17:44 +0100 | [diff] [blame] | 58 | workerExecutor |
| 59 | .noIsolation() |
| 60 | .submit( |
| 61 | RunSmali.class, |
| 62 | parameters -> { |
| 63 | parameters.getSources().set(source.getFiles()); |
| 64 | parameters.getDestination().set(destination); |
| 65 | }); |
Jake Wharton | 2d7aab8 | 2019-09-13 10:24:26 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Morten Krogh-Jespersen | 39a9252 | 2023-03-01 20:17:44 +0100 | [diff] [blame] | 68 | public interface RunSmaliParameters extends WorkParameters { |
Jake Wharton | 2d7aab8 | 2019-09-13 10:24:26 -0400 | [diff] [blame] | 69 | |
Morten Krogh-Jespersen | 39a9252 | 2023-03-01 20:17:44 +0100 | [diff] [blame] | 70 | SetProperty<File> getSources(); |
| 71 | |
| 72 | RegularFileProperty getDestination(); |
| 73 | } |
| 74 | |
| 75 | public abstract static class RunSmali implements WorkAction<RunSmaliParameters> { |
Jake Wharton | 2d7aab8 | 2019-09-13 10:24:26 -0400 | [diff] [blame] | 76 | |
| 77 | @Override |
Morten Krogh-Jespersen | 39a9252 | 2023-03-01 20:17:44 +0100 | [diff] [blame] | 78 | public void execute() { |
Jake Wharton | 2d7aab8 | 2019-09-13 10:24:26 -0400 | [diff] [blame] | 79 | try { |
Morten Krogh-Jespersen | 39a9252 | 2023-03-01 20:17:44 +0100 | [diff] [blame] | 80 | RunSmaliParameters parameters = getParameters(); |
| 81 | List<String> fileNames = |
| 82 | parameters.getSources().get().stream().map(File::toString).collect(toList()); |
Jake Wharton | 2d7aab8 | 2019-09-13 10:24:26 -0400 | [diff] [blame] | 83 | SmaliOptions options = new SmaliOptions(); |
Morten Krogh-Jespersen | 39a9252 | 2023-03-01 20:17:44 +0100 | [diff] [blame] | 84 | options.outputDexFile = parameters.getDestination().getAsFile().get().getCanonicalPath(); |
Jake Wharton | 2d7aab8 | 2019-09-13 10:24:26 -0400 | [diff] [blame] | 85 | Smali.assemble(options, fileNames); |
| 86 | } catch (IOException e) { |
| 87 | throw new UncheckedIOException(e); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |