这就是我的XML的样子。
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="xxxxxxxx" android:versionName="xxxxx" package="xxxxxx" xmlns:android="xxxxxxxxxxxxxx">
<application android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true">
</application>
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
</manifest>
我需要在<应用
android:networkSecurityConfig="@xml/network_security_config" android:requestLegacyExternalStorage="true"
因此,输出应该看起来像
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="xxxxxxxx" android:versionName="xxxxx" package="xxxxxx" xmlns:android="xxxxxxxxxxxxxx">
<application android:networkSecurityConfig="@xml/network_security_config" android:requestLegacyExternalStorage="true" android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true">
</application>
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
</manifest>
我试过sed命令,但仍然没有成功。每个人都建议使用XMLSTART。
sed -i '/<application/a android:networkSecurityConfig="@xml/network_security_config" android:requestLegacyExternalStorage="true"' AndroidManifest.xml
如果xmlstarlet
不可用或不是首选,则可以使用xmllint
通过完成向元素添加属性
# get uses-feature element
uf="$(xmllint --xpath '/manifest/uses-feature' tmp.xml)"
# get application attributes
attrs="$(xmllint --xpath '/manifest/application/@*' tmp.xml | tr 'n' ' ')"
# append desired attributes
attrs+=' android:networkSecurityConfig="@xml/network_security_config" android:requestLegacyExternalStorage="true"'
# build manifest inner xml
inner="<application $attrs></application>$uf"
# restore manifest inner xml (with @CharlesDuffy kind suggestion)
printf '%sn' setrootns 'cd /manifest' "set $inner" save bye | xmllint --shell tmp.xml
注意:xmllint --xpath '/manifest/application/@*' tmp.xml
的输出包含新行,在set
命令上使用它之前必须删除这些新行
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
对于内容包含多个节点的其他情况,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="xxxxxxxxxxxxxx" android:hardwareAccelerated="true" android:versionCode="xxxxxxxx" android:versionName="xxxxx" package="xxxxxx">
<a>text1</a>
<application android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<b>
<c>text2</c>
</b>
</manifest>
以下XPath应该用于获取除要更改的元素之外的所有内容
xmllint --xpath '/manifest/*[not(name()="application")]' tmp.xml
结果
<a>text1</a>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<b>
<c>text2</c>
</b>
这可能看起来像:
xmlstarlet ed
-i '/manifest/application' -t attr -n android:networkSecurityConfig -v '@xml/network_security_config'
-i '/manifest/application' -t attr -n android:requestLegacyExternalStorage -v true
<in.xml >out.xml