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

主要修改三种类型:

  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");
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值