Skip to content
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

[Xamarin.Android.Build.Tasks] close XAAssemblyResolvers #9531

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 13 additions & 3 deletions src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ void Run (bool useMarshalMethods)
}
JCWGenerator.EnsureAllArchitecturesAreIdentical (Log, nativeCodeGenStates);

NativeCodeGenState.Template = templateCodeGenState;
BuildEngine4.RegisterTaskObjectAssemblyLocal (ProjectSpecificTaskObjectKey (NativeCodeGenStateRegisterTaskKey), nativeCodeGenStates, RegisteredTaskObjectLifetime.Build);

NativeCodeGenState.TemplateJniAddNativeMethodRegistrationAttributePresent = templateCodeGenState.JniAddNativeMethodRegistrationAttributePresent;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonathanpeppers: My "spider-sense" is that this change is the cause of the test failure.

Before this PR, state.JniAddNativeMethodRegistrationAttributePresent is not modified until after WriteTypeMappings() is invoked:

With this change, you're setting NativeCodeGenState.TemplateJniAddNativeMethodRegistrationAttributePresent before WriteTypeMappings() has had a chance to execute, and thus before it has had a chance to set state.JniAddNativeMethodRegistrationAttributePresent. Consequently, NativeCodeGenState.TemplateJniAddNativeMethodRegistrationAttributePresent will always be false, when it needs to be true for Java.Interop-Tests to work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps all you need to do is "move this line" to be after line 262?

/* 262 */     WriteTypeMappings (state);
/* 263 */     NativeCodeGenState.TemplateJniAddNativeMethodRegistrationAttributePresent = NativeCodeGenState.TemplateJniAddNativeMethodRegistrationAttributePresent || state.JniAddNativeMethodRegistrationAttributePresent;
/* 264 */ }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving it down, fixes it. It worked before, because NativeCodeGenState.Template had a reference to the object. 👍

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.
Expand Down Expand Up @@ -270,6 +268,18 @@ void Run (bool useMarshalMethods)
IList<string> additionalProviders = MergeManifest (templateCodeGenState, MaybeGetArchAssemblies (userAssembliesPerArch, templateCodeGenState.TargetArch));
GenerateAdditionalProviderSources (templateCodeGenState, additionalProviders);

if (useMarshalMethods) {
// Save NativeCodeGenState for <GeneratePackageManagerJava/> task later
Log.LogDebugMessage($"Saving {nameof (NativeCodeGenState)} to {nameof (NativeCodeGenStateRegisterTaskKey)}");
jonathanpeppers marked this conversation as resolved.
Show resolved Hide resolved
BuildEngine4.RegisterTaskObjectAssemblyLocal (ProjectSpecificTaskObjectKey (NativeCodeGenStateRegisterTaskKey), nativeCodeGenStates, RegisteredTaskObjectLifetime.Build);
} else {
// Otherwise, dispose all XAAssemblyResolvers
Log.LogDebugMessage($"Disposing all {nameof (NativeCodeGenState)}.{nameof (NativeCodeGenState.Resolver)}");
jonathanpeppers marked this conversation as resolved.
Show resolved Hide resolved
foreach (var state in nativeCodeGenStates.Values) {
state.Resolver.Dispose ();
}
}

Dictionary<string, ITaskItem> MaybeGetArchAssemblies (Dictionary<AndroidTargetArch, Dictionary<string, ITaskItem>> dict, AndroidTargetArch arch)
{
if (!dict.TryGetValue (arch, out Dictionary<string, ITaskItem> archDict)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Xamarin.Android.Tasks;
/// </summary>
class NativeCodeGenState
{
public static NativeCodeGenState? Template { get; set; }
public static bool TemplateJniAddNativeMethodRegistrationAttributePresent { get; set; }

/// <summary>
/// Target architecture for which this instance was created.
Expand Down
Loading