-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace Android resource ids with
android.R
fields (#2119)
- Loading branch information
Showing
8 changed files
with
115 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
jadx-core/src/main/java/jadx/core/dex/visitors/prepare/AddAndroidConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package jadx.core.dex.visitors.prepare; | ||
|
||
import jadx.core.dex.info.ClassInfo; | ||
import jadx.core.dex.info.ConstStorage; | ||
import jadx.core.dex.info.FieldInfo; | ||
import jadx.core.dex.instructions.args.ArgType; | ||
import jadx.core.dex.nodes.RootNode; | ||
import jadx.core.dex.visitors.AbstractVisitor; | ||
import jadx.core.dex.visitors.JadxVisitor; | ||
import jadx.core.utils.android.AndroidResourcesMap; | ||
import jadx.core.utils.exceptions.JadxException; | ||
|
||
// TODO: move this pass to separate "Android plugin" | ||
@JadxVisitor( | ||
name = "AddAndroidConstants", | ||
desc = "Insert Android constants from resource mapping file", | ||
runBefore = { | ||
CollectConstValues.class | ||
} | ||
) | ||
public class AddAndroidConstants extends AbstractVisitor { | ||
|
||
private static final String R_CLS = "android.R"; | ||
private static final String R_INNER_CLS = R_CLS + '$'; | ||
|
||
@Override | ||
public void init(RootNode root) throws JadxException { | ||
if (!root.getArgs().isReplaceConsts()) { | ||
return; | ||
} | ||
if (root.resolveClass(R_CLS) != null) { | ||
// Android R class already loaded | ||
return; | ||
} | ||
ConstStorage constStorage = root.getConstValues(); | ||
AndroidResourcesMap.getMap().forEach((resId, path) -> { | ||
int sep = path.indexOf('/'); | ||
String clsName = R_INNER_CLS + path.substring(0, sep); | ||
String resName = path.substring(sep + 1); | ||
ClassInfo cls = ClassInfo.fromName(root, clsName); | ||
FieldInfo field = FieldInfo.from(root, cls, resName, ArgType.INT); | ||
constStorage.addGlobalConstField(field, resId); | ||
}); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
jadx-core/src/main/java/jadx/core/utils/android/AndroidResourcesMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package jadx.core.utils.android; | ||
|
||
import java.io.InputStream; | ||
import java.util.Map; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import jadx.core.utils.exceptions.JadxRuntimeException; | ||
|
||
/** | ||
* Store resources id to name mapping | ||
*/ | ||
public class AndroidResourcesMap { | ||
private static final Map<Integer, String> RES_MAP = loadBundled(); | ||
|
||
public static @Nullable String getResName(int resId) { | ||
return RES_MAP.get(resId); | ||
} | ||
|
||
public static Map<Integer, String> getMap() { | ||
return RES_MAP; | ||
} | ||
|
||
private static Map<Integer, String> loadBundled() { | ||
try (InputStream is = AndroidResourcesMap.class.getResourceAsStream("/android/res-map.txt")) { | ||
return TextResMapFile.read(is); | ||
} catch (Exception e) { | ||
throw new JadxRuntimeException("Failed to load android resource file (res-map.txt)", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
jadx-core/src/test/java/jadx/tests/integration/android/TestResConstReplace.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package jadx.tests.integration.android; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import jadx.tests.api.IntegrationTest; | ||
|
||
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat; | ||
|
||
public class TestResConstReplace extends IntegrationTest { | ||
|
||
public static class TestCls { | ||
public int test() { | ||
return 0x0101013f; // android.R.attr.minWidth | ||
} | ||
} | ||
|
||
@Test | ||
public void test() { | ||
disableCompilation(); | ||
assertThat(getClassNode(TestCls.class)) | ||
.code() | ||
.containsOne("import android.R;") | ||
.containsOne("return R.attr.minWidth;"); | ||
} | ||
} |