fix: format Android resources ids as hex (#1171)

This commit is contained in:
Skylot
2021-05-20 18:29:20 +01:00
parent 8785c33d06
commit 07d7e68dc2
3 changed files with 22 additions and 2 deletions
@@ -43,6 +43,7 @@ import jadx.core.dex.nodes.RootNode;
import jadx.core.utils.CodeGenUtils;
import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.Utils;
import jadx.core.utils.android.AndroidResourcesUtils;
import jadx.core.utils.exceptions.CodegenException;
import jadx.core.utils.exceptions.JadxRuntimeException;
@@ -402,7 +403,9 @@ public class ClassGen {
if (encodedValue.getType() == EncodedType.ENCODED_NULL) {
code.add(TypeGen.literalToString(0, f.getType(), cls, fallback));
} else {
annotationGen.encodeValue(cls.root(), code, encodedValue);
if (!AndroidResourcesUtils.handleResourceFieldValue(cls, code, encodedValue)) {
annotationGen.encodeValue(cls.root(), code, encodedValue);
}
}
} else if (fv.isInsn()) {
InsnGen insnGen = makeInsnGen(fv.getInsnMth());
@@ -74,6 +74,23 @@ public class AndroidResourcesUtils {
return false;
}
/**
* Force hex format for Android resources ids
*/
@SuppressWarnings("RedundantCast")
public static boolean handleResourceFieldValue(ClassNode cls, ICodeWriter code, EncodedValue encodedValue) {
if (encodedValue.getType() == EncodedType.ENCODED_INT && isResourceClass(cls)) {
code.add(String.format("0x%X", ((Integer) encodedValue.getValue())));
return true;
}
return false;
}
public static boolean isResourceClass(ClassNode cls) {
ClassNode parentClass = cls.getParentClass();
return parentClass != null && parentClass.getShortName().equals("R");
}
private static ClassNode makeClass(RootNode root, String clsName, ResourceStorage resStorage) {
ClassNode rCls = ClassNode.addSyntheticClass(root, clsName, AccessFlags.PUBLIC | AccessFlags.FINAL);
rCls.addAttr(AType.COMMENTS, "This class is generated by JADX");
@@ -35,7 +35,7 @@ public class XmlGenUtils {
Set<String> addedValues = new HashSet<>();
for (ResourceEntry ri : resStorage.getResources()) {
if (addedValues.add(ri.getTypeName() + '.' + ri.getKeyName())) {
String format = String.format("<public type=\"%s\" name=\"%s\" id=\"%s\" />",
String format = String.format("<public type=\"%s\" name=\"%s\" id=\"0x%X\" />",
ri.getTypeName(), ri.getKeyName(), ri.getId());
writer.startLine(format);
}