Skip to content

Commit dec6301

Browse files
authored
chore: Improve SentryJava logging (#2556)
1 parent 4cae0a2 commit dec6301

3 files changed

Lines changed: 25 additions & 35 deletions

File tree

scripts/smoke-test-android.ps1

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ Write-Host "#####################################################"
2525
$BuildDir = $(GetNewProjectBuildPath)
2626
$ApkFileName = "test.apk"
2727
$ProcessName = "io.sentry.unity.integrationtest"
28-
$TestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerActivity"
29-
$FallBackTestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerGameActivity"
28+
$TestActivityName = $null # Detected after install via dumpsys
3029

3130
$_ArtifactsPath = (Test-Path env:ARTIFACTS_PATH) ? $env:ARTIFACTS_PATH : (Join-Path $BuildDir "../test-artifacts/" $(Get-Date -Format "HHmmss"))
3231

@@ -199,6 +198,15 @@ else
199198
return 1
200199
}
201200

201+
# Detect the launcher activity from the installed package
202+
$dumpOutput = adb -s $device shell dumpsys package $ProcessName 2>&1 | Out-String
203+
if ($dumpOutput -match "com.unity3d.player.UnityPlayerGameActivity") {
204+
$TestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerGameActivity"
205+
} else {
206+
$TestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerActivity"
207+
}
208+
Write-Log "Detected activity: $TestActivityName"
209+
202210
function ProcessNewLogs([array]$newLogs, [ref]$lastLogCount, [array]$logCache) {
203211
if ($newLogs) {
204212
$currentLogs = @($newLogs) # Force array creation even for single line
@@ -227,21 +235,19 @@ function RunTest([string] $Name, [string] $SuccessString, [string] $FailureStrin
227235
adb -s $device shell am force-stop $ProcessName
228236
Start-Sleep -Milliseconds 500
229237

230-
$activityName = $TestActivityName
231-
232238
Write-Log "Setting configuration"
233239

234240
# Mark the full-screen notification as acknowledged
235241
adb -s $device shell "settings put secure immersive_mode_confirmations confirmed"
236242
adb -s $device shell "input keyevent KEYCODE_HOME"
237243

238-
Write-Log "Starting app '$activityName'"
244+
Write-Log "Starting app '$TestActivityName'"
239245

240246
# Start the adb command as a background job so we can wait for it to finish with a timeout
241247
$job = Start-Job -ScriptBlock {
242248
param($device, $activityName, $Name)
243249
& adb -s $device shell am start -n $activityName -e test $Name -W 2>&1
244-
} -ArgumentList $device, $activityName, $Name
250+
} -ArgumentList $device, $TestActivityName, $Name
245251

246252
# Wait for the job to complete or to timeout
247253
$completed = Wait-Job $job -Timeout 60
@@ -252,27 +258,9 @@ function RunTest([string] $Name, [string] $SuccessString, [string] $FailureStrin
252258
return $false
253259
}
254260

255-
$output = Receive-Job $job
261+
Receive-Job $job | Out-Null
256262
Remove-Job $job
257263

258-
Write-Log "Checking if activity started"
259-
260-
# Check if the activity failed to start
261-
if ($output -match "Error type 3" -or $output -match "Activity class \{$activityName\} does not exist.")
262-
{
263-
$activityName = $FallBackTestActivityName
264-
Write-Log "Trying fallback activity $activityName"
265-
266-
$output = & adb -s $device shell am start -n $activityName -e test $Name -W 2>&1
267-
268-
# Check if the fallback activity failed to start
269-
if ($output -match "Error type 3" -or $output -match "Activity class \{$activityName\} does not exist.")
270-
{
271-
Write-Log "Activity does not exist"
272-
return $false
273-
}
274-
}
275-
276264
Write-Log "Activity started successfully"
277265

278266
$appPID = PidOf $device $ProcessName

src/Sentry.Unity.Android/SentryJava.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ public void Init(SentryUnityOptions options)
109109
try
110110
{
111111
using var sentry = new AndroidJavaClass("io.sentry.android.core.SentryAndroid");
112-
using var context = new AndroidJavaClass("com.unity3d.player.UnityPlayer")
113-
.GetStatic<AndroidJavaObject>("currentActivity");
112+
using var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
113+
using var context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
114114

115115
sentry.CallStatic("init", context, new AndroidOptionsConfiguration(androidOptions =>
116116
{
@@ -120,9 +120,9 @@ public void Init(SentryUnityOptions options)
120120
androidOptions.Call("setDist", options.Distribution);
121121
androidOptions.Call("setEnvironment", options.Environment);
122122

123-
var sentryLevelClass = new AndroidJavaClass("io.sentry.SentryLevel");
123+
using var sentryLevelClass = new AndroidJavaClass("io.sentry.SentryLevel");
124124
var levelString = GetLevelString(options.DiagnosticLevel);
125-
var sentryLevel = sentryLevelClass.GetStatic<AndroidJavaObject>(levelString);
125+
using var sentryLevel = sentryLevelClass.GetStatic<AndroidJavaObject>(levelString);
126126
androidOptions.Call("setDiagnosticLevel", sentryLevel);
127127

128128
if (options.SampleRate.HasValue)
@@ -267,7 +267,7 @@ public bool IsSentryJavaPresent()
267267
{
268268
try
269269
{
270-
_ = GetSentryJava();
270+
using var _ = GetSentryJava();
271271
}
272272
catch (AndroidJavaException)
273273
{
@@ -387,9 +387,9 @@ internal void RunJniSafe(Action action, [CallerMemberName] string actionName = "
387387
{
388388
action.Invoke();
389389
}
390-
catch (Exception)
390+
catch (Exception e)
391391
{
392-
_logger?.LogError("Calling '{0}' failed.", actionName);
392+
_logger?.LogError(e, "Calling '{0}' failed.", actionName);
393393
}
394394
}
395395
else
@@ -423,9 +423,9 @@ private void SyncScope()
423423
{
424424
action.Invoke();
425425
}
426-
catch (Exception)
426+
catch (Exception e)
427427
{
428-
_logger?.LogError("Calling '{0}' failed.", actionName);
428+
_logger?.LogError(e, "Calling '{0}' failed.", actionName);
429429
}
430430
}
431431
}
@@ -450,7 +450,8 @@ public void Close()
450450

451451
if (!MainThreadData.IsMainThread())
452452
{
453-
_logger?.LogError("Calling Close() on Android SDK requires running on MainThread");
453+
_logger?.LogError("Calling Close() on Android SDK requires running on MainThread. " +
454+
"Scope sync thread stopped but Java SDK was not closed.");
454455
return;
455456
}
456457

test/IntegrationTest/Integration.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ BeforeAll {
3030
Write-Host "Running $Action..."
3131

3232
$extras = @("-e", "test", $Action)
33+
3334
$runResult = Invoke-DeviceApp -ExecutablePath $script:AndroidComponent -Arguments $extras
3435

3536
# Save result to JSON file

0 commit comments

Comments
 (0)