Skip to content

Commit

Permalink
fix: improve resource type detection and remove deprecated method
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Nov 4, 2020
1 parent 71bf2aa commit cd006ce
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
31 changes: 27 additions & 4 deletions jadx-core/src/main/java/jadx/api/ResourceType.java
@@ -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;
Expand All @@ -21,12 +27,29 @@ public String[] getExts() {
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;
Expand Down
5 changes: 0 additions & 5 deletions jadx-gui/src/main/java/jadx/gui/JadxWrapper.java
Expand Up @@ -143,11 +143,6 @@ public List<ResourceFile> getResources() {
return decompiler.getResources();
}

@Deprecated
public File getOpenFile() {
return openPaths.get(0).toFile();
}

public List<Path> getOpenPaths() {
return openPaths;
}
Expand Down
17 changes: 14 additions & 3 deletions jadx-gui/src/main/java/jadx/gui/treemodel/ApkSignature.java
Expand Up @@ -15,6 +15,8 @@

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;
Expand All @@ -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) {
Expand Down

0 comments on commit cd006ce

Please sign in to comment.