-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathAndroidManifestConfiguration.cs
518 lines (427 loc) · 19.5 KB
/
AndroidManifestConfiguration.cs
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
using Sentry.Extensibility;
using Sentry.Unity.Editor.ConfigurationWindow;
using UnityEditor;
using UnityEditor.Android;
using UnityEngine;
namespace Sentry.Unity.Editor.Android;
// https://github.com/getsentry/sentry-java/blob/d3764bfc97eed22564a1e23ba96fa73ad2685498/sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java#L83-L217
public class PostGenerateGradleAndroidProject : IPostGenerateGradleAndroidProject
{
public int callbackOrder
{
get
{
var result = 1;
var options = SentryScriptableObject.LoadOptions();
if (options != null)
{
result = options.PostGenerateGradleProjectCallbackOrder;
}
return result;
}
}
public void OnPostGenerateGradleAndroidProject(string basePath)
{
var androidManifestConfiguration = new AndroidManifestConfiguration();
androidManifestConfiguration.OnPostGenerateGradleAndroidProject(basePath);
}
}
public class AndroidManifestConfiguration
{
private readonly SentryUnityOptions? _options;
private readonly SentryCliOptions? _sentryCliOptions;
private readonly IDiagnosticLogger _logger;
private readonly bool _isDevelopmentBuild;
private readonly ScriptingImplementation _scriptingImplementation;
public AndroidManifestConfiguration()
: this(
SentryScriptableObject.ConfiguredBuildTimeOptions,
isDevelopmentBuild: EditorUserBuildSettings.development,
#pragma warning disable CS0618
scriptingImplementation: PlayerSettings.GetScriptingBackend(BuildTargetGroup.Android))
#pragma warning restore CS0618
{ }
// Testing
internal AndroidManifestConfiguration(
Func<(SentryUnityOptions?, SentryCliOptions?)> getOptions,
bool isDevelopmentBuild,
ScriptingImplementation scriptingImplementation,
ILogger? logger = null)
{
(_options, _sentryCliOptions) = getOptions();
_logger = _options?.DiagnosticLogger ?? new UnityLogger(_options ?? new SentryUnityOptions(), logger);
_isDevelopmentBuild = isDevelopmentBuild;
_scriptingImplementation = scriptingImplementation;
}
public void OnPostGenerateGradleAndroidProject(string basePath)
{
if (_options is null)
{
_logger.LogWarning("Android native support disabled because Sentry has not been configured. " +
"You can do that through the editor: {0}", SentryWindow.EditorMenuPath);
return;
}
if (_scriptingImplementation != ScriptingImplementation.IL2CPP)
{
if (_options is { AndroidNativeSupportEnabled: true })
{
_logger.LogWarning("Android native support requires IL2CPP scripting backend and is currently unsupported on Mono.");
}
return;
}
ModifyManifest(basePath);
var unityProjectPath = Directory.GetParent(Application.dataPath).FullName;
var gradleProjectPath = Directory.GetParent(basePath).FullName;
CopyAndroidSdkToGradleProject(unityProjectPath, gradleProjectPath);
AddAndroidSdkDependencies(gradleProjectPath);
if (_sentryCliOptions?.IgnoreCliErrors is true)
{
_logger.LogWarning("Sentry CLI errors will be ignored during build. BE AWARE you might have " +
"unminified/unsymbolicated crashes in production if the debug symbol upload fails. " +
"When using this flag, you should store built sourcemaps and debug files, to re-run the " +
"upload symbols command at a later point.");
}
SetupSymbolsUpload(unityProjectPath, gradleProjectPath);
SetupProguard(gradleProjectPath);
}
internal void ModifyManifest(string basePath)
{
var manifestPath = GetManifestPath(basePath);
if (!File.Exists(manifestPath))
{
throw new FileNotFoundException("Can't configure native Android SDK nor set auto-init:false.",
manifestPath);
}
var enableNativeSupport = true;
if (_options is null)
{
_logger.LogWarning("Android native support disabled because Sentry has not been configured. " +
"You can do that through the editor: {0}", SentryWindow.EditorMenuPath);
enableNativeSupport = false;
}
else if (!_options.IsValid())
{
_logger.LogDebug("Android native support disabled.");
enableNativeSupport = false;
}
else if (!_options.AndroidNativeSupportEnabled)
{
_logger.LogDebug("Android native support disabled through the options.");
enableNativeSupport = false;
}
var androidManifest = new AndroidManifest(manifestPath, _logger);
androidManifest.RemovePreviousConfigurations();
if (!enableNativeSupport)
{
return;
}
androidManifest.AddDisclaimerComment();
if (_options?.AndroidNativeInitializationType is NativeInitializationType.Runtime)
{
_logger.LogDebug("Setting 'auto-init' to 'false'. The Android SDK will be initialized at runtime.");
androidManifest.SetAutoInit(false);
_ = androidManifest.Save();
return;
}
_logger.LogInfo("Adding Sentry options to the AndroidManifest.");
_logger.LogDebug("Modifying AndroidManifest: {0}", basePath);
androidManifest.SetSDK("sentry.java.android.unity");
_logger.LogDebug("Setting DSN: {0}", _options!.Dsn);
androidManifest.SetDsn(_options.Dsn!);
if (_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);
androidManifest.SetRelease(_options.Release);
}
if (_options.Environment is not null)
{
_logger.LogDebug("Setting Environment: {0}", _options.Environment);
androidManifest.SetEnvironment(_options.Environment);
}
_logger.LogDebug("Setting DiagnosticLevel: {0}", _options.DiagnosticLevel);
androidManifest.SetLevel(_options.DiagnosticLevel);
if (_options.SampleRate.HasValue)
{
// To keep the logs in line with what the SDK writes to the AndroidManifest we're formatting here too
_logger.LogDebug("Setting SampleRate: {0}", ((float)_options.SampleRate).ToString("F", CultureInfo.InvariantCulture));
androidManifest.SetSampleRate(_options.SampleRate.Value);
}
// TODO: Missing on AndroidManifest
// _logger.LogDebug("Setting MaxBreadcrumbs: {0}", options.MaxBreadcrumbs);
// androidManifest.SetMaxBreadcrumbs(options.MaxBreadcrumbs);
// _logger.LogDebug("Setting MaxCacheItems: {0}", options.MaxCacheItems);
// androidManifest.SetMaxCacheItems(options.MaxCacheItems);
// _logger.LogDebug("Setting SendDefaultPii: {0}", options.SendDefaultPii);
// // androidManifest.SetSendDefaultPii(options.SendDefaultPii);
// Note: doesn't work - produces a blank (white) screenshot
// _logger.LogDebug("Setting AttachScreenshot: {0}", _options.AttachScreenshot);
// androidManifest.SetAttachScreenshot(_options.AttachScreenshot);
androidManifest.SetAttachScreenshot(false);
// Disabling the native in favor of the C# layer for now
androidManifest.SetNdkEnabled(_options.NdkIntegrationEnabled);
androidManifest.SetNdkScopeSync(_options.NdkScopeSyncEnabled);
androidManifest.SetAutoTraceIdGeneration(false);
androidManifest.SetAutoSessionTracking(false);
androidManifest.SetAutoAppLifecycleBreadcrumbs(false);
androidManifest.SetAnr(false);
androidManifest.SetPersistentScopeObserver(false);
// TODO: All SentryOptions and create specific Android options
_ = androidManifest.Save();
}
internal void CopyAndroidSdkToGradleProject(string unityProjectPath, string gradlePath)
{
var androidSdkPath = Path.Combine(unityProjectPath, "Packages", SentryPackageInfo.GetName(), "Plugins", "Android", "Sentry~");
var targetPath = Path.Combine(gradlePath, "unityLibrary", "libs");
if (_options is { Enabled: true, AndroidNativeSupportEnabled: true })
{
if (!Directory.Exists(androidSdkPath))
{
throw new DirectoryNotFoundException($"Failed to find the Android SDK at '{androidSdkPath}'.");
}
Directory.CreateDirectory(targetPath);
_logger.LogInfo("Copying the Android SDK to '{0}'.", gradlePath);
foreach (var sourceFileName in Directory.GetFiles(androidSdkPath))
{
var fileName = Path.GetFileName(sourceFileName);
var destFileName = Path.Combine(targetPath, fileName);
_logger.LogDebug("Copying '{0}' to '{1}'", fileName, destFileName);
File.Copy(sourceFileName, destFileName, overwrite: true);
}
}
else
{
_logger.LogInfo("Removing the Android SDK from the output project.");
foreach (var file in Directory.GetFiles(androidSdkPath))
{
var fileToDelete = Path.Combine(targetPath, Path.GetFileName(file));
if (File.Exists(fileToDelete))
{
File.Delete(fileToDelete);
}
}
}
}
internal void AddAndroidSdkDependencies(string gradleProjectPath)
{
var tool = new GradleSetup(_logger, gradleProjectPath);
var nativeSupportEnabled = _options is { Enabled: true, AndroidNativeSupportEnabled: true };
try
{
if (nativeSupportEnabled)
{
tool.UpdateGradleProject();
}
else
{
tool.ClearGradleProject();
}
}
catch (Exception e)
{
_logger.LogError(e, $"Failed to {(nativeSupportEnabled ? "add" : "remove")} Android Dependencies in the gradle project");
}
}
internal void SetupSymbolsUpload(string unityProjectPath, string gradleProjectPath)
{
var disableSymbolsUpload = false;
var isExporting = EditorUserBuildSettings.exportAsGoogleAndroidProject;
_logger.LogInfo("The project is exporting: '{0}'", isExporting);
var symbolsUpload = new DebugSymbolUpload(_logger, _sentryCliOptions, unityProjectPath, gradleProjectPath, isExporting, AndroidUtils.ShouldUploadMapping());
if (_options is not { Enabled: true, AndroidNativeSupportEnabled: true })
{
disableSymbolsUpload = true;
}
if (_sentryCliOptions is null)
{
_logger.LogWarning("Failed to load sentry-cli options.");
disableSymbolsUpload = true;
}
else if (!_sentryCliOptions.IsValid(_logger, _isDevelopmentBuild))
{
disableSymbolsUpload = true;
}
if (disableSymbolsUpload)
{
symbolsUpload.RemoveUploadFromGradleFile();
if (_options is { Il2CppLineNumberSupportEnabled: true })
{
_logger.LogWarning("The IL2CPP line number support requires the debug symbol upload to be enabled.");
}
return;
}
try
{
_logger.LogInfo("Adding automated debug symbols upload to the gradle project.");
// TODO this currently copies the CLI for the current platform, thus making the exported project only
// build properly on the same platform as it was exported from (Linux->Linux, Windows->Windows, etc.).
// In practice, users should be able to build the project on any platform, regardless of where Unity
// ran. In that case, we would either need to include all CLI binaries and switch in Gradle, or let
// gradle download CLI on demand (relevant code could be taken from sentry-java repo?).
var launcherDirectory = Path.Combine(gradleProjectPath, "launcher");
var sentryCliPath = SentryCli.SetupSentryCli(isExporting ? launcherDirectory : null);
SentryCli.CreateSentryProperties(launcherDirectory, _sentryCliOptions!, _options!);
symbolsUpload.TryCopySymbolsToGradleProject();
symbolsUpload.AppendUploadToGradleFile(sentryCliPath);
}
catch (Exception e)
{
_logger.LogError(e, "Failed to add the automatic symbols upload to the gradle project");
}
}
private void SetupProguard(string gradleProjectPath)
{
var tool = new ProguardSetup(_logger, gradleProjectPath);
var nativeSupportEnabled = _options is { Enabled: true, AndroidNativeSupportEnabled: true };
try
{
if (nativeSupportEnabled)
{
tool.AddToGradleProject();
}
else
{
tool.RemoveFromGradleProject();
}
}
catch (Exception e)
{
_logger.LogError(e, $"Failed to {(nativeSupportEnabled ? "add" : "remove")} Proguard rules in the gradle project");
}
}
internal static string GetManifestPath(string basePath) =>
new StringBuilder(basePath)
.Append(Path.DirectorySeparatorChar)
.Append("src")
.Append(Path.DirectorySeparatorChar)
.Append("main")
.Append(Path.DirectorySeparatorChar)
.Append("AndroidManifest.xml")
.ToString();
}
internal class AndroidXmlDocument : XmlDocument
{
private readonly string _path;
protected const string AndroidXmlNamespace = "http://schemas.android.com/apk/res/android";
protected const string AndroidNsPrefix = "android";
protected AndroidXmlDocument(string path)
{
_path = path;
using (var reader = new XmlTextReader(_path))
{
_ = reader.Read();
Load(reader);
}
var nsManager = new XmlNamespaceManager(NameTable);
nsManager.AddNamespace("android", AndroidXmlNamespace);
}
public string Save() => SaveAs(_path);
private string SaveAs(string path)
{
using var writer = new XmlTextWriter(path, new UTF8Encoding(false)) { Formatting = Formatting.Indented };
Save(writer);
return path;
}
}
internal class AndroidManifest : AndroidXmlDocument
{
private const string SentryPrefix = "io.sentry";
private const string Disclaimer = "GENERATED BY SENTRY. Changes to the Sentry options will be lost!";
private readonly XmlElement _applicationElement;
private readonly IDiagnosticLogger _logger;
public AndroidManifest(string path, IDiagnosticLogger logger) : base(path)
{
_applicationElement = (XmlElement)SelectSingleNode("/manifest/application");
_logger = logger;
}
internal void RemovePreviousConfigurations()
{
var nodesToRemove = new List<XmlNode>();
foreach (XmlNode node in _applicationElement.ChildNodes)
{
if (node.NodeType == XmlNodeType.Comment && node.Value.Equals(Disclaimer))
{
nodesToRemove.Add(node);
continue;
}
if (node.Name.Equals("meta-data"))
{
foreach (XmlAttribute attr in node.Attributes)
{
if (attr.Prefix.Equals(AndroidNsPrefix) && attr.LocalName.Equals("name") &&
attr.Value.StartsWith(SentryPrefix))
{
_logger.LogDebug("Removing AndroidManifest meta-data '{0}'", attr.Value);
nodesToRemove.Add(node);
break;
}
}
}
}
foreach (var node in nodesToRemove)
{
_ = node.ParentNode.RemoveChild(node);
}
}
public void AddDisclaimerComment() =>
_applicationElement.AppendChild(_applicationElement.OwnerDocument.CreateComment(Disclaimer));
internal void SetAutoInit(bool enableAutoInit)
=> SetMetaData($"{SentryPrefix}.auto-init", enableAutoInit.ToString());
internal void SetDsn(string dsn) => SetMetaData($"{SentryPrefix}.dsn", dsn);
internal void SetSampleRate(float sampleRate) =>
// Keeping the sample-rate as float: https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
SetMetaData($"{SentryPrefix}.sample-rate", sampleRate.ToString("F", CultureInfo.InvariantCulture));
internal void SetRelease(string release) => SetMetaData($"{SentryPrefix}.release", release);
internal void SetAttachScreenshot(bool value) => SetMetaData($"{SentryPrefix}.attach-screenshot", value.ToString());
internal void SetEnvironment(string environment) => SetMetaData($"{SentryPrefix}.environment", environment);
internal void SetSDK(string name) => SetMetaData($"{SentryPrefix}.sdk.name", name);
internal void SetAutoSessionTracking(bool enableAutoSessionTracking)
=> SetMetaData($"{SentryPrefix}.auto-session-tracking.enable", enableAutoSessionTracking.ToString());
public void SetAutoAppLifecycleBreadcrumbs(bool enableAutoAppLifeCycleBreadcrumbs)
=> SetMetaData($"{SentryPrefix}.breadcrumbs.app-lifecycle", enableAutoAppLifeCycleBreadcrumbs.ToString());
internal void SetAnr(bool enableAnr)
=> SetMetaData($"{SentryPrefix}.anr.enable", enableAnr.ToString());
internal void SetPersistentScopeObserver(bool enableScopePersistence)
=> SetMetaData($"{SentryPrefix}.enable-scope-persistence", enableScopePersistence.ToString());
internal void SetNdkEnabled(bool enableNdk)
=> SetMetaData($"{SentryPrefix}.ndk.enable", enableNdk.ToString());
internal void SetNdkScopeSync(bool enableNdkScopeSync)
=> SetMetaData($"{SentryPrefix}.ndk.scope-sync.enable", enableNdkScopeSync.ToString());
public void SetAutoTraceIdGeneration(bool enableAutoTraceIdGeneration)
=> SetMetaData($"{SentryPrefix}.traces.enable-auto-id-generation", enableAutoTraceIdGeneration.ToString());
internal void SetDebug(bool debug) => SetMetaData($"{SentryPrefix}.debug", debug ? "true" : "false");
// https://github.com/getsentry/sentry-java/blob/db4dfc92f202b1cefc48d019fdabe24d487db923/sentry/src/main/java/io/sentry/SentryLevel.java#L4-L9
internal void SetLevel(SentryLevel level) =>
SetMetaData($"{SentryPrefix}.debug.level", level switch
{
SentryLevel.Debug => "debug",
SentryLevel.Error => "error",
SentryLevel.Fatal => "fatal",
SentryLevel.Info => "info",
SentryLevel.Warning => "warning",
_ => "debug"
});
private void SetMetaData(string key, string value)
{
var element = _applicationElement.AppendChild(_applicationElement.OwnerDocument
.CreateElement("meta-data"));
_ = element.Attributes.Append(CreateAndroidAttribute("name", key));
_ = element.Attributes.Append(CreateAndroidAttribute("value", value));
}
private XmlAttribute CreateAndroidAttribute(string key, string value)
{
var attr = CreateAttribute(AndroidNsPrefix, key, AndroidXmlNamespace);
attr.Value = value;
return attr;
}
}