-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathSentryNativeAndroid.cs
More file actions
162 lines (134 loc) · 5.82 KB
/
SentryNativeAndroid.cs
File metadata and controls
162 lines (134 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;
using Sentry.Unity.NativeUtils;
using UnityEngine;
using UnityEngine.Analytics;
namespace Sentry.Unity.Android;
/// <summary>
/// Access to the Sentry native support on Android.
/// </summary>
public static class SentryNativeAndroid
{
// This is an internal static field that gets overwritten during testing. We cannot have it as optional
// parameter on `Configure` due SentryNativeAndroid being public
internal static ISentryJava? SentryJava;
private static IDiagnosticLogger? Logger;
/// <summary>
/// Configures the native Android support.
/// </summary>
/// <param name="options">The Sentry Unity options to use.</param>
public static void Configure(SentryUnityOptions options)
{
Logger = options.DiagnosticLogger;
Logger?.LogInfo("Attempting to configure native support via the Android SDK");
if (!options.AndroidNativeSupportEnabled)
{
Logger?.LogDebug("Native support is disabled for Android");
return;
}
Logger?.LogDebug("Checking whether the Android SDK is present.");
// If it's not been set (in a test)
SentryJava ??= new SentryJava(Logger);
if (!SentryJava.IsSentryJavaPresent())
{
Logger?.LogError("Android Native Support has been enabled but the " +
"Android SDK is missing. This could have been caused by a mismatching" +
"build time / runtime configuration. Please make sure you have " +
"Android Native Support enabled during build time.");
return;
}
Logger?.LogDebug("Checking whether the Android SDK has already been initialized");
if (SentryJava.IsEnabled() is true)
{
Logger?.LogDebug("The Android SDK is already initialized");
}
else
{
Logger?.LogInfo("Initializing the Android SDK");
SentryJava.Init(options);
Logger?.LogDebug("Validating Android SDK initialization");
if (SentryJava.IsEnabled() is not true)
{
Logger?.LogError("Failed to initialize Android Native Support");
return;
}
}
Logger?.LogDebug("Configuring scope sync");
options.NativeContextWriter = new NativeContextWriter(SentryJava);
options.ScopeObserver = new AndroidJavaScopeObserver(options, SentryJava);
options.EnableScopeSync = true;
options.NativeDebugImageProvider = new Native.NativeDebugImageProvider();
options.CrashedLastRun = () =>
{
Logger?.LogDebug("Checking for 'CrashedLastRun'");
var crashedLastRun = SentryJava.CrashedLastRun();
if (crashedLastRun is null)
{
// Could happen if the Android SDK wasn't initialized before the .NET layer.
Logger?
.LogWarning(
"Unclear from the native SDK if the previous run was a crash. Assuming it was not.");
crashedLastRun = false;
}
else
{
Logger?.LogDebug("Native SDK reported: 'crashedLastRun': '{0}'", crashedLastRun);
}
return crashedLastRun.Value;
};
try
{
Logger?.LogDebug("Reinstalling native backend.");
// At this point Unity has taken the signal handler and will not invoke the original handler (Sentry)
// So we register our backend once more to make sure user-defined data is available in the crash report.
SentryNative.ReinstallBackend();
}
catch (Exception e)
{
Logger?.LogError(
e, "Failed to reinstall backend. Captured native crashes will miss scope data and tag.");
}
options.NativeSupportCloseCallback = () => Close(options);
Logger?.LogDebug("Fetching installation ID");
options.DefaultUserId = SentryJava.GetInstallationId();
if (string.IsNullOrEmpty(options.DefaultUserId))
{
// In case we can't get an installation ID we create one and sync that down to the native layer
Logger?.LogDebug(
"Failed to fetch 'Installation ID' from the native SDK. Creating new 'Default User ID'.");
// We fall back to Unity's Analytics Session Info: https://docs.unity3d.com/ScriptReference/Analytics.AnalyticsSessionInfo-userId.html
// It's a randomly generated GUID that gets created immediately after installation helping
// to identify the same instance of the game
options.DefaultUserId = AnalyticsSessionInfo.userId;
if (options.DefaultUserId is not null)
{
options.ScopeObserver.SetUser(new SentryUser { Id = options.DefaultUserId });
}
else
{
Logger?.LogDebug("Failed to create new 'Default User ID'.");
}
}
Logger?.LogInfo("Successfully configured the Android SDK");
}
/// <summary>
/// Closes the native Android support.
/// </summary>
public static void Close(SentryUnityOptions options)
{
Logger?.LogInfo("Attempting to close the Android SDK");
if (!options.IsNativeSupportEnabled())
{
Logger?.LogDebug("Android Native Support is not enabled. Skipping closing the Android SDK");
return;
}
if (SentryJava?.IsSentryJavaPresent() is not true)
{
Logger?.LogDebug("Failed to find Sentry Java. Skipping closing the Android SDK");
return;
}
Logger?.LogDebug("Closing the Android SDK");
SentryJava.Close();
}
}