dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.94k stars 533 forks source link

[Xamarin.Android.Build.Tasks] fix Debug mode and $(PublishTrimmed) #9452

Closed jonathanpeppers closed 3 weeks ago

jonathanpeppers commented 1 month ago

There is a problem if you have the combination:

We emit an XA0119 warning with this combination, as there is not a good reason to do it.

But unfortunately, the build will be completely broken as all the .NET assemblies don't make it to the .apk! I could reproduce this in a test.

The fix is that the logic in the <ProcessAssemblies/> MSBuild task:

if (PublishTrimmed && !AndroidIncludeDebugSymbols) {
    //...
    ShrunkAssemblies = shrunkAssemblies.ToArray ();
}

Has a case, where we did not fill out the ShrunkAssemblies [Output] item group:

if (!AndroidIncludeDebugSymbols) {
    // existing logic...
} else {
    ShrunkAssemblies = OutputAssemblies;
}

Going forward, we could probably refactor some of this logic to make things simpler, but this is a reasonable fix for now.

jonathanpeppers commented 1 month ago

One test has an NRE, so going to see what is happening there:

(_GenerateJavaStubs target) -> 
         /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001: System.NullReferenceException: Object reference not set to an instance of an object. [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Java.Interop.Tools.Cecil.CustomAttributeProviderRocks.GetCustomAttributes(ICustomAttributeProvider item, String attribute_fullname)+MoveNext() in /Users/builder/azdo/_work/13/s/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/CustomAttributeProviderRocks.cs:line 30 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found) [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source) [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Android.App.ApplicationAttribute.FromCustomAttributeProvider(ICustomAttributeProvider provider, TypeDefinitionCache cache) in /Users/builder/azdo/_work/13/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Mono.Android/ApplicationAttribute.Partial.cs:line 60 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Xamarin.Android.Tasks.ManifestDocument.CreateApplicationElement(XElement manifest, String applicationClass, List`1 subclasses, TypeDefinitionCache cache) in /Users/builder/azdo/_work/13/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs:line 581 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Xamarin.Android.Tasks.ManifestDocument.Merge(TaskLoggingHelper log, TypeDefinitionCache cache, List`1 subclasses, String applicationClass, Boolean embed, String bundledWearApplicationName, IEnumerable`1 mergedManifestDocuments) in /Users/builder/azdo/_work/13/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs:line 290 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Xamarin.Android.Tasks.GenerateJavaStubs.MergeManifest(NativeCodeGenState codeGenState, Dictionary`2 userAssemblies) in /Users/builder/azdo/_work/13/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs:line 354 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Xamarin.Android.Tasks.GenerateJavaStubs.Run(Boolean useMarshalMethods) in /Users/builder/azdo/_work/13/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs:line 270 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Xamarin.Android.Tasks.GenerateJavaStubs.RunTask() in /Users/builder/azdo/_work/13/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs:line 102 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/builder/azdo/_work/13/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 25 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
grendello commented 1 month ago

One test has an NRE, so going to see what is happening there:

(_GenerateJavaStubs target) -> 
         /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001: System.NullReferenceException: Object reference not set to an instance of an object. [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Java.Interop.Tools.Cecil.CustomAttributeProviderRocks.GetCustomAttributes(ICustomAttributeProvider item, String attribute_fullname)+MoveNext() in /Users/builder/azdo/_work/13/s/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/CustomAttributeProviderRocks.cs:line 30 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found) [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source) [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Android.App.ApplicationAttribute.FromCustomAttributeProvider(ICustomAttributeProvider provider, TypeDefinitionCache cache) in /Users/builder/azdo/_work/13/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Mono.Android/ApplicationAttribute.Partial.cs:line 60 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Xamarin.Android.Tasks.ManifestDocument.CreateApplicationElement(XElement manifest, String applicationClass, List`1 subclasses, TypeDefinitionCache cache) in /Users/builder/azdo/_work/13/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs:line 581 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Xamarin.Android.Tasks.ManifestDocument.Merge(TaskLoggingHelper log, TypeDefinitionCache cache, List`1 subclasses, String applicationClass, Boolean embed, String bundledWearApplicationName, IEnumerable`1 mergedManifestDocuments) in /Users/builder/azdo/_work/13/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs:line 290 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Xamarin.Android.Tasks.GenerateJavaStubs.MergeManifest(NativeCodeGenState codeGenState, Dictionary`2 userAssemblies) in /Users/builder/azdo/_work/13/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs:line 354 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Xamarin.Android.Tasks.GenerateJavaStubs.Run(Boolean useMarshalMethods) in /Users/builder/azdo/_work/13/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs:line 270 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Xamarin.Android.Tasks.GenerateJavaStubs.RunTask() in /Users/builder/azdo/_work/13/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs:line 102 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]
       /Users/runner/work/1/s/bin/Release/dotnet/packs/Microsoft.Android.Sdk.Darwin/35.99.0-ci.pr.gh9452.3/tools/Xamarin.Android.Common.targets(1488,3): error XAGJS7001:    at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/builder/azdo/_work/13/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 25 [/Users/runner/work/1/a/TestRelease/10-23_14.31.09/temp/BuildPropsBreaksConvertResourcesCasesOnSecondBuild/UnnamedProject.csproj]

You need the changes here https://github.com/dotnet/android/pull/9367/commits/49ace41b90752405d4fe55aa6a167a553ec8ee5f

Attaching the whole diff for the above, since on the next rebase the commit will change and the URL will no longer be valid:

diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/ApplicationAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/ApplicationAttribute.Partial.cs
index 4507424b7a5..b72c2ce4d9d 100644
--- a/src/Xamarin.Android.Build.Tasks/Mono.Android/ApplicationAttribute.Partial.cs
+++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/ApplicationAttribute.Partial.cs
@@ -57,6 +57,12 @@ static partial void AddManualMapping ()

        public static ApplicationAttribute FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
        {
+           // `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
+           // tests which check these situations.
+           if (provider == null) {
+               return null;
+           }
+
            CustomAttribute attr = provider.GetCustomAttributes ("Android.App.ApplicationAttribute")
                .SingleOrDefault ();
            if (attr == null)
diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/InstrumentationAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/InstrumentationAttribute.Partial.cs
index ae3cc8d31b9..ef1f5833ff2 100644
--- a/src/Xamarin.Android.Build.Tasks/Mono.Android/InstrumentationAttribute.Partial.cs
+++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/InstrumentationAttribute.Partial.cs
@@ -11,11 +11,17 @@
 namespace Android.App {

    partial class InstrumentationAttribute {
-       
+
        ICollection<string> specified;

        public static IEnumerable<InstrumentationAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
        {
+           // `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
+           // tests which check these situations.
+           if (provider == null) {
+               yield break;
+           }
+
            foreach (CustomAttribute attr in provider.GetCustomAttributes ("Android.App.InstrumentationAttribute")) {
                InstrumentationAttribute self = new InstrumentationAttribute ();
                self.specified = mapping.Load (self, attr, cache);
diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionAttribute.Partial.cs
index 1ca8b28ae39..16d32e0bf5b 100644
--- a/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionAttribute.Partial.cs
+++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionAttribute.Partial.cs
@@ -16,11 +16,17 @@
 namespace Android.App {

    partial class PermissionAttribute {
-       
+
        ICollection<string> specified;

        public static IEnumerable<PermissionAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
        {
+           // `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
+           // tests which check these situations.
+           if (provider == null) {
+               yield break;
+           }
+
            var attrs = provider.GetCustomAttributes ("Android.App.PermissionAttribute");
            foreach (var attr in attrs) {
                PermissionAttribute self = new PermissionAttribute ();
diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionGroupAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionGroupAttribute.Partial.cs
index a1e00e4b68e..1cda1eefe1b 100644
--- a/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionGroupAttribute.Partial.cs
+++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionGroupAttribute.Partial.cs
@@ -16,11 +16,17 @@
 namespace Android.App {

    partial class PermissionGroupAttribute {
-       
+
        ICollection<string> specified;

        public static IEnumerable<PermissionGroupAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
        {
+           // `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
+           // tests which check these situations.
+           if (provider == null) {
+               yield break;
+           }
+
            var attrs = provider.GetCustomAttributes ("Android.App.PermissionGroupAttribute");
            foreach (var attr in attrs) {
                PermissionGroupAttribute self = new PermissionGroupAttribute ();
diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionTreeAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionTreeAttribute.Partial.cs
index 0d388e440d3..5447164294c 100644
--- a/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionTreeAttribute.Partial.cs
+++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionTreeAttribute.Partial.cs
@@ -16,11 +16,17 @@
 namespace Android.App {

    partial class PermissionTreeAttribute {
-       
+
        ICollection<string> specified;

        public static IEnumerable<PermissionTreeAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
        {
+           // `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
+           // tests which check these situations.
+           if (provider == null) {
+               yield break;
+           }
+
            var attrs = provider.GetCustomAttributes ("Android.App.PermissionTreeAttribute");
            foreach (var attr in attrs) {
                PermissionTreeAttribute self = new PermissionTreeAttribute ();
diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/SupportsGLTextureAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/SupportsGLTextureAttribute.Partial.cs
index 41d970eb8cb..1f5cc0c65ec 100644
--- a/src/Xamarin.Android.Build.Tasks/Mono.Android/SupportsGLTextureAttribute.Partial.cs
+++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/SupportsGLTextureAttribute.Partial.cs
@@ -29,16 +29,21 @@ internal XElement ToElement (string packageName, TypeDefinitionCache cache)

        public static IEnumerable<SupportsGLTextureAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
        {
+           // `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
+           // tests which check these situations.
+           if (provider == null) {
+               yield break;
+           }
+
            var attrs = provider.GetCustomAttributes ("Android.App.SupportsGLTextureAttribute");
            foreach (var attr in attrs) {
                if (attr.HasConstructorArguments && attr.ConstructorArguments.Count == 1) {
                    SupportsGLTextureAttribute self = new SupportsGLTextureAttribute((string)attr.ConstructorArguments[0].Value);
                    self.specified = mapping.Load (self, attr, cache);
                    self.specified.Add("Name");
-                   yield return self;                  
+                   yield return self;
                }
            }
        }
    }
 }
-
diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesFeatureAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesFeatureAttribute.Partial.cs
index 58fc679741a..ca62e532f93 100644
--- a/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesFeatureAttribute.Partial.cs
+++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesFeatureAttribute.Partial.cs
@@ -35,12 +35,18 @@ internal XElement ToElement (string packageName, TypeDefinitionCache cache)

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

                UsesFeatureAttribute self = new UsesFeatureAttribute ();

-               if (attr.HasProperties) {   
+               if (attr.HasProperties) {
                    // handle the case where the user sets additional properties
                    self.specified = mapping.Load (self, attr, cache);
                    if (self.specified.Contains("GLESVersion") && self.GLESVersion==0) {
@@ -64,4 +70,3 @@ public static IEnumerable<UsesFeatureAttribute> FromCustomAttributeProvider (ICu
        }
    }
 }
-
diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesPermissionAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesPermissionAttribute.Partial.cs
index 5ecad9d4fdd..2a850fe120e 100644
--- a/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesPermissionAttribute.Partial.cs
+++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesPermissionAttribute.Partial.cs
@@ -16,6 +16,12 @@ partial class UsesPermissionAttribute {

        public static IEnumerable<UsesPermissionAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
        {
+           // `provider` might be null in situations when application configuration is broken, and it surfaces in a number of
+           // tests which check these situations.
+           if (provider == null) {
+               yield break;
+           }
+
            var attrs = provider.GetCustomAttributes ("Android.App.UsesPermissionAttribute");
            foreach (var attr in attrs) {
                UsesPermissionAttribute self;
diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs
index ebb9c77c080..046057a40bd 100644
--- a/src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs
+++ b/src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs
@@ -468,8 +468,15 @@ bool ShouldIgnoreSplitConfigs ()

        void GetRequiredTokens (string assemblyFilePath, out int android_runtime_jnienv_class_token, out int jnienv_initialize_method_token, out int jnienv_registerjninatives_method_token)
        {
-           using (var pe = new PEReader (File.OpenRead (assemblyFilePath))) {
+           if (File.Exists (assemblyFilePath)) {
+               using var pe = new PEReader (File.OpenRead (assemblyFilePath));
                GetRequiredTokens (pe.GetMetadataReader (), out android_runtime_jnienv_class_token, out jnienv_initialize_method_token, out jnienv_registerjninatives_method_token);
+           } else {
+               android_runtime_jnienv_class_token = -1;
+               jnienv_initialize_method_token = -1;
+               jnienv_registerjninatives_method_token = -1;
+               Log.LogDebugMessage ($"Assembly '{assemblyFilePath}' does not exist, unable to read required tokens from it");
+               return;
            }

            if (android_runtime_jnienv_class_token == -1 || jnienv_initialize_method_token == -1 || jnienv_registerjninatives_method_token == -1) {
diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs b/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs
index 50e1e71e3f9..4419ad33407 100644
--- a/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs
+++ b/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs
@@ -578,6 +578,10 @@ XElement CreateApplicationElement (XElement manifest, string applicationClass, L
            List<UsesConfigurationAttribute> usesConfigurationAttr = [];
            foreach (var assemblyPath in Assemblies) {
                var assembly = Resolver.GetAssembly (assemblyPath);
+               if (assembly == null) {
+                   continue;
+               }
+
                if (ApplicationAttribute.FromCustomAttributeProvider (assembly, cache) is ApplicationAttribute a) {
                    assemblyAttr.Add (a);
                }
jonathanpeppers commented 4 weeks ago

This change is still not fully correct, as it is passing the incorrect assembly paths to <GenerateJavaStubs/>:

image

The trimmer output the files in here per-RID:

image

jonathanpeppers commented 4 weeks ago

Ok, the fix was actually in C#, in the <ProcessAssemblies/> task. I updated the PR description.