在做安卓开发,都会遇到一个打多渠道包,包名的问题,想通过一套代码,只改包名来实现多个渠道。

那问题来了,1;mainfest里的package是容易改,但其他如activity,service中也有应用到包名的地方呢?如何改?

    2:除lmainfest外,src下所有方法,应用了包名 的类又如何改?

对于第一个问题,比较容易解决,通过我上一篇,递归获取mainfest节点的方法,我们可以找到节点下的element,获取他们的attribute,得到他们的内容,如果内容包括我们包名,我们就对次节点进行修改。否则不管。

对于第二个问题。这就比较棘手,src下很有可能有多个文件,这用Ios去,读写,量比较大,速度肯定不快。想了一个方法,将当前读取到的文件记录下其包括包名的位置,然后再写文件的时候对此位置进行替换操作。可试验了一上午,并非我想象的那样。没办法

只能用传统的读写操作。

 

下面是修改mainfest中跟包名相关代码

public static String modifyMainfestPackeName(String mainfestPath, String strOldPageName, String strNewPageName){
        
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
        factory.setIgnoringElementContentWhitespace(true);  
        DocumentBuilder db; 
        try {  
            db = factory.newDocumentBuilder();  
            Document doc = db.parse(mainfestPath);  
            NodeList nl = doc.getChildNodes();  
        
            // 只有一个node,即 manifest  
            // item(0)即为 manifest。  
            Node node = nl.item(0);  
         
            // 获得manifest的各个属性  
            NamedNodeMap map = node.getAttributes();  

            // 重新设置package的值  
            map.getNamedItem("package").setNodeValue(strNewPageName);  
            
            NodeList rootList = node.getChildNodes();
            List<Element> temp = new ArrayList<Element>(); 
            for(int i = 0; i < rootList.getLength(); i++){
                String strNodeName = rootList.item(i).getNodeName();
                System.out.println("最上面的节点 ,此时名称为:" + strNodeName);
                if(!strNodeName.equals("#text") && !strNodeName.equals("#comment")){
                    Element childElment = changeAtuttiesPackName((Element)rootList.item(i), doc, strOldPageName, strNewPageName);
                    temp.add(childElment);
                    /*node.appendChild(childElment);*/
                    //node.insertBefore(childElment, rootList.item(rootList.getLength()- 1));
                }
            }
            for(Element element : temp){
                node.insertBefore(element, rootList.item(rootList.getLength()- 1));
            }
        
           //
            return writeXML(doc, mainfestPath);
          } catch (Exception e) {  
               e.printStackTrace();  
               return "失败";
          }  
    }

递归修改

private static Element changeAtuttiesPackName(Element element, Document doc,String strOldPackName, String strNewPackName){
        Element currentElement  = doc.createElement(element.getNodeName());
        NamedNodeMap attributesNNM = element.getAttributes();
        for(int i = 0; i < attributesNNM.getLength(); i++){
            String strContext = attributesNNM.item(i).getTextContent();
            //Element tempElement = doc.createElement(attributesNNM.item(i).getNodeName());
            if(strContext.contains(strOldPackName)){                
                strContext = strContext.replace(strOldPackName, strNewPackName);
                System.out.println("此时的节点名为:" + attributesNNM.item(i).getNodeName() + " ,修改后的包括包名内容为:"+ strContext);
            
                currentElement.setAttribute(attributesNNM.item(i).getNodeName(), strContext);
            }else{
                System.out.println("此时属性不包括包名为:" + attributesNNM.item(i).getNodeName() + " ,strContext:"+ strContext);
                
                currentElement.setAttribute(attributesNNM.item(i).getNodeName(), strContext);
            }
        }

        //如果存在子节点
        Node sourceNode = (Node)element;
        NodeList sourceChildNodeList =sourceNode.getChildNodes();
        for(int i =0; i < sourceChildNodeList.getLength(); i ++){
            System.out.println("子节点名称为:===》" + sourceChildNodeList.item(i).getNodeName() + " , 节点值为:" + sourceChildNodeList.item(i).getNodeValue() + " , 子节点内容为:" +
                    sourceChildNodeList.item(i).getTextContent());
            String strChildName = sourceChildNodeList.item(i).getNodeName();
            if(!strChildName.equals("#text") && !strChildName.equals("#comment") ){
                //说明存在子节点
                System.out.println("说明存在子节点,此时strChildName=〉" + strChildName);
                Element childElemnt = changeAtuttiesPackName((Element)sourceChildNodeList.item(i), doc, strOldPackName, strNewPackName);

                ((Node)currentElement).appendChild(childElemnt);
            }else if (strChildName.equals("#comment")){
                //不存在子节点
                Comment com = doc.createComment(sourceChildNodeList.item(i).getTextContent());

                ((Node)currentElement).appendChild(com);
                System.out.println("说明不存在子节点,此时strChildName=〉" + strChildName);
            }
        }

        return currentElement;
    }

接下来修改src下的

public static String modifyPackName(String strOldPageName, String strNewPageName){
        File rootFile = new File(".\\src"); 
        File[] fileList = rootFile.listFiles();
        
        for(File file : fileList){
            if(file.isDirectory()){
                ReadFile(file.getAbsolutePath());
            }else{
                list.add(file);
            }
        }
        
        
        for(File file : list){
            System.out.println("当前文件名==" +  file.getName());
            if (!file.getName().equals("test.jave"))
                continue;
            
            System.out.println("当前路径为:" + file.getAbsolutePath());
            System.out.println("当前路径为path=》 :" + file.getPath());
            String strFileContext = FileUtils.read(file.getAbsolutePath());
            System.out.println("替换前的内容:" + strFileContext);
            strFileContext = strFileContext.replace(strOldPageName, strNewPageName);
            System.out.println("替换后的内容为:" + strFileContext);
            int iResult = FileUtils.write(file.getAbsolutePath(), strFileContext, false);
            System.out.println("执行完写++++");
            if(iResult ==  0){
                System.out.println("当前成功写入了新的字符");
            }else if(iResult == 104){
                System.out.println("文件读取出错了");
            }else{
                System.out.println("文件没有找到");
            }
        }
        return "成功";
    }

其中read,write方法,主要是读取文件的所有内容,转为字符串,然后通过替换字符串中的包名,在将新的字符串写入到文件里,而实现,替换包名。

 

posted on 2015-08-07 09:42  zihaobiao  阅读(1646)  评论(0编辑  收藏  举报