diff --git a/scripts/smoke-test-android.ps1 b/scripts/smoke-test-android.ps1 index cfb778b1b..260ac2988 100644 --- a/scripts/smoke-test-android.ps1 +++ b/scripts/smoke-test-android.ps1 @@ -25,8 +25,7 @@ Write-Host "#####################################################" $BuildDir = $(GetNewProjectBuildPath) $ApkFileName = "test.apk" $ProcessName = "io.sentry.unity.integrationtest" -$TestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerActivity" -$FallBackTestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerGameActivity" +$TestActivityName = $null # Detected after install via dumpsys $_ArtifactsPath = (Test-Path env:ARTIFACTS_PATH) ? $env:ARTIFACTS_PATH : (Join-Path $BuildDir "../test-artifacts/" $(Get-Date -Format "HHmmss")) @@ -199,6 +198,15 @@ else return 1 } +# Detect the launcher activity from the installed package +$dumpOutput = adb -s $device shell dumpsys package $ProcessName 2>&1 | Out-String +if ($dumpOutput -match "com.unity3d.player.UnityPlayerGameActivity") { + $TestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerGameActivity" +} else { + $TestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerActivity" +} +Write-Log "Detected activity: $TestActivityName" + function ProcessNewLogs([array]$newLogs, [ref]$lastLogCount, [array]$logCache) { if ($newLogs) { $currentLogs = @($newLogs) # Force array creation even for single line @@ -227,21 +235,19 @@ function RunTest([string] $Name, [string] $SuccessString, [string] $FailureStrin adb -s $device shell am force-stop $ProcessName Start-Sleep -Milliseconds 500 - $activityName = $TestActivityName - Write-Log "Setting configuration" # Mark the full-screen notification as acknowledged adb -s $device shell "settings put secure immersive_mode_confirmations confirmed" adb -s $device shell "input keyevent KEYCODE_HOME" - Write-Log "Starting app '$activityName'" + Write-Log "Starting app '$TestActivityName'" # Start the adb command as a background job so we can wait for it to finish with a timeout $job = Start-Job -ScriptBlock { param($device, $activityName, $Name) & adb -s $device shell am start -n $activityName -e test $Name -W 2>&1 - } -ArgumentList $device, $activityName, $Name + } -ArgumentList $device, $TestActivityName, $Name # Wait for the job to complete or to timeout $completed = Wait-Job $job -Timeout 60 @@ -252,27 +258,9 @@ function RunTest([string] $Name, [string] $SuccessString, [string] $FailureStrin return $false } - $output = Receive-Job $job + Receive-Job $job | Out-Null Remove-Job $job - Write-Log "Checking if activity started" - - # Check if the activity failed to start - if ($output -match "Error type 3" -or $output -match "Activity class \{$activityName\} does not exist.") - { - $activityName = $FallBackTestActivityName - Write-Log "Trying fallback activity $activityName" - - $output = & adb -s $device shell am start -n $activityName -e test $Name -W 2>&1 - - # Check if the fallback activity failed to start - if ($output -match "Error type 3" -or $output -match "Activity class \{$activityName\} does not exist.") - { - Write-Log "Activity does not exist" - return $false - } - } - Write-Log "Activity started successfully" $appPID = PidOf $device $ProcessName diff --git a/src/Sentry.Unity.Android/SentryJava.cs b/src/Sentry.Unity.Android/SentryJava.cs index ca74d7668..9a68442aa 100644 --- a/src/Sentry.Unity.Android/SentryJava.cs +++ b/src/Sentry.Unity.Android/SentryJava.cs @@ -109,8 +109,8 @@ public void Init(SentryUnityOptions options) try { using var sentry = new AndroidJavaClass("io.sentry.android.core.SentryAndroid"); - using var context = new AndroidJavaClass("com.unity3d.player.UnityPlayer") - .GetStatic("currentActivity"); + using var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); + using var context = unityPlayer.GetStatic("currentActivity"); sentry.CallStatic("init", context, new AndroidOptionsConfiguration(androidOptions => { @@ -120,9 +120,9 @@ public void Init(SentryUnityOptions options) androidOptions.Call("setDist", options.Distribution); androidOptions.Call("setEnvironment", options.Environment); - var sentryLevelClass = new AndroidJavaClass("io.sentry.SentryLevel"); + using var sentryLevelClass = new AndroidJavaClass("io.sentry.SentryLevel"); var levelString = GetLevelString(options.DiagnosticLevel); - var sentryLevel = sentryLevelClass.GetStatic(levelString); + using var sentryLevel = sentryLevelClass.GetStatic(levelString); androidOptions.Call("setDiagnosticLevel", sentryLevel); if (options.SampleRate.HasValue) @@ -267,7 +267,7 @@ public bool IsSentryJavaPresent() { try { - _ = GetSentryJava(); + using var _ = GetSentryJava(); } catch (AndroidJavaException) { @@ -387,9 +387,9 @@ internal void RunJniSafe(Action action, [CallerMemberName] string actionName = " { action.Invoke(); } - catch (Exception) + catch (Exception e) { - _logger?.LogError("Calling '{0}' failed.", actionName); + _logger?.LogError(e, "Calling '{0}' failed.", actionName); } } else @@ -423,9 +423,9 @@ private void SyncScope() { action.Invoke(); } - catch (Exception) + catch (Exception e) { - _logger?.LogError("Calling '{0}' failed.", actionName); + _logger?.LogError(e, "Calling '{0}' failed.", actionName); } } } @@ -450,7 +450,8 @@ public void Close() if (!MainThreadData.IsMainThread()) { - _logger?.LogError("Calling Close() on Android SDK requires running on MainThread"); + _logger?.LogError("Calling Close() on Android SDK requires running on MainThread. " + + "Scope sync thread stopped but Java SDK was not closed."); return; } diff --git a/test/IntegrationTest/Integration.Tests.ps1 b/test/IntegrationTest/Integration.Tests.ps1 index a07a897bc..dc36f22c0 100644 --- a/test/IntegrationTest/Integration.Tests.ps1 +++ b/test/IntegrationTest/Integration.Tests.ps1 @@ -30,6 +30,7 @@ BeforeAll { Write-Host "Running $Action..." $extras = @("-e", "test", $Action) + $runResult = Invoke-DeviceApp -ExecutablePath $script:AndroidComponent -Arguments $extras # Save result to JSON file