AndroidManifest属性修改

来源:互联网 发布:湖北地税软件下载 编辑:程序博客网 时间:2024/05/22 06:13

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);    }

主要修改三种类型:

  1. AndroidManifest:meta-data属性
 代码结构:<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.testparse"    android:versionCode="1"    android:versionName="1.0" >    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme"          <meta-data            android:name="BAIDUSDK_CHANNEL"            android:value="12345666" />            </application></manifest>

执行方法:

public static void main(String[] args) {        configappIdInMainfest("12345666","//manifest//application//meta-data/@android:value");    }

多个< meta-data>属性时,指定修改某个属性的值:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.testparse"    android:versionCode="1"    android:versionName="1.0" >    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme"          <meta-data            android:name="BAIDUSDK_CHANNEL"            android:value="12345666" />     <meta-data            android:name="BAIDUSDK_APPID"            android:value="12345666" />            </application></manifest>

执行方法:

public static void main(String[] args) {change("F:\\testDemo\\AndroidManifest.xml","//manifest//application//meta-data[@android:name='"+ "需要修改meta-data的name" + "']/@android:value", "23244");    }

2.AndroidManifest:某个Activity下某个< intent-filter>标签下某个元素的值,例如修改data的scheme 属性。

代码结构:<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.testparse"    android:versionCode="1"    android:versionName="1.0" >    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme">        <activity            android:name="com.wandoujia.oakenshield.activity.OakenshieldActivity"            android:configChanges="keyboardHidden|orientation|screenSize"            android:theme="@android:style/Theme.Translucent.NoTitleBar"            android:windowSoftInputMode="adjustResize" >            <intent-filter>                <action android:name="com.wandoujia.oakenshield" />                <category android:name="android.intent.category.DEFAULT" />                <data android:scheme="Wandoujia-PaySdk-12345666" />            </intent-filter>        </activity></manifest>

执行方法:

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属性的值。

代码结构:<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.testparse"    android:versionCode="1"    android:versionName="1.0" >    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@Theme">        <receiver android:name="com.wandoujia.mariosdk.plugin.api.WandouPluginReceiver">            <intent-filter>                <action android:name="Wandoujia-PaySdk-12345666"/>                <action android:name="Wandoujia-PaySdk-12345666"/>                <action android:name="Wandoujia-PaySdk-12345666"/>                <action android:name="pheonix.intent.action.LOGOUT_SUCCESS"/>            </intent-filter>          </receiver></manifest>

调用方法:

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");    }
原创粉丝点击