Skip to content

Commit

Permalink
feat: proguard support (#844)
Browse files Browse the repository at this point in the history
* chore: remove AndroidLogcatSettings.asset from the repo

* chore: cleanup DebugSymbolUpload

* refactor: AndroidManifestConfiguration - streamline logging

* feat: proguard rule setup

* chore: update changelog

* chore: update package snapshot

* chore: cleanup
  • Loading branch information
vaind authored Jun 23, 2022
1 parent b660d1c commit 50658f5
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 142 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Launch a setup wizard after installation ([#780](https://github.com/getsentry/sentry-unity/pull/780))
- Disable AutoSessionTracking on unknown platforms ([#840](https://github.com/getsentry/sentry-unity/pull/840))
- Support Android apps minified with Proguard ([#844](https://github.com/getsentry/sentry-unity/pull/844))
- Bump Cocoa SDK to v7.17.0 ([#802](https://github.com/getsentry/sentry-unity/pull/802) & [#821](https://github.com/getsentry/sentry-unity/pull/821))
- [changelog](https://github.com/getsentry/sentry-cocoa/blob/master/CHANGELOG.md#7170)
- [diff](https://github.com/getsentry/sentry-cocoa/compare/7.16.0...7.17.0)
Expand Down
9 changes: 9 additions & 0 deletions package-dev/Plugins/Android/proguard-sentry-unity.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Unity: keep names on select sentry-java classes & their methods - we use string-based JNI lookup in our integration.
-keep class io.sentry.Sentry { *; }
-keep class io.sentry.SentryLevel { *; }
-keep class io.sentry.SentryOptions { *; }
-keep class io.sentry.Hub { *; }
-keep class io.sentry.Breadcrumb { *; }
-keep class io.sentry.Scope { *; }
-keep class io.sentry.ScopeCallback { *; }
-keep class io.sentry.protocol.** { *; }
8 changes: 8 additions & 0 deletions samples/unity-of-bugs/Assets/Plugins/Android.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-keep class unity.of.bugs.** { *; }

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 0 additions & 95 deletions samples/unity-of-bugs/UserSettings/AndroidLogcatSettings.asset

This file was deleted.

75 changes: 47 additions & 28 deletions src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AndroidManifestConfiguration : IPostGenerateGradleAndroidProject
{
private readonly SentryUnityOptions? _options;
private readonly SentryCliOptions? _sentryCliOptions;
private readonly IUnityLoggerInterceptor? _interceptor;
private readonly IDiagnosticLogger _logger;

private readonly bool _isDevelopmentBuild;
private readonly ScriptingImplementation _scriptingImplementation;
Expand Down Expand Up @@ -43,8 +43,7 @@ internal AndroidManifestConfiguration(
{
_options = getOptions();
_sentryCliOptions = getSentryCliOptions();

_interceptor = interceptor;
_logger = _options?.DiagnosticLogger ?? new UnityLogger(_options ?? new SentryUnityOptions(), interceptor);

_isDevelopmentBuild = isDevelopmentBuild;
_scriptingImplementation = scriptingImplementation;
Expand All @@ -57,6 +56,7 @@ public void OnPostGenerateGradleAndroidProject(string basePath)
var unityProjectPath = Directory.GetParent(Application.dataPath).FullName;
var gradleProjectPath = Directory.GetParent(basePath).FullName;
SetupSymbolsUpload(unityProjectPath, gradleProjectPath);
SetupProguard(gradleProjectPath);
}

internal void ModifyManifest(string basePath)
Expand All @@ -69,26 +69,24 @@ internal void ModifyManifest(string basePath)
}

var disableAutoInit = false;
var logger = _options?.DiagnosticLogger ?? new UnityLogger(_options ?? new SentryUnityOptions(), _interceptor);

if (_options is null)
{
logger.LogWarning("Android Native support disabled. " +
_logger.LogWarning("Android Native support disabled. " +
"Sentry has not been configured. You can do that through the editor: Tools -> Sentry");
disableAutoInit = true;
}
else if (!_options.IsValid())
{
logger.LogDebug("Native support disabled.");
_logger.LogDebug("Native support disabled.");
disableAutoInit = true;
}
else if (!_options.AndroidNativeSupportEnabled)
{
logger.LogDebug("Android Native support disabled through the options.");
_logger.LogDebug("Android Native support disabled through the options.");
disableAutoInit = true;
}

var androidManifest = new AndroidManifest(manifestPath, _options?.DiagnosticLogger);
var androidManifest = new AndroidManifest(manifestPath, _logger);
androidManifest.RemovePreviousConfigurations();
androidManifest.AddDisclaimerComment();

Expand All @@ -98,43 +96,43 @@ internal void ModifyManifest(string basePath)
return;
}

logger.LogDebug("Configuring Sentry options on AndroidManifest: {0}", basePath);
_logger.LogDebug("Configuring Sentry options on AndroidManifest: {0}", basePath);
androidManifest.SetSDK("sentry.java.android.unity");
logger.LogDebug("Setting DSN: {0}", _options!.Dsn);
_logger.LogDebug("Setting DSN: {0}", _options!.Dsn);
androidManifest.SetDsn(_options.Dsn!);
if (_options.Debug)
{
logger.LogDebug("Setting Debug: {0}", _options.Debug);
_logger.LogDebug("Setting Debug: {0}", _options.Debug);
androidManifest.SetDebug(_options.Debug);
}

if (_options.Release is not null)
{
logger.LogDebug("Setting Release: {0}", _options.Release);
_logger.LogDebug("Setting Release: {0}", _options.Release);
androidManifest.SetRelease(_options.Release);
}

if (_options.Environment is not null)
{
logger.LogDebug("Setting Environment: {0}", _options.Environment);
_logger.LogDebug("Setting Environment: {0}", _options.Environment);
androidManifest.SetEnvironment(_options.Environment);
}

logger.LogDebug("Setting DiagnosticLevel: {0}", _options.DiagnosticLevel);
_logger.LogDebug("Setting DiagnosticLevel: {0}", _options.DiagnosticLevel);
androidManifest.SetLevel(_options.DiagnosticLevel);

if (_options.SampleRate.HasValue)
{
logger.LogDebug("Setting SampleRate: {0}", _options.SampleRate);
_logger.LogDebug("Setting SampleRate: {0}", _options.SampleRate);
androidManifest.SetSampleRate(_options.SampleRate.Value);
}

// TODO: Missing on AndroidManifest
// options.DiagnosticLogger?.LogDebug("Setting MaxBreadcrumbs: {0}", options.MaxBreadcrumbs);
// _logger.LogDebug("Setting MaxBreadcrumbs: {0}", options.MaxBreadcrumbs);
// androidManifest.SetMaxBreadcrumbs(options.MaxBreadcrumbs);
// options.DiagnosticLogger?.LogDebug("Setting MaxCacheItems: {0}", options.MaxCacheItems);
// _logger.LogDebug("Setting MaxCacheItems: {0}", options.MaxCacheItems);
// androidManifest.SetMaxCacheItems(options.MaxCacheItems);
// options.DiagnosticLogger?.LogDebug("Setting SendDefaultPii: {0}", options.SendDefaultPii);
// _logger.LogDebug("Setting SendDefaultPii: {0}", options.SendDefaultPii);
// // androidManifest.SetSendDefaultPii(options.SendDefaultPii);

// Disabling the native in favor of the C# layer for now
Expand All @@ -150,8 +148,7 @@ internal void ModifyManifest(string basePath)
internal void SetupSymbolsUpload(string unityProjectPath, string gradleProjectPath)
{
var disableSymbolsUpload = false;
var logger = _options?.DiagnosticLogger ?? new UnityLogger(new SentryUnityOptions());
var symbolsUpload = new DebugSymbolUpload(logger, _sentryCliOptions, unityProjectPath, gradleProjectPath,
var symbolsUpload = new DebugSymbolUpload(_logger, _sentryCliOptions, unityProjectPath, gradleProjectPath,
PlayerSettings.GetScriptingBackend(BuildTargetGroup.Android), EditorUserBuildSettings.exportAsGoogleAndroidProject);

if (_options is null || !_options.Enabled || !_options.AndroidNativeSupportEnabled)
Expand All @@ -161,10 +158,10 @@ internal void SetupSymbolsUpload(string unityProjectPath, string gradleProjectPa

if (_sentryCliOptions is null)
{
logger.LogWarning("Failed to load sentry-cli options.");
_logger.LogWarning("Failed to load sentry-cli options.");
disableSymbolsUpload = true;
}
else if (!_sentryCliOptions.IsValid(logger, _isDevelopmentBuild))
else if (!_sentryCliOptions.IsValid(_logger, _isDevelopmentBuild))
{
disableSymbolsUpload = true;
}
Expand All @@ -177,7 +174,7 @@ internal void SetupSymbolsUpload(string unityProjectPath, string gradleProjectPa

try
{
logger.LogInfo("Adding automated debug symbols upload to the gradle project.");
_logger.LogInfo("Adding automated debug symbols upload to the gradle project.");

var sentryCliPath = SentryCli.SetupSentryCli();
SentryCli.CreateSentryProperties(gradleProjectPath, _sentryCliOptions!, _options!);
Expand All @@ -191,7 +188,29 @@ internal void SetupSymbolsUpload(string unityProjectPath, string gradleProjectPa
}
catch (Exception e)
{
logger.LogError("Failed to add the automatic symbols upload to the gradle project", e);
_logger.LogError("Failed to add the automatic symbols upload to the gradle project", e);
}
}

internal void SetupProguard(string gradleProjectPath)
{
var tool = new ProguardSetup(_logger, gradleProjectPath);
var pluginEnabled = _options is not null && _options.Enabled && _options.AndroidNativeSupportEnabled;

try
{
if (pluginEnabled)
{
tool.AddToGradleProject();
}
else
{
tool.RemoveFromGradleProject();
}
}
catch (Exception e)
{
_logger.LogError($"Failed to {(pluginEnabled ? "add" : "remove")} Proguard rules in the gradle project", e);
}
}

Expand Down Expand Up @@ -241,9 +260,9 @@ internal class AndroidManifest : AndroidXmlDocument
private const string Disclaimer = "GENERATED BY SENTRY. Changes to the Sentry options will be lost!";

private readonly XmlElement _applicationElement;
private readonly IDiagnosticLogger? _logger;
private readonly IDiagnosticLogger _logger;

public AndroidManifest(string path, IDiagnosticLogger? logger) : base(path)
public AndroidManifest(string path, IDiagnosticLogger logger) : base(path)
{
_applicationElement = (XmlElement)SelectSingleNode("/manifest/application");
_logger = logger;
Expand All @@ -268,7 +287,7 @@ internal void RemovePreviousConfigurations()
if (attr.Prefix.Equals(AndroidNsPrefix) && attr.LocalName.Equals("name") &&
attr.Value.StartsWith(SentryPrefix))
{
_logger?.LogDebug("Removing AndroidManifest meta-data '{0}'", attr.Value);
_logger.LogDebug("Removing AndroidManifest meta-data '{0}'", attr.Value);
nodesToRemove.Add(node);
break;
}
Expand Down
Loading

0 comments on commit 50658f5

Please sign in to comment.