這就是我的 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>
我需要在 <application 之后添加以下行
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
uj5u.com熱心網友回復:
如果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 '%s\n' 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>
uj5u.com熱心網友回復:
這可能看起來像:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/515322.html
標籤:linuxxml壳
下一篇:sqlplus-L選項
