|
| 1 | +# Android Helper Functions |
| 2 | +# Shared utilities for Android device providers (ADB and SauceLabs) |
| 3 | + |
| 4 | +<# |
| 5 | +.SYNOPSIS |
| 6 | +Converts an Android activity path into package name and activity name components. |
| 7 | +
|
| 8 | +.DESCRIPTION |
| 9 | +Converts the ExecutablePath format used by Android apps: "package.name/activity.name" |
| 10 | +Returns a hashtable with PackageName and ActivityName properties. |
| 11 | +
|
| 12 | +.PARAMETER ExecutablePath |
| 13 | +The full activity path in format "package.name/activity.name" |
| 14 | +
|
| 15 | +.EXAMPLE |
| 16 | +ConvertFrom-AndroidActivityPath "io.sentry.unreal.sample/com.epicgames.unreal.GameActivity" |
| 17 | +Returns: @{ PackageName = "io.sentry.unreal.sample"; ActivityName = "com.epicgames.unreal.GameActivity" } |
| 18 | +
|
| 19 | +.EXAMPLE |
| 20 | +ConvertFrom-AndroidActivityPath "com.example.app/.MainActivity" |
| 21 | +Returns: @{ PackageName = "com.example.app"; ActivityName = ".MainActivity" } |
| 22 | +#> |
| 23 | +function ConvertFrom-AndroidActivityPath { |
| 24 | + [CmdletBinding()] |
| 25 | + param( |
| 26 | + [Parameter(Mandatory = $true)] |
| 27 | + [string]$ExecutablePath |
| 28 | + ) |
| 29 | + |
| 30 | + if ($ExecutablePath -notmatch '^([^/]+)/(.+)$') { |
| 31 | + throw "ExecutablePath must be in format 'package.name/activity.name'. Got: $ExecutablePath" |
| 32 | + } |
| 33 | + |
| 34 | + return @{ |
| 35 | + PackageName = $matches[1] |
| 36 | + ActivityName = $matches[2] |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +<# |
| 41 | +.SYNOPSIS |
| 42 | +Validates that Android Intent extras are in the correct format. |
| 43 | +
|
| 44 | +.DESCRIPTION |
| 45 | +Android Intent extras should be passed in the format understood by `am start`. |
| 46 | +This function validates and optionally formats the arguments string. |
| 47 | +
|
| 48 | +Common Intent extra formats: |
| 49 | + -e key value String extra |
| 50 | + -es key value String extra (explicit) |
| 51 | + -ez key true|false Boolean extra |
| 52 | + -ei key value Integer extra |
| 53 | + -el key value Long extra |
| 54 | +
|
| 55 | +.PARAMETER Arguments |
| 56 | +The arguments string to validate/format |
| 57 | +
|
| 58 | +.EXAMPLE |
| 59 | +Test-IntentExtrasFormat "-e cmdline -crash-capture" |
| 60 | +Returns: $true |
| 61 | +
|
| 62 | +.EXAMPLE |
| 63 | +Test-IntentExtrasFormat "-e test true -ez debug false" |
| 64 | +Returns: $true |
| 65 | +#> |
| 66 | +function Test-IntentExtrasFormat { |
| 67 | + [CmdletBinding()] |
| 68 | + param( |
| 69 | + [Parameter(Mandatory = $false)] |
| 70 | + [string]$Arguments |
| 71 | + ) |
| 72 | + |
| 73 | + if ([string]::IsNullOrWhiteSpace($Arguments)) { |
| 74 | + return $true |
| 75 | + } |
| 76 | + |
| 77 | + # Intent extras must start with flags: -e, -es, -ez, -ei, -el, -ef, -eu, etc. |
| 78 | + # Followed by at least one whitespace and additional content |
| 79 | + if ($Arguments -notmatch '^--?[a-z]{1,2}\s+') { |
| 80 | + throw "Invalid Intent extras format: '$Arguments'. Must start with flags like -e, -es, -ez, -ei, -el, etc. followed by key-value pairs." |
| 81 | + } |
| 82 | + |
| 83 | + return $true |
| 84 | +} |
| 85 | + |
| 86 | +<# |
| 87 | +.SYNOPSIS |
| 88 | +Extracts the package name from an APK file using aapt. |
| 89 | +
|
| 90 | +.DESCRIPTION |
| 91 | +Extracts the real package name from an APK file using aapt (Android Asset Packaging Tool). |
| 92 | +Requires aapt or aapt2 to be available in PATH. |
| 93 | +
|
| 94 | +.PARAMETER ApkPath |
| 95 | +Path to the APK file |
| 96 | +
|
| 97 | +.EXAMPLE |
| 98 | +Get-ApkPackageName "MyApp.apk" |
| 99 | +Returns: "com.example.myapp" (actual package name from AndroidManifest.xml) |
| 100 | +
|
| 101 | +.EXAMPLE |
| 102 | +Get-ApkPackageName "SentryPlayground.apk" |
| 103 | +Returns: "io.sentry.sample" |
| 104 | +
|
| 105 | +.NOTES |
| 106 | +Requires aapt or aapt2 to be in PATH or Android SDK to be installed. |
| 107 | +Throws an error if aapt is not available or if the package name cannot be extracted. |
| 108 | +#> |
| 109 | +function Get-ApkPackageName { |
| 110 | + [CmdletBinding()] |
| 111 | + param( |
| 112 | + [Parameter(Mandatory = $true)] |
| 113 | + [string]$ApkPath |
| 114 | + ) |
| 115 | + |
| 116 | + if (-not (Test-Path $ApkPath)) { |
| 117 | + throw "APK file not found: $ApkPath" |
| 118 | + } |
| 119 | + |
| 120 | + if ($ApkPath -notlike '*.apk') { |
| 121 | + throw "File must be an .apk file. Got: $ApkPath" |
| 122 | + } |
| 123 | + |
| 124 | + # Find aapt or aapt2 |
| 125 | + $aaptCmd = Get-Command aapt -ErrorAction SilentlyContinue |
| 126 | + if (-not $aaptCmd) { |
| 127 | + $aaptCmd = Get-Command aapt2 -ErrorAction SilentlyContinue |
| 128 | + } |
| 129 | + |
| 130 | + if (-not $aaptCmd) { |
| 131 | + throw "aapt or aapt2 not found in PATH. Please install Android SDK Build Tools and ensure aapt is available in PATH." |
| 132 | + } |
| 133 | + |
| 134 | + Write-Debug "Using $($aaptCmd.Name) to extract package name from APK" |
| 135 | + |
| 136 | + try { |
| 137 | + $PSNativeCommandUseErrorActionPreference = $false |
| 138 | + $output = & $aaptCmd.Name dump badging $ApkPath 2>&1 |
| 139 | + |
| 140 | + # Parse output for package name: package: name='com.example.app' |
| 141 | + foreach ($line in $output) { |
| 142 | + if ($line -match "package:\s+name='([^']+)'") { |
| 143 | + $packageName = $matches[1] |
| 144 | + Write-Debug "Extracted package name: $packageName" |
| 145 | + return $packageName |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + throw "Failed to extract package name from APK using aapt. APK may be corrupted or invalid." |
| 150 | + } |
| 151 | + finally { |
| 152 | + $PSNativeCommandUseErrorActionPreference = $true |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +<# |
| 157 | +.SYNOPSIS |
| 158 | +Parses logcat output into structured format. |
| 159 | +
|
| 160 | +.DESCRIPTION |
| 161 | +Converts raw logcat output (array of strings) into a consistent format |
| 162 | +that can be used by test utilities like Get-EventIds. |
| 163 | +
|
| 164 | +.PARAMETER LogcatOutput |
| 165 | +Array of logcat log lines (raw output from adb or SauceLabs) |
| 166 | +
|
| 167 | +.EXAMPLE |
| 168 | +$logs = adb -s emulator-5554 logcat -d |
| 169 | +Format-LogcatOutput $logs |
| 170 | +#> |
| 171 | +function Format-LogcatOutput { |
| 172 | + [CmdletBinding()] |
| 173 | + param( |
| 174 | + [Parameter(Mandatory = $false)] |
| 175 | + [object[]]$LogcatOutput |
| 176 | + ) |
| 177 | + |
| 178 | + if ($null -eq $LogcatOutput -or $LogcatOutput.Count -eq 0) { |
| 179 | + return @() |
| 180 | + } |
| 181 | + |
| 182 | + # Ensure output is an array of strings |
| 183 | + return @($LogcatOutput | ForEach-Object { |
| 184 | + if ($null -ne $_) { |
| 185 | + $_.ToString() |
| 186 | + } |
| 187 | + } | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }) |
| 188 | +} |
0 commit comments