refactor: cleanup some things and catch possible errors

This commit is contained in:
Lucaskyy 2022-06-11 19:17:09 +02:00
parent 4dca2b2d27
commit cdc3eefd88
No known key found for this signature in database
GPG Key ID: 1530BFF96D1EEB89
2 changed files with 18 additions and 18 deletions

View File

@ -37,6 +37,7 @@ import org.apache.commons.io.IOUtils;
import org.xmlpull.v1.XmlSerializer;
import java.io.*;
import java.nio.file.Files;
import java.util.*;
import java.util.logging.Logger;
import java.util.zip.CRC32;
@ -617,7 +618,7 @@ final public class AndrolibResources {
List<String> cmd = new ArrayList<>();
try {
String aaptCommand = AaptManager.getAaptExecutionCommand(aaptPath, getAaptBinaryFile());
String aaptCommand = AaptManager.getAaptExecutionCommand(aaptPath, getAaptVersion());
cmd.add(aaptCommand);
} catch (BrutException ex) {
LOGGER.warning("aapt: " + ex.getMessage() + " (defaulting to $PATH binary)");
@ -810,7 +811,7 @@ final public class AndrolibResources {
if (id == 1) {
try (InputStream in = getAndroidFrameworkResourcesAsStream();
OutputStream out = new FileOutputStream(apk)) {
OutputStream out = Files.newOutputStream(apk.toPath())) {
IOUtils.copy(in, out);
return apk;
} catch (IOException ex) {
@ -1011,17 +1012,6 @@ final public class AndrolibResources {
return dir;
}
private File getAaptBinaryFile() throws AndrolibException {
try {
if (getAaptVersion() == 2) {
return AaptManager.getAapt2();
}
return AaptManager.getAapt1();
} catch (BrutException ex) {
throw new AndrolibException(ex);
}
}
private int getAaptVersion() {
return buildOptions.isAapt2() ? 2 : 1;
}

View File

@ -23,7 +23,6 @@ import java.util.ArrayList;
import java.util.List;
public class AaptManager {
private static File getAapt(Integer version) throws BrutException {
File aaptBinary;
String aaptVersion = getAaptBinaryName(version);
@ -56,17 +55,20 @@ public class AaptManager {
throw new BrutException("Can't set aapt binary as executable");
}
public static String getAaptExecutionCommand(String aaptPath, File aapt) throws BrutException {
public static String getAaptExecutionCommand(String aaptPath, int aaptVersion) throws BrutException {
if (!aaptPath.isEmpty()) {
File aaptFile = new File(aaptPath);
if (aaptFile.canRead() && aaptFile.exists()) {
aaptFile.setExecutable(true);
executable(aaptFile);
return aaptFile.getPath();
} else {
throw new BrutException("binary could not be read: " + aaptFile.getAbsolutePath());
}
} else {
return aapt.getAbsolutePath();
if (aaptVersion == 2) {
return getAapt2().getAbsolutePath();
}
return getAapt1().getAbsolutePath();
}
}
@ -94,7 +96,7 @@ public class AaptManager {
if (!aapt.isFile()) {
throw new BrutException("Could not identify aapt binary as executable.");
}
aapt.setExecutable(true);
executable(aapt);
List<String> cmd = new ArrayList<>();
cmd.add(aapt.getAbsolutePath());
@ -109,6 +111,14 @@ public class AaptManager {
return getAppVersionFromString(version);
}
private static void executable(File aapt) {
if (!aapt.canExecute()) {
try {
aapt.setExecutable(true);
} catch (Exception ignored) {}
}
}
public static File getAapt2() throws BrutException {
return getAapt(2);
}