From addbf8336da7d3e1e1285ceffb416cf64b302060 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Thu, 16 Apr 2015 07:24:27 -0500 Subject: [PATCH] [WIP] Wires up rewriter of @string references in provider attrs - finds all in manifest - finds corresponding @string in res/values/strings.xml - does reference replacement w/ literal value - fixes #636 --- .../src/main/java/brut/androlib/Androlib.java | 2 + .../brut/androlib/res/AndrolibResources.java | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java index 8b4a9b4b..91111665 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java @@ -280,7 +280,9 @@ public class Androlib { new File(appDir, APK_DIRNAME).mkdirs(); buildSources(appDir); buildNonDefaultSources(appDir); + mAndRes.fixing_public_attrs_in_providers(new File(appDir, "AndroidManifest.xml")); buildResources(appDir, (Map) meta.get("usesFramework")); + buildLib(appDir); buildCopyOriginalFiles(appDir); buildApk(appDir, outFile); diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/AndrolibResources.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/AndrolibResources.java index 0bfa0efe..21eea782 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/AndrolibResources.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/AndrolibResources.java @@ -41,12 +41,14 @@ import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import javax.xml.xpath.*; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.apache.commons.io.IOUtils; +import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import org.xmlpull.v1.XmlSerializer; @@ -227,6 +229,64 @@ final public class AndrolibResources { } } + public void fixing_public_attrs_in_providers(File file) throws AndrolibException { + if (file.exists()) { + try { + Document doc = loadDocument(file.getAbsolutePath()); + XPath xPath = XPathFactory.newInstance().newXPath(); + XPathExpression expression = xPath.compile("/manifest/application/provider"); + + Object result = expression.evaluate(doc, XPathConstants.NODESET); + NodeList nodes = (NodeList) result; + + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + NamedNodeMap attrs = node.getAttributes(); + + if (attrs != null) { + Node provider = attrs.getNamedItem("android:authorities"); + + if (provider != null) { + String reference = provider.getNodeValue(); + String replacement = pull_value_from_strings(file.getParentFile(), reference); + + if (replacement != null) { + provider.setNodeValue(replacement); + saveDocument(file.getAbsolutePath(), doc); + } + } + } + } + + } catch (SAXException | ParserConfigurationException | IOException | + XPathExpressionException | TransformerException ignored) { + } + } + } + + public String pull_value_from_strings(File directory, String key) throws AndrolibException { + File file = new File(directory, "/res/values/strings.xml"); + key = key.replace("@string/", ""); + + if (file.exists()) { + try { + Document doc = loadDocument(file.getAbsolutePath()); + XPath xPath = XPathFactory.newInstance().newXPath(); + XPathExpression expression = xPath.compile("/resources/string[@name=" + '"' + key + "\"]/text()"); + + Object result = expression.evaluate(doc, XPathConstants.STRING); + + if (result != null) { + return (String) result; + } + + } catch (SAXException | ParserConfigurationException | IOException | XPathExpressionException ignored) { + } + } + + return null; + } + public void remove_manifest_versions(String filePath) throws AndrolibException {