[Build] Fix JNI remapping counts lost on incremental builds#11378
Open
simonrozsival wants to merge 7 commits into
Open
[Build] Fix JNI remapping counts lost on incremental builds#11378simonrozsival wants to merge 7 commits into
simonrozsival wants to merge 7 commits into
Conversation
GenerateJniRemappingNativeCode registers JNI remapping counts (type and method replacement counts) as an in-memory MSBuild task object. On incremental builds where the remap target is skipped (outputs up to date) but _GeneratePackageManagerJava re-runs (e.g. due to assembly changes), GenerateNativeApplicationConfigSources finds no registered task object and writes zero counts into environment.ll. This silently disables JNI method remapping at runtime (jniRemappingInUse = false). Fix by persisting the counts to a file (jni_remapping_info.txt) alongside the generated jni_remap.ll sources. GenerateNativeApplicationConfigSources falls back to reading this file when the task object is not available. The info file is also added to both the remap targets' Outputs and the _GeneratePackageManagerJava target's Inputs, ensuring proper incremental build invalidation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Verifies that building a project with JNI remapping produces a jni_remapping_info.txt file with the correct type and method replacement counts, and that these counts match the values in the generated environment.ll. Without the fix in the previous commit, this file would not be created and the test would fail at the file existence assertion. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…sed fallback The remap targets are very fast (small XML parse + LL file write) and already use CopyIfStreamChanged, so always running them has negligible cost. This ensures the registered task object is always available for GenerateNativeApplicationConfigSources, eliminating the incremental build bug without any new files or fallback logic. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an incremental build issue in the Android MSBuild pipeline where JNI remapping metadata (replacement counts) can be missing on builds that skip the remap generation targets, resulting in environment.ll being generated with zero counts and remapping being disabled at runtime.
Changes:
- Removed
Inputs/Outputsfrom_GenerateEmptyAndroidRemapNativeCodeso it no longer gets skipped by MSBuild incremental target skipping. - Removed
Inputs/Outputsfrom_GenerateAndroidRemapNativeCodefor the same reason, ensuring the remapping task object is always registered for the current build.
Comments suppressed due to low confidence (2)
src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets:1638
- With Inputs/Outputs removed, this target will run whenever it is invoked. Because the target also unconditionally
Touches@(_AndroidRemapAssemblySource), thejni_remap.*.lltimestamps will change even whenCopyIfStreamChangedkeeps content identical, which will force_CompileNativeAssemblySources(Inputs include@(_AndroidRemapAssemblySource)) to re-run on incremental builds. Consider removing theTouchhere (now that the target is always executed for task-object registration), or otherwise avoid updating timestamps when the generated file content didn’t change (e.g., only touch when missing).
This issue also appears on line 1652 of the same file.
Condition=" '@(_AndroidRemapMembers->Count())' == '0' ">
<GenerateJniRemappingNativeCode
OutputDirectory="$(_NativeAssemblySourceDir)"
GenerateEmptyCode="True"
SupportedAbis="@(_BuildTargetAbis)"
src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets:1656
- Same as the empty-code target: now that Inputs/Outputs are removed, this target will execute whenever invoked, and the unconditional
Touchof@(_AndroidRemapAssemblySource)will updatejni_remap.*.lltimestamps even when the generated content is unchanged. This can unnecessarily invalidate_CompileNativeAssemblySourcesand cause native recompiles on incremental builds. Consider removing theTouch, or making it conditional so timestamps only change when the.llcontent actually changes or is missing.
DependsOnTargets="$(_GenerateAndroidRemapNativeCodeDependsOn)"
Condition=" '@(_AndroidRemapMembers->Count())' != '0' ">
<GenerateJniRemappingNativeCode
OutputDirectory="$(_NativeAssemblySourceDir)"
RemappingXmlFilePath="$(_XARemapMembersFilePath)"
Verify JNI remapping counts survive a C#-only incremental rebuild so the remap native code target continues to populate the build-scoped task object before environment.ll is regenerated. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Assert the observable generated environment counts instead of requiring a specific target execution detail. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Only the non-empty remapping target needs to run every build to register non-zero JNI remapping counts. The empty target may remain incremental because skipped task data still correctly results in zero generated environment counts. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix an incremental build bug where non-empty JNI remapping can be silently disabled after a C#-only incremental rebuild.
Problem
GenerateJniRemappingNativeCoderegisters JNI remapping counts as an in-memory MSBuild task object.GenerateNativeApplicationConfigSourceslater reads that task object and writes the counts intoenvironment.*.ll.For projects with
_AndroidRemapMembers, the old_GenerateAndroidRemapNativeCodetarget hadInputs/Outputs. On an incremental build where that target was skipped as up-to-date but_GeneratePackageManagerJavare-ran, the registered task object was missing.GenerateNativeApplicationConfigSourcesthen wrote zero remapping counts intoenvironment.*.ll, disabling remapping at runtime.The empty-remap target does not have this problem: when there are no remap members, zero counts are the correct generated output even if no task object is registered.
Fix
Remove
Inputs/Outputsonly from_GenerateAndroidRemapNativeCode, the non-empty remapping target. This ensures the task runs before_GeneratePackageManagerJavaand registers the non-zero counts needed byGenerateNativeApplicationConfigSources.Keep
Inputs/Outputson_GenerateEmptyAndroidRemapNativeCode, because skipped task data still correctly results in zero generated environment counts for the no-remap case.Validation / Repro
This PR includes
IncrementalBuildTest.JniRemappingCountsSurviveIncrementalBuild, which validates the observable generated output:XamarinAndroidApplicationProjectwith an_AndroidRemapMembersXML file containing one<replace-type>and one<replace-method>.obj/Release/android/environment.*.ll; bothjni_remapping_replacement_type_countandjni_remapping_replacement_method_index_entry_countare1.MainActivity.csand rebuild withdoNotCleanupOnUpdate: true/saveProject: false.1after the incremental rebuild.I verified the test fails without this fix by temporarily restoring the old
Inputs/Outputsmetadata on_GenerateAndroidRemapNativeCode. In that baseline, the incremental rebuild rewritesenvironment.*.llwith both JNI remapping counts as0, and the test fails with:Validation command used: