Android反編譯助手

來源:互聯網
上載者:User
package cn.android.find.main;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.math.BigInteger;import java.util.HashMap;import java.util.Map;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;public class FindReplace {public Map<String, String> resources = new HashMap<String, String>(); // key:id value:R.id.bgpublic static File xml = new File("D:/qq/public.xml");//XML路徑public static File file = new File("D:/qq/mobileqq.src");//待替換的源碼檔案夾public static void main(String[] args) {long start = System.currentTimeMillis();FindReplace replace = new FindReplace();replace.findReplace(replace.findReplaceXml(xml), file);System.out.println("用時:" + (System.currentTimeMillis() - start));}/** * 產生十六進位轉成十進位的資源檔 * @param file * @return */public File findReplaceXml(File file) {FileReader fr = null;FileWriter fw = null;File fileTemp = new File(file.getParent(), "re"+file.getName());try {fr = new FileReader(file);fw = new FileWriter(fileTemp);BufferedReader bf = new BufferedReader(fr);String str = null;// 取一行while ((str = bf.readLine()) != null) { // 一行一行的讀取全部內容// 顯示行號int index = str.lastIndexOf("id=\"0x");if (index != -1) {index = index + 6;String s = str.substring(index, index + 8);str = str.replace("0x" + s, hexToDecimal(s));}fw.write(str);fw.write("\r\n");// 寫入換行}fw.close();} catch (Exception e) {e.printStackTrace();}return fileTemp;}/** * 十六進位轉成十進位 * @param hexString * @return */private static String hexToDecimal(String hexString) {BigInteger bi = null;bi = new BigInteger(hexString, 16);String show = bi.toString(10);return show;}/** * 尋找替換 * @param xml * @param fileDir */public void findReplace(File xml, File fileDir) {InputStream inStream = null;try {initMap(new FileInputStream(xml));findFileList(fileDir);} catch (FileNotFoundException e) {e.printStackTrace();} finally {if (inStream != null)try {inStream.close();} catch (IOException e) {e.printStackTrace();}}}/** * Regex尋找替換 */private String matcher(String regex) {Matcher m = Pattern.compile("[0-9]\\d{9}").matcher(regex);while (m.find()) {String recource = resources.get(m.group());if(recource!=null){regex = regex.replaceAll(m.group(), recource);}}return regex;}/** * 遞迴遍曆目錄檔案 *  * @param file * @param resourceContent * @param replaceContent */public void findFileList(File file) {File[] files = file.listFiles();if (files == null) {return;}for (File f : files) {if (f.isDirectory()) {findFileList(f);} else {if (f.getName().endsWith(".java")) {transferFile(f); // 尋找替換}}}}/** * 儲存到原檔案 * @param file */private void transferFile(File file) {try {FileReader reader = new FileReader(file);char[] dates = new char[1024];int count = 0;StringBuilder sb = new StringBuilder();while ((count = reader.read(dates)) > 0) {String str = String.valueOf(dates, 0, count);sb.append(str);}reader.close();// 從構造器中產生字串,並替換搜尋文本String fileContent = matcher(sb.toString());FileWriter writer = new FileWriter(file);writer.write(fileContent.toCharArray());writer.close();} catch (Exception e) {e.printStackTrace();}}/** * 從流中初始化資源集合 *  // R.attr.name 2130771968 * @param inStream */public void initMap(InputStream inStream) {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();try {DocumentBuilder builder = factory.newDocumentBuilder();Document dom = builder.parse(inStream);Element root = dom.getDocumentElement();NodeList items = root.getElementsByTagName("public");// 尋找所有person節點System.out.println("XML檔案中有記錄:" + items.getLength());for (int i = 0; i < items.getLength(); i++) {// 得到public節點Element publicNode = (Element) items.item(i);resources.put(publicNode.getAttribute("id"), "R." + publicNode.getAttribute("type") + "." + publicNode.getAttribute("name"));}System.out.println("添加記錄:"+resources.size());inStream.close();} catch (Exception e) {e.printStackTrace();}}}

轉自:http://my.oschina.net/onlytwo/blog/52758

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.