Skip to content

Commit c1b144f

Browse files
committed
Don't assume assemblies exist
1 parent 8840f9b commit c1b144f

10 files changed

+66
-9
lines changed

src/Xamarin.Android.Build.Tasks/Mono.Android/ApplicationAttribute.Partial.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ static partial void AddManualMapping ()
5757

5858
public static ApplicationAttribute FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
5959
{
60+
// `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
61+
// tests which check these situations.
62+
if (provider == null) {
63+
return null;
64+
}
65+
6066
CustomAttribute attr = provider.GetCustomAttributes ("Android.App.ApplicationAttribute")
6167
.SingleOrDefault ();
6268
if (attr == null)

src/Xamarin.Android.Build.Tasks/Mono.Android/InstrumentationAttribute.Partial.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111
namespace Android.App {
1212

1313
partial class InstrumentationAttribute {
14-
14+
1515
ICollection<string> specified;
1616

1717
public static IEnumerable<InstrumentationAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
1818
{
19+
// `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
20+
// tests which check these situations.
21+
if (provider == null) {
22+
yield break;
23+
}
24+
1925
foreach (CustomAttribute attr in provider.GetCustomAttributes ("Android.App.InstrumentationAttribute")) {
2026
InstrumentationAttribute self = new InstrumentationAttribute ();
2127
self.specified = mapping.Load (self, attr, cache);

src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionAttribute.Partial.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
namespace Android.App {
1717

1818
partial class PermissionAttribute {
19-
19+
2020
ICollection<string> specified;
2121

2222
public static IEnumerable<PermissionAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
2323
{
24+
// `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
25+
// tests which check these situations.
26+
if (provider == null) {
27+
yield break;
28+
}
29+
2430
var attrs = provider.GetCustomAttributes ("Android.App.PermissionAttribute");
2531
foreach (var attr in attrs) {
2632
PermissionAttribute self = new PermissionAttribute ();

src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionGroupAttribute.Partial.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
namespace Android.App {
1717

1818
partial class PermissionGroupAttribute {
19-
19+
2020
ICollection<string> specified;
2121

2222
public static IEnumerable<PermissionGroupAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
2323
{
24+
// `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
25+
// tests which check these situations.
26+
if (provider == null) {
27+
yield break;
28+
}
29+
2430
var attrs = provider.GetCustomAttributes ("Android.App.PermissionGroupAttribute");
2531
foreach (var attr in attrs) {
2632
PermissionGroupAttribute self = new PermissionGroupAttribute ();

src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionTreeAttribute.Partial.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
namespace Android.App {
1717

1818
partial class PermissionTreeAttribute {
19-
19+
2020
ICollection<string> specified;
2121

2222
public static IEnumerable<PermissionTreeAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
2323
{
24+
// `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
25+
// tests which check these situations.
26+
if (provider == null) {
27+
yield break;
28+
}
29+
2430
var attrs = provider.GetCustomAttributes ("Android.App.PermissionTreeAttribute");
2531
foreach (var attr in attrs) {
2632
PermissionTreeAttribute self = new PermissionTreeAttribute ();

src/Xamarin.Android.Build.Tasks/Mono.Android/SupportsGLTextureAttribute.Partial.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,21 @@ internal XElement ToElement (string packageName, TypeDefinitionCache cache)
2929

3030
public static IEnumerable<SupportsGLTextureAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
3131
{
32+
// `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
33+
// tests which check these situations.
34+
if (provider == null) {
35+
yield break;
36+
}
37+
3238
var attrs = provider.GetCustomAttributes ("Android.App.SupportsGLTextureAttribute");
3339
foreach (var attr in attrs) {
3440
if (attr.HasConstructorArguments && attr.ConstructorArguments.Count == 1) {
3541
SupportsGLTextureAttribute self = new SupportsGLTextureAttribute((string)attr.ConstructorArguments[0].Value);
3642
self.specified = mapping.Load (self, attr, cache);
3743
self.specified.Add("Name");
38-
yield return self;
44+
yield return self;
3945
}
4046
}
4147
}
4248
}
4349
}
44-

src/Xamarin.Android.Build.Tasks/Mono.Android/UsesFeatureAttribute.Partial.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,18 @@ internal XElement ToElement (string packageName, TypeDefinitionCache cache)
3535

3636
public static IEnumerable<UsesFeatureAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
3737
{
38+
// `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
39+
// tests which check these situations.
40+
if (provider == null) {
41+
yield break;
42+
}
43+
3844
var attrs = provider.GetCustomAttributes ("Android.App.UsesFeatureAttribute");
3945
foreach (var attr in attrs) {
4046

4147
UsesFeatureAttribute self = new UsesFeatureAttribute ();
4248

43-
if (attr.HasProperties) {
49+
if (attr.HasProperties) {
4450
// handle the case where the user sets additional properties
4551
self.specified = mapping.Load (self, attr, cache);
4652
if (self.specified.Contains("GLESVersion") && self.GLESVersion==0) {
@@ -64,4 +70,3 @@ public static IEnumerable<UsesFeatureAttribute> FromCustomAttributeProvider (ICu
6470
}
6571
}
6672
}
67-

src/Xamarin.Android.Build.Tasks/Mono.Android/UsesPermissionAttribute.Partial.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ partial class UsesPermissionAttribute {
1616

1717
public static IEnumerable<UsesPermissionAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
1818
{
19+
// `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
20+
// tests which check these situations.
21+
if (provider == null) {
22+
yield break;
23+
}
24+
1925
var attrs = provider.GetCustomAttributes ("Android.App.UsesPermissionAttribute");
2026
foreach (var attr in attrs) {
2127
UsesPermissionAttribute self;

src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,15 @@ bool ShouldIgnoreSplitConfigs ()
468468

469469
void GetRequiredTokens (string assemblyFilePath, out int android_runtime_jnienv_class_token, out int jnienv_initialize_method_token, out int jnienv_registerjninatives_method_token)
470470
{
471-
using (var pe = new PEReader (File.OpenRead (assemblyFilePath))) {
471+
if (File.Exists (assemblyFilePath)) {
472+
using var pe = new PEReader (File.OpenRead (assemblyFilePath));
472473
GetRequiredTokens (pe.GetMetadataReader (), out android_runtime_jnienv_class_token, out jnienv_initialize_method_token, out jnienv_registerjninatives_method_token);
474+
} else {
475+
android_runtime_jnienv_class_token = -1;
476+
jnienv_initialize_method_token = -1;
477+
jnienv_registerjninatives_method_token = -1;
478+
Log.LogDebugMessage ($"Assembly '{assemblyFilePath}' does not exist, unable to read required tokens from it");
479+
return;
473480
}
474481

475482
if (android_runtime_jnienv_class_token == -1 || jnienv_initialize_method_token == -1 || jnienv_registerjninatives_method_token == -1) {

src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,10 @@ XElement CreateApplicationElement (XElement manifest, string applicationClass, L
578578
List<UsesConfigurationAttribute> usesConfigurationAttr = [];
579579
foreach (var assemblyPath in Assemblies) {
580580
var assembly = Resolver.GetAssembly (assemblyPath);
581+
if (assembly == null) {
582+
continue;
583+
}
584+
581585
if (ApplicationAttribute.FromCustomAttributeProvider (assembly, cache) is ApplicationAttribute a) {
582586
assemblyAttr.Add (a);
583587
}

0 commit comments

Comments
 (0)