Skip to content

Commit f347e90

Browse files
authored
feat: SentryUnityInfo for build-time variables at runtime (#531)
1 parent 1455077 commit f347e90

File tree

7 files changed

+46
-26
lines changed

7 files changed

+46
-26
lines changed

package-dev/Runtime/SentryInitialization.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,12 @@ public static void Init()
2727
var options = ScriptableSentryUnityOptions.LoadSentryUnityOptions();
2828
if (options.ShouldInitializeSdk())
2929
{
30+
var sentryUnityInfo = new SentryUnityInfo();
3031

3132
#if SENTRY_NATIVE_IOS
3233
SentryNativeIos.Configure(options);
3334
#elif SENTRY_NATIVE_ANDROID
34-
var il2cpp =
35-
#if ENABLE_IL2CPP
36-
true;
37-
#else
38-
false;
39-
#endif
40-
SentryNativeAndroid.Configure(options, il2cpp);
35+
SentryNativeAndroid.Configure(options, sentryUnityInfo);
4136
#endif
4237

4338
SentryUnity.Init(options);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Sentry.Unity;
2+
3+
public class SentryUnityInfo : ISentryUnityInfo
4+
{
5+
public bool IL2CPP
6+
{
7+
get =>
8+
#if ENABLE_IL2CPP
9+
true;
10+
#else
11+
false;
12+
#endif
13+
}
14+
}

samples/unity-of-bugs/Assets/Scripts/SmokeTester.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,14 @@ void Verify(HttpRequestMessage message)
7474
options.DiagnosticLogger = new ConsoleDiagnosticLogger(SentryLevel.Debug);
7575
options.CreateHttpClientHandler = () => new TestHandler(Verify);
7676

77+
var sentryUnityInfo = new SentryUnityInfo();
78+
7779
#if SENTRY_NATIVE_IOS
7880
Debug.Log("SMOKE TEST: Configure Native iOS.");
7981
SentryNativeIos.Configure(options);
8082
#elif SENTRY_NATIVE_ANDROID
8183
Debug.Log("SMOKE TEST: Configure Native Android.");
82-
var il2cpp =
83-
#if ENABLE_IL2CPP
84-
true;
85-
#else
86-
false;
87-
#endif
88-
SentryNativeAndroid.Configure(options, il2cpp);
84+
SentryNativeAndroid.Configure(options, sentryUnityInfo);
8985
#endif
9086

9187
Debug.Log("SMOKE TEST: SentryUnity Init.");

src/Sentry.Unity.Android/SentryNativeAndroid.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class SentryNativeAndroid
1212
/// Configures the native Android support.
1313
/// </summary>
1414
/// <param name="options">The Sentry Unity options to use.</param>
15-
public static void Configure(SentryUnityOptions options, bool il2cpp)
15+
public static void Configure(SentryUnityOptions options, ISentryUnityInfo sentryUnityInfo)
1616
{
1717
if (options.AndroidNativeSupportEnabled)
1818
{
@@ -39,7 +39,7 @@ public static void Configure(SentryUnityOptions options, bool il2cpp)
3939

4040
// When running on Mono, we shouldn't take over the signal handler because its used to propagate exceptions into the VM.
4141
// If we take over, a C# null reference ends up crashing the app.
42-
if (il2cpp)
42+
if (sentryUnityInfo.IL2CPP)
4343
{
4444
// At this point Unity has taken the signal handler and will not invoke the original handler (Sentry)
4545
// So we register our backend once more to make sure user-defined data is available in the crash report.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Sentry.Unity
2+
{
3+
public interface ISentryUnityInfo
4+
{
5+
public bool IL2CPP { get; }
6+
}
7+
}

test/Scripts.Tests/package-release.zip.snapshot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ Runtime/Sentry.xml
301301
Runtime/Sentry.xml.meta
302302
Runtime/SentryInitialization.cs
303303
Runtime/SentryInitialization.cs.meta
304+
Runtime/SentryUnityInfo.cs
305+
Runtime/SentryUnityInfo.cs.meta
304306
Samples~/unity-of-bugs/Scenes.meta
305307
Samples~/unity-of-bugs/Scripts.meta
306308
Samples~/unity-of-bugs/Scenes/1_Bugfarm.unity

test/Sentry.Unity.Android.Tests/SentryNativeAndroidTests.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Threading;
33
using NUnit.Framework;
4+
using Sentry.Unity;
45

56
namespace Sentry.Unity.Android.Tests
67
{
@@ -9,7 +10,7 @@ public class SentryNativeAndroidTests
910
private bool _reinstallCalled;
1011
private Action? _originalReinstallSentryNativeBackendStrategy;
1112
private Action _fakeReinstallSentryNativeBackendStrategy;
12-
private bool _il2cpp;
13+
private TestSentryUnityInfo _sentryUnityInfo = null!;
1314

1415
public SentryNativeAndroidTests()
1516
=> _fakeReinstallSentryNativeBackendStrategy = () => _reinstallCalled = true;
@@ -21,7 +22,7 @@ public void SetUp()
2122
Interlocked.Exchange(ref SentryNative.ReinstallSentryNativeBackendStrategy,
2223
_fakeReinstallSentryNativeBackendStrategy);
2324
_reinstallCalled = false;
24-
_il2cpp = false;
25+
_sentryUnityInfo = new TestSentryUnityInfo { IL2CPP = false };
2526
}
2627

2728
[TearDown]
@@ -34,39 +35,39 @@ public void TearDown() =>
3435
public void Configure_DefaultConfiguration_SetsScopeObserver()
3536
{
3637
var options = new SentryUnityOptions();
37-
SentryNativeAndroid.Configure(options, _il2cpp);
38+
SentryNativeAndroid.Configure(options, _sentryUnityInfo);
3839
Assert.IsAssignableFrom<AndroidJavaScopeObserver>(options.ScopeObserver);
3940
}
4041

4142
[Test]
4243
public void Configure_DefaultConfiguration_SetsCrashedLastRun()
4344
{
4445
var options = new SentryUnityOptions();
45-
SentryNativeAndroid.Configure(options, _il2cpp);
46+
SentryNativeAndroid.Configure(options, _sentryUnityInfo);
4647
Assert.IsNotNull(options.CrashedLastRun);
4748
}
4849

4950
[Test]
5051
public void Configure_NativeAndroidSupportDisabled_ObserverIsNull()
5152
{
5253
var options = new SentryUnityOptions { AndroidNativeSupportEnabled = false };
53-
SentryNativeAndroid.Configure(options, _il2cpp);
54+
SentryNativeAndroid.Configure(options, _sentryUnityInfo);
5455
Assert.Null(options.ScopeObserver);
5556
}
5657

5758
[Test]
5859
public void Configure_DefaultConfiguration_EnablesScopeSync()
5960
{
6061
var options = new SentryUnityOptions();
61-
SentryNativeAndroid.Configure(options, _il2cpp);
62+
SentryNativeAndroid.Configure(options, _sentryUnityInfo);
6263
Assert.True(options.EnableScopeSync);
6364
}
6465

6566
[Test]
6667
public void Configure_NativeAndroidSupportDisabled_DisabledScopeSync()
6768
{
6869
var options = new SentryUnityOptions { AndroidNativeSupportEnabled = false };
69-
SentryNativeAndroid.Configure(options, _il2cpp);
70+
SentryNativeAndroid.Configure(options, _sentryUnityInfo);
7071
Assert.False(options.EnableScopeSync);
7172
}
7273

@@ -75,10 +76,10 @@ public void Configure_NativeAndroidSupportDisabled_DisabledScopeSync()
7576
[TestCase(false, false)]
7677
public void Configure_IL2CPP_ReInitializesNativeBackendOnlyOnIL2CPP(bool il2cpp, bool expectedReinstall)
7778
{
78-
_il2cpp = il2cpp;
79+
_sentryUnityInfo.IL2CPP = il2cpp;
7980
Assert.False(_reinstallCalled); // Sanity check
8081

81-
SentryNativeAndroid.Configure(new(), _il2cpp);
82+
SentryNativeAndroid.Configure(new(), _sentryUnityInfo);
8283

8384
Assert.AreEqual(expectedReinstall, _reinstallCalled);
8485
}
@@ -87,8 +88,13 @@ public void Configure_IL2CPP_ReInitializesNativeBackendOnlyOnIL2CPP(bool il2cpp,
8788
public void Configure_NativeAndroidSupportDisabled_DoesNotReInitializeNativeBackend()
8889
{
8990
var options = new SentryUnityOptions { AndroidNativeSupportEnabled = false };
90-
SentryNativeAndroid.Configure(options, _il2cpp);
91+
SentryNativeAndroid.Configure(options, _sentryUnityInfo);
9192
Assert.False(_reinstallCalled);
9293
}
9394
}
95+
96+
public class TestSentryUnityInfo : ISentryUnityInfo
97+
{
98+
public bool IL2CPP { get; set; }
99+
}
94100
}

0 commit comments

Comments
 (0)