AndroidManifest屬性修改

AndroidManifest屬性修改,AndroidManifest屬於xml的一種形式,所以解析XML無非就三種形式:DOM、SAX、PULL。
本文采用SAX解析數據;xmlWriter修改數據。

public static boolean change(String fileName, String xPath, String value) {
        InputStream in = null;
        XMLWriter xmlWriter = null;
        try {
            File file = new File(fileName);
            in = new FileInputStream(file);
            SAXReader saxReader = new SAXReader();
            Document doc = saxReader.read(in);
            // Attribute attr = (Attribute)
            // doc.selectSingleNode("/manifest/@package"); 取mainfest下package屬性
            Attribute attr = (Attribute) doc.selectSingleNode(xPath);
            attr.setValue(value);

            // 寫文件
            OutputStream out = new FileOutputStream(fileName);
            Writer writer = new OutputStreamWriter(out, "UTF-8");
            xmlWriter = new XMLWriter(writer);
            xmlWriter.write(doc);
            xmlWriter.close();
            return true;
        } catch (Throwable e) {
            FileUtils.print(e);
        } finally {
            if (xmlWriter != null)
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                    FileUtils.print(e);
                }
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                    FileUtils.print(e);
                }
        }
        return false;
    }

public static void configappIdInMainfest(String appId, String xPath) {
    String value = "測試" + appId;            
    ConfigParser.change("F:\\TestDemo\\AndroidManifest.xml",xPath, value);
    }

主要修改三種類型:

AndroidManifest:meta-data屬性

 代碼結構:

    %3Cmeta-data%0A%20%20%20%20%20%20%20%20%20%20%20%20android%3Aname%3D%22BAIDUSDK_CHANNEL%22%0A%20%20%20%20%20%20%20%20%20%20%20%20android%3Avalue%3D%2212345666%22%20%2F%3E
            


執行方法:

public static void main(String[] args) {

        configappIdInMainfest("12345666","//manifest//application//meta-data/@android:value");
    }

2.AndroidManifest:某個Activity下某個< intent-filter>標簽下某個元素的值,例如修改data的scheme 屬性。

代碼結構:

    
        
            
                
                
                
            
        

執行方法:

public static void main(String[] args) {

configappIdInMainfest("12345666","//manifest//application//activity[@android:name='com.wandoujia.oakenshield.activity.OakenshieldActivity']//intent-filter/data/@android:scheme");
    }

3、AndroidManifest:某個Activity下某個< intent-filter>標簽下相同屬性集合下的某元素的值,例如修改action的第三個name屬性的值。

代碼結構:

    
        
            
                
                
                
                
            
          

調用方法:

public static void main(String[] args) {
configappIdInMainfest("12345666",//manifest//application//receiver[@android:name='com.wandoujia.mariosdk.plugin.api.WandouPluginReceiver']//intent-filter/action[3]/@android:name");
    }

You May Also Like