Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Android Native initialization without SDK #1714

Merged
merged 21 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Added a fallback for user.id on Android and iOS in case none could be extracted from the native layer ([#1710](https://github.com/getsentry/sentry-unity/pull/1710))
- The IL2CPP exception processor no longer fails when the native support has been disabled ([#1708](https://github.com/getsentry/sentry-unity/pull/1708))
- The SDK now checks whether the Android SDK is available before attempting to initialize it. This prevents `AndroidJavaException: java.lang.ClassNotFoundException: io.sentry.Sentry` from being thrown ([#1714](https://github.com/getsentry/sentry-unity/pull/1714))

### Dependencies

Expand Down
19 changes: 17 additions & 2 deletions src/Sentry.Unity.Android/SentryJava.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void WriteScope(
string? GpuVendorId,
bool? GpuMultiThreadedRendering,
string? GpuGraphicsShaderLevel);
public bool IsSentryJavaPresent();
}

/// <summary>
Expand All @@ -38,6 +39,8 @@ public void WriteScope(
/// <see href="https://github.com/getsentry/sentry-java"/>
internal class SentryJava : ISentryJava
{
private static AndroidJavaObject GetSentryJava() => new AndroidJavaClass("io.sentry.Sentry");

public string? GetInstallationId(IJniExecutor jniExecutor)
{
return jniExecutor.Run(() =>
Expand Down Expand Up @@ -78,8 +81,6 @@ public void Close(IJniExecutor jniExecutor)
});
}

private static AndroidJavaObject GetSentryJava() => new AndroidJavaClass("io.sentry.Sentry");

public void WriteScope(
IJniExecutor jniExecutor,
int? GpuId,
Expand Down Expand Up @@ -125,6 +126,20 @@ public void WriteScope(
});
}

public bool IsSentryJavaPresent()
{
try
{
_ = GetSentryJava();
}
catch (AndroidJavaException)
{
return false;
}

return true;
}

// Implements the io.sentry.ScopeCallback interface.
internal class ScopeCallback : AndroidJavaProxy
{
Expand Down
17 changes: 15 additions & 2 deletions src/Sentry.Unity.Android/SentryNativeAndroid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Sentry.Unity.Android
public static class SentryNativeAndroid
{
internal static IJniExecutor? JniExecutor;
internal static ISentryJava? SentryJava;
internal static ISentryJava SentryJava = new SentryJava();

/// <summary>
/// Configures the native Android support.
Expand All @@ -27,8 +27,16 @@ public static void Configure(SentryUnityOptions options, ISentryUnityInfo sentry
return;
}

if (!SentryJava.IsSentryJavaPresent())
{
options.DiagnosticLogger?.LogError("Android Native Support has been enabled but the " +
"Sentry Java 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;
}

JniExecutor ??= new JniExecutor();
SentryJava ??= new SentryJava();

options.NativeContextWriter = new NativeContextWriter(JniExecutor, SentryJava);
options.ScopeObserver = new AndroidJavaScopeObserver(options, JniExecutor);
Expand Down Expand Up @@ -94,6 +102,11 @@ public static void Configure(SentryUnityOptions options, ISentryUnityInfo sentry
/// </summary>
public static void Close(IDiagnosticLogger? logger = null)
{
if (!SentryJava.IsSentryJavaPresent())
{
return;
}

// Sentry Native is initialized and closed by the Java SDK, no need to call into it directly
logger?.LogDebug("Closing the sentry-java SDK");

Expand Down
2 changes: 2 additions & 0 deletions test/Sentry.Unity.Android.Tests/TestSentryJava.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ public void WriteScope(
bool? GpuMultiThreadedRendering,
string? GpuGraphicsShaderLevel)
{ }

public bool IsSentryJavaPresent() => true;
}
}
Loading