[Build] Fix JNI remapping counts lost on incremental builds#11378
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>
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>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Run the JNI remap native-code target when the package-manager native config inputs change, and stamp target execution separately from the generated LLVM IR files. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
/review |
|
✅ Android PR Reviewer completed successfully! |
There was a problem hiding this comment.
✅ LGTM — well-crafted incremental build fix
Summary: The fix correctly identifies that _GenerateAndroidRemapNativeCode has an in-memory side effect (JniRemappingNativeCodeInfo via RegisterTaskObject) that disappears between builds, so its Outputs can't be the .ll files alone. Switching to a stamp file tied to the same input boundary as _GeneratePackageManagerJava ensures the target re-runs whenever environment generation needs the counts.
What's good:
- Root cause analysis is thorough and the PR description explains the "why" behind each design choice (stamp vs touching
.llfiles, shared input boundary) - Adding
$(_XARemapMembersFilePath)to_GeneratePackageManagerJavaInputscorrectly propagates remap XML changes to downstream targets - The regression test validates both the observable output (environment counts) and the no-unnecessary-rebuild property (
.lltimestamps) - Test follows repo patterns (
Touch+BuildwithdoNotCleanupOnUpdate/saveProject: false)
CI: dotnet-android ✅ succeeded, license/cla ✅ passed. Xamarin.Android-PR not visible in checks — may need a maintainer trigger or re-run.
One minor 💡 suggestion inline about strengthening the test assertion.
Generated by Android PR Reviewer for issue #11378 · ● 12.6M
| proj.Touch ("MainActivity.cs"); | ||
| Assert.IsTrue (b.Build (proj, doNotCleanupOnUpdate: true, saveProject: false), "second build failed"); | ||
| AssertJniRemappingCounts (proj, b, expectedTypeCount: 1, expectedMethodCount: 1); | ||
| AssertJniRemappingSourceTimestamps (remapSourceTimestamps); |
There was a problem hiding this comment.
🤖 💡 Testing — Consider asserting that _GenerateAndroidRemapNativeCode was actually skipped during the incremental build (e.g., b.Output.AssertTargetIsSkipped ("_GenerateAndroidRemapNativeCode")). This would directly verify the MSBuild target-skip behavior, making the test more diagnostic if it ever fails — you'd know whether the target ran unnecessarily vs. ran but produced wrong output.
Rule: Test assertions must be specific
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 could be skipped as up-to-date while_GeneratePackageManagerJavare-ran. In that case, the registered task object was missing, soGenerateNativeApplicationConfigSourceswrote 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
The remapping flow is:
$(_XARemapMembersFilePath)is the output of_CollectAndroidRemapMembers, but it is an input to_GenerateAndroidRemapNativeCode. Changes to that merged XML must also invalidate_GeneratePackageManagerJava, becauseGenerateNativeApplicationConfigSourcesconsumes the remap counts and writes them intoenvironment.*.ll.Make
_GenerateAndroidRemapNativeCodeuse the same incremental input boundary as_GeneratePackageManagerJava, plus the merged remap XML:The stamp file is needed because
_GenerateAndroidRemapNativeCodehas an important non-file side effect: it registersJniRemappingNativeCodeInfoin MSBuild's in-memory task-object cache. That object has no timestamp and disappears between builds.If the target used
jni_remap.*.llas its outputs, MSBuild could skip the target because those files are up-to-date, but then the in-memory counts would not be registered. That is the original bug. If we explicitly touchedjni_remap.*.ll, we would dirty native inputs and force unnecessary native recompilation.The stamp records that the remap target ran for the same inputs that can cause
environment.*.llto be regenerated. If environment generation can skip, the remap target can skip too. If environment generation must run, the stamp is stale and remap generation runs first, recreating the in-memory counts.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.obj/Release/android/jni_remap.*.lltimestamps.MainActivity.csand rebuild withdoNotCleanupOnUpdate: true/saveProject: false.1after the incremental rebuild.jni_remap.*.lltimestamps did not change when regenerated with unchanged contents.Validation command used: