Skip to content

Pass CLI logs to Unity in any case for Android build #1691

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

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ internal void AddAndroidSdkDependencies(string gradleProjectPath)
internal void SetupSymbolsUpload(string unityProjectPath, string gradleProjectPath)
{
var disableSymbolsUpload = false;
var symbolsUpload = new DebugSymbolUpload(_logger, _sentryCliOptions, unityProjectPath, gradleProjectPath, EditorUserBuildSettings.exportAsGoogleAndroidProject, ShouldUploadMapping());
var symbolsUpload = new DebugSymbolUpload(_logger, _sentryCliOptions, unityProjectPath, gradleProjectPath, EditorUserBuildSettings.exportAsGoogleAndroidProject, AndroidUtils.ShouldUploadMapping());

if (_options is not { Enabled: true, AndroidNativeSupportEnabled: true })
{
Expand Down Expand Up @@ -330,34 +330,7 @@ internal static string GetManifestPath(string basePath) =>
.Append("AndroidManifest.xml")
.ToString();

private bool ShouldUploadMapping()
{
var isDebug = EditorUserBuildSettings.development;
var majorVersion = int.Parse(Application.unityVersion.Split('.')[0]);
if (majorVersion < 2020)
{
var buildSettingsType = typeof(EditorUserBuildSettings);
var propertyName = isDebug ? "androidDebugMinification" : "androidReleaseMinification";
var prop = buildSettingsType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);
if (prop != null)
{
var value = (int)prop.GetValue(null);
return value > 0;
}
}
else
{
var type = typeof(PlayerSettings.Android);
var propertyName = isDebug ? "minifyDebug" : "minifyRelease";
var prop = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);
if (prop != null)
{
return (bool)prop.GetValue(null);
}
}

return false;
}
}

internal class AndroidXmlDocument : XmlDocument
Expand Down
38 changes: 38 additions & 0 deletions src/Sentry.Unity.Editor/Android/AndroidUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace Sentry.Unity.Editor.Android
{
internal static class AndroidUtils
{
internal static bool ShouldUploadMapping()
{
var isDebug = EditorUserBuildSettings.development;
var majorVersion = int.Parse(Application.unityVersion.Split('.')[0]);
if (majorVersion < 2020)
{
var buildSettingsType = typeof(EditorUserBuildSettings);
var propertyName = isDebug ? "androidDebugMinification" : "androidReleaseMinification";
var prop = buildSettingsType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);
if (prop != null)
{
var value = (int)prop.GetValue(null);
return value > 0;
}
}
else
{
var type = typeof(PlayerSettings.Android);
var propertyName = isDebug ? "minifyDebug" : "minifyRelease";
var prop = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);
if (prop != null)
{
return (bool)prop.GetValue(null);
}
}

return false;
}
}
}
54 changes: 43 additions & 11 deletions src/Sentry.Unity.Editor/Android/PostBuildCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,40 @@ internal void CheckUploadTaskResult()
}

var unityProjectPath = projectDir.FullName;
CheckUploadSymbolLogs(unityProjectPath);
CheckUploadMappingLogs(unityProjectPath);
}

private void CheckUploadSymbolLogs(string unityProjectPath)
{
var symbolUploadLogPath = Path.Combine(unityProjectPath, "Logs", DebugSymbolUpload.SymbolUploadLogName);
if (HasError(symbolUploadLogPath, out var symbolError, out var symbolLog))
var hasSymbolError = HasError(symbolUploadLogPath, out var symbolError, out var symbolLog);
if (hasSymbolError)
{
_logger.LogWarning($"Symbol upload task error: {symbolError}");
_logger.LogWarning("Symbol upload log file content:");
LogFileContent(symbolLog);
File.WriteAllText(symbolUploadLogPath, symbolLog); // Clean up the log file
}

LogFileContent("Symbol upload log file content:", symbolLog, hasSymbolError);
File.WriteAllText(symbolUploadLogPath, symbolLog); // Clean up the log file
}

private void CheckUploadMappingLogs(string unityProjectPath)
{
if (!AndroidUtils.ShouldUploadMapping())
{
_logger.LogDebug("Minification is disabled. Will not check upload mapping task result.");
return;
}

var mappingUploadLogPath = Path.Combine(unityProjectPath, "Logs", DebugSymbolUpload.MappingUploadLogName);
if (HasError(mappingUploadLogPath, out var mappingError, out var mappingLog))
var hasMappingError = HasError(mappingUploadLogPath, out var mappingError, out var mappingLog);
if (hasMappingError)
{
_logger.LogWarning($"Mapping upload task error: {mappingError}");
_logger.LogWarning("Mapping upload log file content:");
LogFileContent(mappingLog);
File.WriteAllText(mappingUploadLogPath, mappingLog); // Clean up the log file
}

LogFileContent("Mapping upload log file content:", mappingLog, hasMappingError);
File.WriteAllText(mappingUploadLogPath, mappingLog); // Clean up the log file
}

private bool HasError(string filePath, out string error, out string fileContent)
Expand All @@ -91,6 +108,7 @@ private bool HasError(string filePath, out string error, out string fileContent)
var text = File.ReadAllText(filePath);
if (!text.Contains(errorMarker))
{
fileContent = text;
return false;
}

Expand All @@ -105,19 +123,33 @@ private bool HasError(string filePath, out string error, out string fileContent)
return !string.IsNullOrEmpty(error);
}

private void LogFileContent(string fileContent)
private void LogFileContent(string title, string fileContent, bool hasError)
{
var logFunction = new Action<string>(message =>
{
if (hasError)
{
Debug.LogWarning(message);
}
else
{
Debug.Log(message);
}
});

logFunction(title);

const int maxLogLength = 8192;
if (fileContent.Length < maxLogLength)
{
Debug.LogWarning(fileContent);
logFunction(fileContent);
return;
}

for (var i = 0; i < fileContent.Length; i += maxLogLength)
{
var chunkLength = maxLogLength + i > fileContent.Length ? fileContent.Length - i : maxLogLength;
Debug.LogWarning(fileContent.Substring(i, chunkLength));
logFunction(fileContent.Substring(i, chunkLength));
}
}
}
Expand Down
Loading