使用powershell脚本从android清单xml文件中删除权限

时间:2018-01-11 10:39:15

标签: xml powershell android-manifest

我有一个包含以下内容的清单文件。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.dlginventory.dev"
    android:versionCode="1"
    android:versionName="1.0">

    <!-- explicity remove -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <!-- Approximate location - If you want to use promptLocation for letting OneSignal know the user location. -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <!--  Precise location If you want to use promptLocation for letting OneSignal know the user location. -->



</manifest>

我想通过powershell删除最后两个权限,但无法执行此操作。 我可以获得所需的权限但无法删除。 这是我用来获取内容的脚本。

$androidManifextFile = "C:\Users\BilalAbbasi\Desktop\Temp\AndroidManifest.xml"
        $newPackageName = "com.dlginventory"

        $xml = [Xml](Get-Content $androidManifextFile) #this loads the config as XML
        $rootElements = $xml.get_DocumentElement(); #this gets all root elements only

        ##$permissionElements = $rootElements.'uses-permission'
        ##$element1 = $permissionElements.name -eq "android.permission.ACCESS_FINE_LOCATION"
        $element1 = $rootElements.'uses-permission'.name -eq "android.permission.ACCESS_FINE_LOCATION"
        $element2 = $rootElements.'uses-permission'.name -eq "android.permission.ACCESS_COARSE_LOCATION"

        $rootElements.package = $newPackageName #update the package name
        $xml.Save($androidManifextFile); #save the file in xml format

我使用Remove child进行测试并删除所有功能但无法执行此操作。

3 个答案:

答案 0 :(得分:0)

使用[xml]基类型方法可能有更好的解决方案,但这有效:

$File = (Get-Content $androidManifextFile)
$File -replace "android.permission.ACCESS_COARSE_LOCATION", "" | Set-Content $androidManifextFile

要删除整行或其他部分,请修改-replace参数。

答案 1 :(得分:0)

这是我得到的结果

(Get-Content $androidManifextFile) | Where-Object { $_ -notmatch "android.permission.ACCESS_FINE_LOCATION" } | Set-Content $androidManifextFile
(Get-Content $androidManifextFile) | Where-Object { $_ -notmatch "android.permission.ACCESS_COARSE_LOCATION" } | Set-Content $androidManifextFile

答案 2 :(得分:0)

你可以这样做:

$androidManifextFile = "C:\Users\BilalAbbasi\Desktop\Temp\AndroidManifest.xml"
$newPackageName = "com.dlginventory"

$xml = [Xml](Get-Content $androidManifextFile) #this loads the config as XML

# Define a list of nodes to remove
$nodesToRemove = "android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"

# Select all "uses-permission" nodes
# Match them against the list of nodes to remove
# Remove each match from the document
$xml.manifest.SelectNodes("uses-permission") | Where-Object { $_.name -in $nodesToRemove } | ForEach-Object { $xml.Manifest.RemoveChild($_) }

$xml.manifest.package = $newPackageName #update the package name

$xml.Save($androidManifextFile); #save the file in xml format