Skip to content

fix(android): Keep Java resources in incremental APKs - #25123

Merged
MartinZikmund merged 3 commits into
masterfrom
dev/mazi/android-meta-inf-services
Oct 10, 2026
Merged

MartinZikmund merged 3 commits into
masterfrom
dev/mazi/android-meta-inf-services

Conversation

@MartinZikmund

Copy link
Copy Markdown
Member

GitHub Issue: closes #24520, closes #23938

PR Type:

🐞 Bugfix

What changed? 🚀

Problem

On .NET 10 Android, every other incremental build produces an APK with no Java resources from AAR/JAR dependencies: no META-INF/services/*. The classes are still dexed, but ServiceLoader finds nothing. Any app that calls kotlinx.coroutines Dispatchers.Main then dies at launch:

java.lang.IllegalStateException: Module with the Main dispatcher is missing.
	at kotlinx.coroutines.internal.MainDispatcherLoader.loadMainDispatcher(MainDispatchers.kt:35)
	at androidx.window.layout.WindowInfoTrackerImpl.windowLayoutInfo(...)

In Uno Platform apps this is reached through the Foldable add-in → androidx.window WindowInfoTrackerCallbackAdapter. The in-repo SamplesApp Android head crashed this way on local Debug builds.

Root cause

This is a .NET for Android SDK bug, dotnet/android#11543. Uno Platform doesn't cause it:

  • When a previous intermediate APK exists, BuildArchive adds all of its entries to an existingEntries list.
  • For each JAR entry already in that APK, it logs "the same file already exists in the apk" and continues without removing the entry from existingEntries.
  • The cleanup step then deletes those entries as "no longer required".
  • Result: the APK alternates between good and bad on each repackage (a plain dotnet new android app alternates between 2 and 0 services files the same way). Clean builds, which CI always does, are always good. That's why this only hurt local development.

It's fixed upstream in dotnet/android#11552 (main) and #12463 (release/11.0.1xx-rc1), but not backported to 10.0 (current workload 36.1.69).

Fix

New src/Uno.Sdk/targets/android-workarounds.targets, imported from Uno.Common.Android.targets, so every Uno.Sdk Android app gets it, SamplesApp included. It follows the existing xamarin-ios-workarounds.targets pattern:

  • Two targets run BeforeTargets="_BuildApkFastDev" / "_BuildApkEmbed" and delete the previous intermediate archive ($(ApkFileIntermediate) / $(_BaseZipIntermediate)). BuildArchive then builds from scratch instead of taking the buggy refresh path.
  • They reuse the SDK's own Inputs/Outputs ($(_BuildApkFastDevStaticInputs), $(_BuildApkEmbedInputs) → $(_BuildApkEmbedOutputs)), so an up-to-date package is left alone. They only run when the SDK is about to repackage anyway.
  • They apply only to TargetFrameworkVersion < 11.0, because .NET 11 has the upstream fix.
  • They can be turned off with UnoDisableIncrementalApkWorkaround=true.

The targets depend on private SDK names (_BuildApkFastDevStaticInputs, _BuildApkEmbedInputs/Outputs, _BaseZipIntermediate, ApkFileIntermediate). This workaround should be removed once dotnet/android#11552 is backported to 10.0, or when 10.0 Android support is dropped. Apps that don't use Uno.Sdk aren't covered; they can copy the upstream workaround target.

Validation

Runtime: SamplesApp Android head, Debug net10.0-android, Pixel 7 API 35 emulator, deployed with -t:Install, Main.Android.cs unmodified

  • Before (origin/master 0e281d88ddf): a clean build has both services files; the incremental rebuild has 0, and the app crashes at launch with the stack above.
  • After: three consecutive incremental builds each keep META-INF/services/kotlinx.coroutines.internal.MainDispatcherFactory and …CoroutineExceptionHandler. sample=Buttons/Button_Events launches and renders, with no MainDispatcher error in logcat.
  • The fix with -p:UnoDisableIncrementalApkWorkaround=true reproduces the crash, which confirms the targets are what fixes it.

Runtime: throwaway apps with the targets imported

  • A plain Android app in Debug keeps the services files across resource-touch builds, and no-change builds leave the APK untouched.
  • The Release AAB base.zip alternates 61/0 → 78/2 entries without the workaround and stays at 78/2 with it.
  • Release + R8: AndroidDispatcherFactory is kept when Dispatchers.Main is reachable, and the app logs "Main dispatcher: Dispatchers.Main".

Not covered: in SamplesApp, packaged_resources is always newer, so the up-to-date skip shows only in the throwaway app and the unit test. A full SamplesApp Release Android build was not run.

Test: Given_AndroidIncrementalApkWorkaround in Uno.UI.Tasks.Tests, 6 cases. It imports the real Uno.Common.Android.targets next to stubs of the SDK targets with the same names, conditions and Inputs/Outputs. It checks that the archive is deleted only when the package is stale, kept when it is up to date, skipped on net11.0, and skipped with the opt-out. Reverting the fix makes the two stale-package cases fail.

The regression guard is an MSBuild-level test, not a runtime test. CI always does clean Android builds, which never hit the bug, so a runtime Dispatchers.Main check or an APK-content check couldn't fail before the fix.

PR Checklist ✅

🤖 Generated with Claude Code

https://claude.ai/code/session_01DZqARsYT34zwsK3y97Vr4G

MartinZikmund and others added 2 commits October 8, 2026 16:53
On net10.0-android, an incremental repackage makes BuildArchive drop
every Java resource of AAR/JAR dependencies (dotnet/android#11543, fixed
only in the .NET 11 workload). Losing META-INF/services breaks
kotlinx.coroutines Dispatchers.Main, crashing apps that use androidx.window
(e.g. via Uno.UI.Foldable) on every other build.

Uno.Sdk now deletes the stale intermediate archive right before it gets
repackaged, so up-to-date packages stay untouched. Opt out with
UnoDisableIncrementalApkWorkaround=true.

Fixes #24520

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01DZqARsYT34zwsK3y97Vr4G
Drives Uno.Common.Android.targets against stubs of the .NET Android
packaging targets: a stale package must start without the previous
archive, while an up-to-date one, net11.0 and the opt-out keep it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01DZqARsYT34zwsK3y97Vr4G
Copilot AI balanced review requested due to automatic review settings October 8, 2026 15:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added the area/code-generation Categorizes an issue or PR as relevant to code generation label Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 22s —— View job


Review of #25123: Keep Java resources in incremental APKs

Verdict: ship. I found no blocking issues and posted no inline comments.

I reviewed the diff by inspection only. I did not build or run the tests.

What I checked

  • Target ordering. _UnoDeleteStaleIntermediateApk* run via BeforeTargets after the SDK target's DependsOnTargets. _BuildApkEmbedOutputs is therefore already populated when the Inputs/Outputs check runs, and the test's _PrepareBuildApk stub models the same ordering.
  • Scope. The workaround is limited to TargetFrameworkVersion < 11.0 and has an opt-out, UnoDisableIncrementalApkWorkaround.
  • Import. It is imported once from Uno.Common.Android.targets, and the existing TargetFile glob in Uno.Sdk.csproj (targets\**\*.targets) packs the new file.
  • Test. Given_AndroidIncrementalApkWorkaround covers the stale, up-to-date, net11 and opt-out cases. It is not skipped or ignored.

Non-blocking notes

  • The targets depend on private SDK names (_BuildApkFastDevStaticInputs, _BuildApkEmbedInputs/Outputs, _BaseZipIntermediate). The test stubs those names rather than the real SDK targets, so it can't detect an SDK rename. If the names change in a 10.0 servicing update, the workaround would silently become a no-op or delete every time. The removal note in android-workarounds.targets helps. A CI check against the real workload would be better, but that is probably out of scope.
  • _UnoDeleteStaleIntermediateApkFastDev uses $(_BuildApkEmbedOutputs) as its Outputs, as the description says the SDK does. I couldn't confirm that against the SDK here, so double-check it when you bump the workload.
  • The test shells out to dotnet msbuild six times. That is acceptable, but it adds some test time.

Copilot AI balanced review requested due to automatic review settings October 9, 2026 20:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 13s —— View job


Re-review of #25123 after latest push

Verdict: ship. No new findings and no inline comments.

I reviewed by inspection only. I did not build or run the tests.

  • CodeQL Path.Combine alerts: the latest commit (test: Use Path.Join in incremental APK test) switches all four Path.Combine calls in Given_AndroidIncrementalApkWorkaround.cs to Path.Join (lines 52, 56, 85, 86). This addresses the github-code-quality comments.
  • Targets: android-workarounds.targets is unchanged from the earlier review. The Inputs/Outputs guards, the < 11.0 condition and the opt-out are all as described.
  • Carried-over note: the targets and their test stub private SDK names. Re-check _BuildApkEmbedOutputs, _BuildApkFastDevStaticInputs and _BaseZipIntermediate against the real workload on each bump, and remove the workaround once Fix BuildArchive updates for existing jar entries dotnet/android#11552 is backported to 10.0.

No specific request came with the @claude mention, so I only re-reviewed. Tell me if you want something else, such as a change or a deeper check.
· Branch: dev/mazi/android-meta-inf-services

@MartinZikmund

Copy link
Copy Markdown
Member Author

Thanks for the review. On the non-blocking notes:

  • _BuildApkFastDev Outputs: confirmed against workload 36.1.69 (Xamarin.Android.Common.targets). _BuildApkFastDev declares Inputs="$(_BuildApkFastDevStaticInputs)" Outputs="$(_BuildApkEmbedOutputs)", and _BuildApkEmbed uses $(_BuildApkEmbedInputs) / $(_BuildApkEmbedOutputs), so the workaround targets mirror them exactly.
  • Private SDK names: agreed, the stub test can't catch an SDK rename. That risk is accepted because the workaround only applies to 10.0 and is meant to be removed once Fix BuildArchive updates for existing jar entries dotnet/android#11552 is backported. The removal note in android-workarounds.targets tracks that.
  • Test time: the six dotnet msbuild invocations are acceptable for now.

Separately, the CodeQL Path.Combine notes are addressed in 99e1cb4.

@unodevops

Copy link
Copy Markdown
Contributor

@MartinZikmund
MartinZikmund merged commit d2ea69f into master Oct 10, 2026
54 of 56 checks passed
@MartinZikmund
MartinZikmund deleted the dev/mazi/android-meta-inf-services branch October 10, 2026 05:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/code-generation Categorizes an issue or PR as relevant to code generation

Projects

None yet

3 participants