How to modify the value of android:scheme in Android manifest.xml externally

Posted by ridster on Sat, 02 May 2020 07:18:10 +0200

The company has a small need to dynamically configure the value of android:scheme in Android manifest.xml.
This translates into the problem of modifying xml node values through an external java project.

Among them, Android manifest.xml needs to stipulate a rule:

<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" needs to be written according to this specification.
Next, you need to build a java project, create a new class ConsoleTest.java, use DOM to parse XML, modify the path of Android manifest.xml file in the code to your own, and run it to see the value modification of 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();
             // Set various output properties  
            transformer.setOutputProperty("encoding", "gb2312");  
            transformer.setOutputProperty("indent", "yes");  
            DOMSource source = new DOMSource();  
            // Assign the output node to be converted to the holder of the DOM source model  
            source.setNode(node); 

            StreamResult result = new StreamResult();  
            if (filePath == null) {  
              // Set the standard output stream as the underlying output target of transformer  
              result.setOutputStream(System.out);  
            } else {  
              result.setOutputStream(new FileOutputStream(filePath));  
            }  
            // Perform transformation from source model to console output stream  
            transformer.transform(source, result);  
          } catch (TransformerConfigurationException e) {  
            e.printStackTrace();  
          } catch (TransformerException e) {  
            e.printStackTrace();  
          } catch (FileNotFoundException e) {  
            e.printStackTrace();  
          } 
    }

}

Topics: xml Android Java encoding