fix: improve resource type detection and remove deprecated method

This commit is contained in:
Skylot
2020-11-04 21:02:13 +00:00
parent 71bf2aa59f
commit cd006ce78e
3 changed files with 41 additions and 12 deletions
@@ -1,14 +1,20 @@
package jadx.api;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import jadx.core.utils.exceptions.JadxRuntimeException;
public enum ResourceType {
CODE(".dex", ".jar", ".class"),
MANIFEST("AndroidManifest.xml"),
XML(".xml"),
ARSC(".arsc"),
FONT(".ttf", ".otf"),
IMG(".png", ".gif", ".jpg"),
MEDIA(".mp3", ".wav"),
LIB(".so"),
MANIFEST,
UNKNOWN;
private final String[] exts;
@@ -21,14 +27,31 @@ public enum ResourceType {
return exts;
}
public static ResourceType getFileType(String fileName) {
private static final Map<String, ResourceType> EXT_MAP = new HashMap<>();
static {
for (ResourceType type : ResourceType.values()) {
for (String ext : type.getExts()) {
if (fileName.toLowerCase().endsWith(ext)) {
return type;
ResourceType prev = EXT_MAP.put(ext, type);
if (prev != null) {
throw new JadxRuntimeException("Duplicate extension in ResourceType: " + ext);
}
}
}
}
public static ResourceType getFileType(String fileName) {
int dot = fileName.lastIndexOf('.');
if (dot != -1) {
String ext = fileName.substring(dot).toLowerCase(Locale.ROOT);
ResourceType resType = EXT_MAP.get(ext);
if (resType != null) {
if (resType == XML && fileName.equals("AndroidManifest.xml")) {
return MANIFEST;
}
return resType;
}
}
return UNKNOWN;
}
}
@@ -143,11 +143,6 @@ public class JadxWrapper {
return decompiler.getResources();
}
@Deprecated
public File getOpenFile() {
return openPaths.get(0).toFile();
}
public List<Path> getOpenPaths() {
return openPaths;
}
@@ -15,6 +15,8 @@ import org.slf4j.LoggerFactory;
import com.android.apksig.ApkVerifier;
import jadx.api.ResourceFile;
import jadx.api.ResourceType;
import jadx.gui.JadxWrapper;
import jadx.gui.utils.CertificateManager;
import jadx.gui.utils.NLS;
@@ -33,11 +35,20 @@ public class ApkSignature extends JNode {
public static ApkSignature getApkSignature(JadxWrapper wrapper) {
// Only show the ApkSignature node if an AndroidManifest.xml is present.
// Without a manifest the Google ApkVerifier refuses to work.
if (wrapper.getResources().stream().noneMatch(r -> "AndroidManifest.xml".equals(r.getOriginalName()))) {
File apkFile = null;
for (ResourceFile resFile : wrapper.getResources()) {
if (resFile.getType() == ResourceType.MANIFEST) {
ResourceFile.ZipRef zipRef = resFile.getZipRef();
if (zipRef != null) {
apkFile = zipRef.getZipFile();
break;
}
}
}
if (apkFile == null) {
return null;
}
File openFile = wrapper.getOpenFile();
return new ApkSignature(openFile);
return new ApkSignature(apkFile);
}
public ApkSignature(File openFile) {