From 49e55f522f05279885801194dc74db3d72af79a3 Mon Sep 17 00:00:00 2001 From: Igor Eisberg Date: Fri, 15 Feb 2019 15:08:03 +0200 Subject: [PATCH] Fix APKs with SDK versions being reference values This non-standard behavior is rare, but quite annoying. The solution is simple - replacing the reference value with the actual value from integers.xml, just like Apktool already does for versionName. --- .../brut/androlib/res/xml/ResXmlPatcher.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/xml/ResXmlPatcher.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/xml/ResXmlPatcher.java index c1581340..8fd124c4 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/xml/ResXmlPatcher.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/xml/ResXmlPatcher.java @@ -197,6 +197,41 @@ public final class ResXmlPatcher { return null; } + /** + * Finds key in integers.xml file and returns text value + * + * @param directory Root directory of apk + * @param key Integer reference (ie @integer/foo) + * @return String|null + * @throws AndrolibException + */ + public static String pullValueFromIntegers(File directory, String key) throws AndrolibException { + if (key == null || ! key.contains("@")) { + return null; + } + + File file = new File(directory, "/res/values/integers.xml"); + key = key.replace("@integer/", ""); + + if (file.exists()) { + try { + Document doc = loadDocument(file); + XPath xPath = XPathFactory.newInstance().newXPath(); + XPathExpression expression = xPath.compile("/resources/integer[@name=" + '"' + key + "\"]/text()"); + + Object result = expression.evaluate(doc, XPathConstants.STRING); + + if (result != null) { + return (String) result; + } + + } catch (SAXException | ParserConfigurationException | IOException | XPathExpressionException ignored) { + } + } + + return null; + } + /** * Removes attributes like "versionCode" and "versionName" from file. *