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.
This commit is contained in:
Igor Eisberg 2019-02-15 15:08:03 +02:00 committed by Connor Tumbleson
parent be1aea76fe
commit 49e55f522f
No known key found for this signature in database
GPG Key ID: C3CC0A201EC7DA75

View File

@ -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.
*