diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs index bc1324a5957..7c13c195b16 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs @@ -222,9 +222,6 @@ void Run (bool useMarshalMethods) } JCWGenerator.EnsureAllArchitecturesAreIdentical (Log, nativeCodeGenStates); - NativeCodeGenState.Template = templateCodeGenState; - BuildEngine4.RegisterTaskObjectAssemblyLocal (ProjectSpecificTaskObjectKey (NativeCodeGenStateRegisterTaskKey), nativeCodeGenStates, RegisteredTaskObjectLifetime.Build); - if (useMarshalMethods) { // We need to parse the environment files supplied by the user to see if they want to use broken exception transitions. This information is needed // in order to properly generate wrapper methods in the marshal methods assembly rewriter. @@ -262,6 +259,9 @@ void Run (bool useMarshalMethods) WriteTypeMappings (state); } + // Set for use by task later + NativeCodeGenState.TemplateJniAddNativeMethodRegistrationAttributePresent = templateCodeGenState.JniAddNativeMethodRegistrationAttributePresent; + var acwMapGen = new ACWMapGenerator (Log); if (!acwMapGen.Generate (templateCodeGenState, AcwMapFile)) { Log.LogDebugMessage ("ACW map generation failed"); @@ -270,6 +270,18 @@ void Run (bool useMarshalMethods) IList additionalProviders = MergeManifest (templateCodeGenState, MaybeGetArchAssemblies (userAssembliesPerArch, templateCodeGenState.TargetArch)); GenerateAdditionalProviderSources (templateCodeGenState, additionalProviders); + if (useMarshalMethods) { + // Save NativeCodeGenState for task later + Log.LogDebugMessage ($"Saving {nameof (NativeCodeGenState)} to {nameof (NativeCodeGenStateRegisterTaskKey)}"); + BuildEngine4.RegisterTaskObjectAssemblyLocal (ProjectSpecificTaskObjectKey (NativeCodeGenStateRegisterTaskKey), nativeCodeGenStates, RegisteredTaskObjectLifetime.Build); + } else { + // Otherwise, dispose all XAAssemblyResolvers + Log.LogDebugMessage ($"Disposing all {nameof (NativeCodeGenState)}.{nameof (NativeCodeGenState.Resolver)}"); + foreach (var state in nativeCodeGenStates.Values) { + state.Resolver.Dispose (); + } + } + Dictionary MaybeGetArchAssemblies (Dictionary> dict, AndroidTargetArch arch) { if (!dict.TryGetValue (arch, out Dictionary archDict)) { diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs index 6f095c30cb2..c0f7f0432ce 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs @@ -331,7 +331,7 @@ void AddEnvironment () BrokenExceptionTransitions = environmentParser.BrokenExceptionTransitions, PackageNamingPolicy = pnp, BoundExceptionType = boundExceptionType, - JniAddNativeMethodRegistrationAttributePresent = NativeCodeGenState.Template != null ? NativeCodeGenState.Template.JniAddNativeMethodRegistrationAttributePresent : false, + JniAddNativeMethodRegistrationAttributePresent = NativeCodeGenState.TemplateJniAddNativeMethodRegistrationAttributePresent, HaveRuntimeConfigBlob = haveRuntimeConfigBlob, NumberOfAssembliesInApk = assemblyCount, BundledAssemblyNameWidth = assemblyNameWidth, @@ -395,6 +395,14 @@ void AddEnvironment () } } + if (nativeCodeGenStates is not null) { + // Dispose all XAAssemblyResolvers + Log.LogDebugMessage ($"Disposing all {nameof (NativeCodeGenState)}.{nameof (NativeCodeGenState.Resolver)}"); + foreach (var state in nativeCodeGenStates.Values) { + state.Resolver.Dispose (); + } + } + NativeCodeGenState EnsureCodeGenState (AndroidTargetArch targetArch) { if (nativeCodeGenStates == null || !nativeCodeGenStates.TryGetValue (targetArch, out NativeCodeGenState? state)) { diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/NativeCodeGenState.cs b/src/Xamarin.Android.Build.Tasks/Utilities/NativeCodeGenState.cs index 96d95391a49..5a541f2d09c 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/NativeCodeGenState.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/NativeCodeGenState.cs @@ -12,7 +12,7 @@ namespace Xamarin.Android.Tasks; /// class NativeCodeGenState { - public static NativeCodeGenState? Template { get; set; } + public static bool TemplateJniAddNativeMethodRegistrationAttributePresent { get; set; } /// /// Target architecture for which this instance was created.