簡體   English   中英

使用PowerShell腳本從Android清單XML文件中刪除權限

[英]Remove Permissions from android manifest xml file with powershell script

我有一個包含以下內容的清單文件。

<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

我已經測試了“刪除子級”並刪除了所有功能,但無法這樣做。

使用[xml]基本類型方法可能會有更好的解決方案,但這可以起作用:

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

要刪除整行或其他部分,請修改-replace參數。

這是我得到的結果

(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

您可以這樣:

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM