Merge branch '8-fixdecompiler' into 'master'
Resolve "FixDecompiler" Closes #8 See merge request bartlomiej.pluta/esa-tool!7
This commit is contained in:
@@ -21,6 +21,7 @@ dependencies {
|
|||||||
compile "commons-cli:commons-cli:${commonCliVersion}"
|
compile "commons-cli:commons-cli:${commonCliVersion}"
|
||||||
compile "io.vavr:vavr:${vavrVersion}"
|
compile "io.vavr:vavr:${vavrVersion}"
|
||||||
compile "com.github.javaparser:javaparser-core:${javaParserVersion}"
|
compile "com.github.javaparser:javaparser-core:${javaParserVersion}"
|
||||||
|
compile "commons-io:commons-io:${commonsIoVersion}"
|
||||||
}
|
}
|
||||||
|
|
||||||
jar {
|
jar {
|
||||||
|
|||||||
@@ -4,4 +4,5 @@ ext {
|
|||||||
commonCliVersion = '1.4'
|
commonCliVersion = '1.4'
|
||||||
vavrVersion = '1.0.0-alpha-2'
|
vavrVersion = '1.0.0-alpha-2'
|
||||||
javaParserVersion = '3.13.4'
|
javaParserVersion = '3.13.4'
|
||||||
|
commonsIoVersion = '2.6'
|
||||||
}
|
}
|
||||||
@@ -1,50 +1,80 @@
|
|||||||
package com.bartek.esa.decompiler.decompiler;
|
package com.bartek.esa.decompiler.decompiler;
|
||||||
|
|
||||||
import com.bartek.esa.error.EsaException;
|
import com.bartek.esa.decompiler.process.ProcessExecutor;
|
||||||
|
import com.bartek.esa.file.cleaner.FileCleaner;
|
||||||
import com.bartek.esa.file.provider.FileProvider;
|
import com.bartek.esa.file.provider.FileProvider;
|
||||||
import io.vavr.control.Try;
|
import com.bartek.esa.file.zip.ZipTool;
|
||||||
|
import org.apache.commons.io.FilenameUtils;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class Decompiler {
|
public class Decompiler {
|
||||||
|
public static final String XML_FILES_DIR = "xml";
|
||||||
|
public static final String JAVA_FILES_DIR = "java";
|
||||||
|
|
||||||
|
private static final String APK_UNZIPPED_DIR = "apk_unzipped";
|
||||||
|
private static final String JAR_FILES_DIR = "jar";
|
||||||
|
|
||||||
private final FileProvider fileProvider;
|
private final FileProvider fileProvider;
|
||||||
|
private final ProcessExecutor processExecutor;
|
||||||
|
private final ZipTool zipTool;
|
||||||
|
private final FileCleaner fileCleaner;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public Decompiler(FileProvider fileProvider) {
|
public Decompiler(FileProvider fileProvider, ProcessExecutor processExecutor1, ZipTool zipTool, FileCleaner fileCleaner) {
|
||||||
|
|
||||||
this.fileProvider = fileProvider;
|
this.fileProvider = fileProvider;
|
||||||
|
this.processExecutor = processExecutor1;
|
||||||
|
this.zipTool = zipTool;
|
||||||
|
this.fileCleaner = fileCleaner;
|
||||||
}
|
}
|
||||||
|
|
||||||
public File decompile(File inputApk) {
|
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();
|
File tmp = fileProvider.createTemporaryDirectory();
|
||||||
return new File(tmp, "apktool_out");
|
File javaDirectory = new File(tmp, JAVA_FILES_DIR);
|
||||||
|
File xmlDirectory = new File(tmp, XML_FILES_DIR);
|
||||||
|
|
||||||
|
decompileJavaFiles(inputApk, tmp, javaDirectory);
|
||||||
|
decompileXmlFiles(inputApk, xmlDirectory);
|
||||||
|
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void decompileJavaFiles(File inputApk, File tmp, File javaDirectory) {
|
||||||
|
File unzippedApkDirectory = new File(tmp, APK_UNZIPPED_DIR);
|
||||||
|
File jarDirectory = new File(tmp, JAR_FILES_DIR);
|
||||||
|
|
||||||
|
zipTool.unzipArchive(inputApk, unzippedApkDirectory);
|
||||||
|
|
||||||
|
Set<File> dexFiles = fileProvider.getGlobMatchedFiles(unzippedApkDirectory.getAbsolutePath(), "**/*.dex");
|
||||||
|
convertDexToJar(dexFiles, jarDirectory);
|
||||||
|
|
||||||
|
Set<File> jarFiles = fileProvider.getGlobMatchedFiles(jarDirectory.getAbsolutePath(), "**/*.jar");
|
||||||
|
decompileJar(jarFiles, javaDirectory);
|
||||||
|
|
||||||
|
fileCleaner.deleteRecursively(unzippedApkDirectory);
|
||||||
|
fileCleaner.deleteRecursively(jarDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void decompileXmlFiles(File inputApk, File target) {
|
||||||
|
String[] command = {"apktool", "d", inputApk.getAbsolutePath(), "-o", target.getAbsolutePath()};
|
||||||
|
processExecutor.execute(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void convertDexToJar(Set<File> dexFiles, File target) {
|
||||||
|
dexFiles.forEach(dex -> {
|
||||||
|
String jarFilename = FilenameUtils.removeExtension(dex.getName()) + ".jar";
|
||||||
|
File jarFile = new File(target, jarFilename);
|
||||||
|
String[] command = {"dex2jar", dex.getAbsolutePath(), "-o", jarFile.getAbsolutePath()};
|
||||||
|
processExecutor.execute(command);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void decompileJar(Set<File> jarFiles, File target) {
|
||||||
|
jarFiles.forEach(jar -> {
|
||||||
|
String[] command = {"cfr", jar.getAbsolutePath(), "--outputdir", target.getAbsolutePath()};
|
||||||
|
processExecutor.execute(command);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
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.decompiler.process.ProcessExecutor;
|
||||||
|
import com.bartek.esa.file.cleaner.FileCleaner;
|
||||||
import com.bartek.esa.file.provider.FileProvider;
|
import com.bartek.esa.file.provider.FileProvider;
|
||||||
|
import com.bartek.esa.file.zip.ZipTool;
|
||||||
import dagger.Module;
|
import dagger.Module;
|
||||||
import dagger.Provides;
|
import dagger.Provides;
|
||||||
|
|
||||||
@@ -9,7 +12,12 @@ import dagger.Provides;
|
|||||||
public class DecompilerModule {
|
public class DecompilerModule {
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
public Decompiler decompiler(FileProvider fileProvider) {
|
public ProcessExecutor processExecutor() {
|
||||||
return new Decompiler(fileProvider);
|
return new ProcessExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
public Decompiler decompiler(FileProvider fileProvider, ProcessExecutor processExecutor, ZipTool zipTool, FileCleaner fileCleaner) {
|
||||||
|
return new Decompiler(fileProvider, processExecutor, zipTool, fileCleaner);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.bartek.esa.decompiler.process;
|
||||||
|
|
||||||
|
import com.bartek.esa.error.EsaException;
|
||||||
|
import io.vavr.control.Try;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
public class ProcessExecutor {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public ProcessExecutor() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void execute(String[] command) {
|
||||||
|
Process process = Try.of(() -> Runtime.getRuntime().exec(command))
|
||||||
|
.getOrElseThrow(EsaException::new);
|
||||||
|
waitForProcess(process);
|
||||||
|
checkExitValue(process, command[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void waitForProcess(Process process) {
|
||||||
|
Try.run(process::waitFor).getOrElseThrow(EsaException::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkExitValue(Process process, String commandName) {
|
||||||
|
if(process.exitValue() != 0) {
|
||||||
|
throw new EsaException("'" + commandName + "' process has finished with non-zero code. Interrupting...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
src/main/java/com/bartek/esa/file/cleaner/FileCleaner.java
Normal file
26
src/main/java/com/bartek/esa/file/cleaner/FileCleaner.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package com.bartek.esa.file.cleaner;
|
||||||
|
|
||||||
|
import com.bartek.esa.error.EsaException;
|
||||||
|
import io.vavr.control.Try;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
public class FileCleaner {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public FileCleaner() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteRecursively(File directory) {
|
||||||
|
Try.run(() -> Files.walk(directory.toPath())
|
||||||
|
.sorted(Comparator.reverseOrder())
|
||||||
|
.map(Path::toFile)
|
||||||
|
.forEach(File::delete))
|
||||||
|
.getOrElseThrow(EsaException::new);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package com.bartek.esa.file.di;
|
package com.bartek.esa.file.di;
|
||||||
|
|
||||||
|
import com.bartek.esa.file.cleaner.FileCleaner;
|
||||||
import com.bartek.esa.file.matcher.GlobMatcher;
|
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||||
import com.bartek.esa.file.provider.FileContentProvider;
|
import com.bartek.esa.file.provider.FileContentProvider;
|
||||||
import com.bartek.esa.file.provider.FileProvider;
|
import com.bartek.esa.file.provider.FileProvider;
|
||||||
|
import com.bartek.esa.file.zip.ZipTool;
|
||||||
import dagger.Module;
|
import dagger.Module;
|
||||||
import dagger.Provides;
|
import dagger.Provides;
|
||||||
|
|
||||||
@@ -23,4 +25,14 @@ public class FileModule {
|
|||||||
public GlobMatcher globMatcher() {
|
public GlobMatcher globMatcher() {
|
||||||
return new GlobMatcher();
|
return new GlobMatcher();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
public ZipTool zipTool() {
|
||||||
|
return new ZipTool();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
public FileCleaner fileCleaner() {
|
||||||
|
return new FileCleaner();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
59
src/main/java/com/bartek/esa/file/zip/ZipTool.java
Normal file
59
src/main/java/com/bartek/esa/file/zip/ZipTool.java
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package com.bartek.esa.file.zip;
|
||||||
|
|
||||||
|
import com.bartek.esa.error.EsaException;
|
||||||
|
import io.vavr.control.Try;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
|
public class ZipTool {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public ZipTool() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unzipArchive(File archive, File outputDir) {
|
||||||
|
Try.run(() -> tryToUnzipArchive(archive, outputDir))
|
||||||
|
.getOrElseThrow(EsaException::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void tryToUnzipArchive(File archive, File outputDir) throws IOException {
|
||||||
|
ZipFile zipFile = new ZipFile(archive);
|
||||||
|
|
||||||
|
zipFile.stream().forEach(entry -> unzipEntry(zipFile, entry, outputDir));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) {
|
||||||
|
if (entry.isDirectory()) {
|
||||||
|
createDir(new File(outputDir, entry.getName()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
File outputFile = new File(outputDir, entry.getName());
|
||||||
|
if (!outputFile.getParentFile().exists()){
|
||||||
|
createDir(outputFile.getParentFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
BufferedInputStream inputStream = Try.of(() -> new BufferedInputStream(zipfile.getInputStream(entry)))
|
||||||
|
.getOrElseThrow(EsaException::new);
|
||||||
|
BufferedOutputStream outputStream = Try.of(() -> new BufferedOutputStream(new FileOutputStream(outputFile)))
|
||||||
|
.getOrElseThrow(EsaException::new);
|
||||||
|
|
||||||
|
try {
|
||||||
|
IOUtils.copy(inputStream, outputStream);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new EsaException(e);
|
||||||
|
} finally {
|
||||||
|
Try.run(outputStream::close).getOrElseThrow(EsaException::new);
|
||||||
|
Try.run(inputStream::close).getOrElseThrow(EsaException::new);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createDir(File dir) {
|
||||||
|
if(!dir.mkdirs()) throw new EsaException("Cannot create directory " + dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user