4: Create decompiler
This commit is contained in:
@@ -1,11 +1,50 @@
|
|||||||
package com.bartek.esa.decompiler.decompiler;
|
package com.bartek.esa.decompiler.decompiler;
|
||||||
|
|
||||||
|
import com.bartek.esa.error.EsaException;
|
||||||
|
import com.bartek.esa.file.provider.FileProvider;
|
||||||
|
import io.vavr.control.Try;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
public class Decompiler {
|
public class Decompiler {
|
||||||
|
private final FileProvider fileProvider;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public Decompiler() {
|
public Decompiler(FileProvider fileProvider) {
|
||||||
|
|
||||||
|
this.fileProvider = fileProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File decompile(File inputApk) {
|
||||||
|
File target = provideTargetDirectory();
|
||||||
|
Process process = decompile(inputApk, target);
|
||||||
|
waitForProcess(process);
|
||||||
|
checkExitValue(process);
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkExitValue(Process process) {
|
||||||
|
if(process.exitValue() != 0) {
|
||||||
|
throw new EsaException("'apktool' process has finished with non-zero code. Interrupting...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void waitForProcess(Process process) {
|
||||||
|
Try.run(process::waitFor).getOrElseThrow(EsaException::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Process decompile(File inputApk, File target) {
|
||||||
|
return Try.of(() -> Runtime.getRuntime().exec(provideCommandArray(inputApk, target)))
|
||||||
|
.getOrElseThrow(EsaException::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String[] provideCommandArray(File inputApk, File target) {
|
||||||
|
return new String[]{"apktool", "d", inputApk.getAbsolutePath(), "-o", target.getAbsolutePath()};
|
||||||
|
}
|
||||||
|
|
||||||
|
private File provideTargetDirectory() {
|
||||||
|
File tmp = fileProvider.createTemporaryDirectory();
|
||||||
|
return new File(tmp, "apktool_out");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.bartek.esa.decompiler.di;
|
package com.bartek.esa.decompiler.di;
|
||||||
|
|
||||||
import com.bartek.esa.decompiler.decompiler.Decompiler;
|
import com.bartek.esa.decompiler.decompiler.Decompiler;
|
||||||
|
import com.bartek.esa.file.provider.FileProvider;
|
||||||
import dagger.Module;
|
import dagger.Module;
|
||||||
import dagger.Provides;
|
import dagger.Provides;
|
||||||
|
|
||||||
@@ -8,7 +9,7 @@ import dagger.Provides;
|
|||||||
public class DecompilerModule {
|
public class DecompilerModule {
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
public Decompiler decompiler() {
|
public Decompiler decompiler(FileProvider fileProvider) {
|
||||||
return new Decompiler();
|
return new Decompiler(fileProvider);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,12 @@ public class FileProvider {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public File createTemporaryDirectory() {
|
||||||
|
return Try.of(() -> Files.createTempDirectory(null))
|
||||||
|
.getOrElseThrow(EsaException::new)
|
||||||
|
.toFile();
|
||||||
|
}
|
||||||
|
|
||||||
public String readFile(File file) {
|
public String readFile(File file) {
|
||||||
StringBuilder content = new StringBuilder();
|
StringBuilder content = new StringBuilder();
|
||||||
Try.run(() -> Files.lines(file.toPath())
|
Try.run(() -> Files.lines(file.toPath())
|
||||||
|
|||||||
Reference in New Issue
Block a user