Skip to content

Android: Kotlin coroutines service registration missing from APK, breaking Dispatchers.Main #24520

Closed

Description

@MartinZikmund

Current behavior 🐛

The in-repo SamplesApp Android head (Debug, net10.0-android) crashes at startup on a clean emulator:

java.lang.ExceptionInInitializerError
	at kotlinx.coroutines.Dispatchers.getMain(Dispatchers.kt:19)
	at androidx.window.layout.WindowInfoTrackerImpl.windowLayoutInfo(WindowInfoTrackerImpl.kt:66)
	at androidx.window.java.layout.WindowInfoTrackerCallbackAdapter.addWindowLayoutInfoListener(WindowInfoTrackerCallbackAdapter.kt:53)
	at crc64d0916be76e7aa092.ApplicationActivity.n_onStart(Native Method)
Caused by: java.lang.IllegalStateException: Module with the Main dispatcher is missing.
	Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android'
	at kotlinx.coroutines.internal.MainDispatcherLoader.loadMainDispatcher(MainDispatchers.kt:35)

The dependency is not missing — the packaged APK simply contains no META-INF/services entries, so the ServiceLoader lookup that resolves Dispatchers.Main finds no factory. androidx.window (reached through the Foldable add-in in ApplicationActivity.OnStart) is the first caller to touch it, so the app dies before any content renders.

Analysis:

  • The upstream artifact does ship the registration: Xamarin.KotlinX.Coroutines.Android 1.11.0.1 → lib/net10.0-android36.0/Xamarin.KotlinX.Coroutines.Android.aar → libs/*.jar contains META-INF/services/kotlinx.coroutines.internal.MainDispatcherFactory (52 bytes) and META-INF/services/kotlinx.coroutines.CoroutineExceptionHandler.
  • The classes are dexed correctly: AndroidDispatcherFactory and HandlerDispatcher are present in classes2.dex of the built APK.
  • The built APK contains exactly three META-INF entries — MANIFEST.MF, ANDROIDD.SF, ANDROIDD.RSA — and nothing else.
  • Nothing appears to ask for that exclusion: the .NET Android SDK's defaults (Sdk/AutoImport.props) only add DebugProbesKt.bin and *.kotlin* to @(AndroidPackagingOptionsExclude), and this repo sets no AndroidPackagingOptions* items at all.
  • The Debug head pins AndroidLinkTool=r8 with a proguard.cfg of -dontshrink -dontoptimize -dontobfuscate, whose comment already anticipated "the kotlinx.coroutines Main dispatcher behind androidx.window" being stripped. Classes do survive now, but the service registration does not, so this looks like jar resource collection rather than shrinking.

Expected behavior 🎯

META-INF/services/* entries from dependency jars are packaged into the APK, so ServiceLoader-based registration works and Dispatchers.Main resolves.

How to reproduce it (as minimally and precisely as possible) 🔬

This reproduces with the repository's own samples head rather than a template app:

cd src
dotnet build SamplesApp/SamplesApp/SamplesApp.csproj -f net10.0-android \
  -p:UnoTargetFrameworkOverride=net10.0-android -t:Install -p:AdbTarget="-s emulator-5554"
adb shell am start -n uno.platform.samplesapp.skia/crc6448f3b0362cbf4bc9.MainActivity

The app shows "keeps stopping"; adb logcat carries the stack above. Inspecting the APK shows the cause:

unzip -l src/SamplesApp/SamplesApp/bin/Debug/net10.0-android/uno.platform.samplesapp.skia-Signed.apk | grep META-INF

Observed on an API 34 x86_64 emulator, .NET Android SDK 36.1.69, Vulkan on SwiftShader.

Workaround 🛠️

Package the two service files as a small jar and reference it from the Android head:

<ItemGroup Condition="'$(_Platform)' == 'android'">
  <AndroidJavaLibrary Include="coroutines-services.jar" />
</ItemGroup>

where the jar holds only META-INF/services/kotlinx.coroutines.internal.MainDispatcherFactory and META-INF/services/kotlinx.coroutines.CoroutineExceptionHandler, copied verbatim from the upstream artifact. With that in place the entries appear in the APK and the app starts normally.

Renderer 🎨

  • Skia
  • Native

Affected platforms 📱💻🖥️

Android

Uno.Sdk version (and other relevant versions) 📦

Built from the repository (master at the time of writing), net10.0-android, .NET Android SDK 36.1.69, Xamarin.KotlinX.Coroutines.Android 1.11.0.1, Xamarin.AndroidX.Window.WindowJava 1.3.0.7.

IDE version 🧑‍💻

dotnet CLI build (no IDE).

Anything else we need to know? 💬

Found while validating an unrelated Skia-Android change on a local emulator: the crash blocks any on-device run of the samples app from a Debug build, so it also blocks manual validation of Android work generally.

Activity

  1. added
    triage/untriaged
    on Sep 16, 2026
  2. MartinZikmund commented on Oct 1, 2026

    @MartinZikmund
    MemberAuthor

    This looks like the same problem as #23938, which was traced to an upstream .NET for Android packaging bug: dotnet/android#11543.

    On an incremental repackage, BuildArchive can silently drop every AAR/JAR resource from the intermediate APK. That includes META-INF/services/kotlinx.coroutines.internal.MainDispatcherFactory, while the classes themselves are still dexed. It matches the symptoms here: classes present in classes2.dex, and only the signature entries under META-INF in the APK. The result alternates between good and bad on consecutive builds.

    If that is what happened here, a clean build (or deleting the intermediate APK before packaging) should produce an APK with the META-INF/services entries. The workaround from #23938 is:

    <Target Name="_CleanIntermediateApk" BeforeTargets="_BuildApkEmbed;_BuildApkFastDev">
      <Delete Files="$(ApkFileIntermediate)" Condition="Exists('$(ApkFileIntermediate)')" />
    </Target>

    dotnet/android#11543 was closed as completed on 2026-08-18. The fix may need a newer Android workload than 36.1.69, so it's worth retrying with an updated one.

  3. self-assigned this
    on Oct 8, 2026
  4. added a commit that references this issue on Oct 10, 2026
    5fd85cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

triage/untriaged

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions