如何外部修改AndroidManifest.xml中android:scheme的值

公司有个小需求,需要动态配置AndroidManifest.xml中android:scheme的值。
这就转化成通过一个外部java工程修改xml节点值的问题。

其中AndroidManifest.xml需要约定一个规则:

<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
                <data android:host="m.test.com" android:scheme="openSchema_replace"/>
            intent-filter>
activity>

android:scheme=”openSchema_replace”需要按这种规范写。
接下来需要建一个java工程,新建一个类ConsoleTest.java,使用DOM的方式解析xml,代码中AndroidManifest.xml文件路径修改成自己的就好,运行一下,就可以看到android:scheme的值修改了。

package changexml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class ConsoleTest {

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 

    public static void main(String[] args){
        System.out.println("hello  World!");
        new ConsoleTest().parse("G:/practicedemo/ToDoList/app/src/main/AndroidManifest.xml");
    }

    /**
     * read xml
     */
    private Document parse(String filePath) { 
        Document document = null; 
    try { 
        //DOM parser instance 
        DocumentBuilder builder = builderFactory.newDocumentBuilder(); 

        document = builder.parse(new File(filePath)); 
        Element root = document.getDocumentElement();
        NodeList nodeList = root.getElementsByTagName("data");
        for (int i = 0; i < nodeList.getLength(); i++) {
            NamedNodeMap namedNodeMap = nodeList.item(i).getAttributes();
            for (int j = 0; j < namedNodeMap.getLength(); j++) {
                Node node = namedNodeMap.item(j);
                String nodeName = node.getNodeName();
                System.out.println("nodeName:"+nodeName);
                String nodeValue = node.getNodeValue();
                System.out.println("nodeValue:"+nodeValue);

                if("android:scheme".equals(nodeName)&& "openSchema_replace".equals(nodeValue)){
                    node.setNodeValue("jd12345678utytgbvrrdcsszxzaqsdcgghj");
                }
                String newNodeValue = node.getNodeValue();
                System.out.println("newNodeValue:"+newNodeValue);
            }
        }
        saveRoot(root, "G:/practicedemo/ToDoList/app/src/main/AndroidManifest.xml");

    } catch (ParserConfigurationException e) { 
        e.printStackTrace();
        } catch (SAXException e) { 
            e.printStackTrace(); 
    } catch (IOException e) { 
        e.printStackTrace();
        }
        return document;
    } 

    private void saveRoot(Node node ,String filePath){
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        try {
            Transformer transformer = transformerFactory.newTransformer();
             // 设置各种输出属性  
            transformer.setOutputProperty("encoding", "gb2312");  
            transformer.setOutputProperty("indent", "yes");  
            DOMSource source = new DOMSource();  
            // 将待转换输出节点赋值给DOM源模型的持有者(holder)  
            source.setNode(node); 

            StreamResult result = new StreamResult();  
            if (filePath == null) {  
              // 设置标准输出流为transformer的底层输出目标  
              result.setOutputStream(System.out);  
            } else {  
              result.setOutputStream(new FileOutputStream(filePath));  
            }  
            // 执行转换从源模型到控制台输出流  
            transformer.transform(source, result);  
          } catch (TransformerConfigurationException e) {  
            e.printStackTrace();  
          } catch (TransformerException e) {  
            e.printStackTrace();  
          } catch (FileNotFoundException e) {  
            e.printStackTrace();  
          } 
    }

}

你可能感兴趣的:(android基础)