Skip to content

Native AOT JobService crashes in Runtime.registerNatives when Android reuses a backup process #13568

Open

Description

@tipa

Android framework version

net11.0-android

Affected platform version

Microsoft.Android.Sdk 37.2.31 / .NET SDK 11.0.100. Also reproduced with a published 37.2.0-rc.2.84 app. ARM64, Release, PublishAot=true.

Description

The attached app schedules one periodic JobService. The job works on a normal cold start. When Android starts the process for full backup, it uses android.app.Application and skips content providers, including NativeAotRuntimeProvider. After Android unbinds the backup agent, JobScheduler can deliver the job to that same process. The generated service class calls mono.android.Runtime.registerNatives from its static initializer and throws UnsatisfiedLinkError before managed OnStartJob runs.

I would expect the job to remain scheduled and run in a process with normal application initialization. Could the SDK detect this case before the generated service calls JNI and defer the job? I reproduced it with the .NET 11 GA workload. The sample uses the default backup agent.

A related Xamarin report observed Runtime.register throwing while Android was creating a custom BackupAgent. In this sample the agent has already been unbound when Android creates the service. #2292 (comment)

I used AI to help investigate this crash and draft this report.

Steps to Reproduce

  1. Unzip jobservice-repro.zip. Use an API 33 ARM64 Google APIs userdebug emulator, a JDK, .NET 11 with the Android workload, Android SDK platform android-37.2 and build-tools 37.0.0.
  2. Set ANDROID_HOME to the SDK path. Run bash reproduce.sh emulator-5554, substituting the emulator serial. The script builds the sample and schedules job 123.
  3. The harness kills only the sample process while retaining the job. It calls ActivityManager.bindBackupAgent in full-backup mode, waits for the process, and unbinds without requesting any backup data. It then runs cmd jobscheduler run -f example.jobstartup 123.
  4. Inspect the crash buffer for the restricted PID. The service initializer throws before managed OnStartJob runs. A normal service-initiated process starts its provider and executes the same job.

Did you find any workaround?

In apps that normally use a custom Application, I registered a Java JobService as the entry point. When getApplication().getClass() == android.app.Application.class, it calls jobFinished(parameters, true) and returns true from onStartJob. In a normal process it dispatches to the managed implementation. I verified that the restricted process survived, JobScheduler retained the job with backoff, and managed work completed in a fresh normal process. This check relies on the app having a custom Application in normal launches.

Relevant log output

java.lang.UnsatisfiedLinkError: No implementation found for void mono.android.Runtime.registerNatives(java.lang.Class)
    at mono.android.Runtime.registerNatives(Native Method)
    at scrc647254390c527c84fa.PeriodicTask.<clinit>
    at java.lang.Class.newInstance
    at android.app.AppComponentFactory.instantiateService
    at android.app.ActivityThread.handleCreateService

Activity

  1. added
    bugComponent does not function as intended.
    Area: NativeAOTIssues that only occur when using NativeAOT.
    and removed
    needs-triageIssues that need to be assigned.
    on Nov 19, 2026
  2. simonrozsival commented on Nov 19, 2026

    @simonrozsival
    Member

    Thanks for the bugreport. I'm looking into it.

  3. simonrozsival commented on Nov 19, 2026

    @simonrozsival
    Member

    Thanks for the detailed report, @tipa. I was able to reproduce the crash and trace it to how Android starts the process—not to NativeAOT or the typemap.

    When Android starts an app for full backup or restore, it can use a restricted startup mode: it creates the base android.app.Application and skips installing the app’s content providers. .NET for Android normally initializes the runtime through a content provider, so in that process the runtime—and the native registration methods used by generated Java wrappers—has not been initialized.

    If Android later reuses that process for the scheduled job, loading the generated JobService wrapper fails in its static initializer, before the managed service code runs. The java.lang.Class in the exception is the parameter type to the native registration method; the class being registered is still PeriodicTask.

    I reproduced this on an Android 15/API 35 ARM64 emulator with .NET 11 RC using NativeAOT and CoreCLR with the legacy typemap. CoreCLR’s legacy wrapper calls Runtime.register; NativeAOT and the trimmable typemap call Runtime.registerNatives. Both fail when the runtime hasn’t been initialized. Your report confirms the NativeAOT case on the .NET 11 GA workload.

    The backup results depend on the kind of agent and whether the process is cold:

    • Android’s default Auto Backup succeeded in my tests; it can copy app files without starting .NET code. The issue is that Android may then reuse that restricted process for a managed component such as the job service.
    • A custom managed full-backup agent failed when Android started it in a cold restricted process. I reproduced this with NativeAOT and both CoreCLR typemaps, and also in a .NET 10 MAUI app running Mono. A warm, already-initialized process could mask the failure.
    • Custom key-value backup succeeded in my cold-process tests. So this does not mean all backup, or all custom backup agents, are broken.

    This appears to be an older limitation of the provider-based startup model, rather than a NativeAOT regression. Android’s source shows the restricted startup path skipping providers as far back as Android 5. There is also a 2018 comment on #2292 describing Runtime.register failing while Android creates a custom backup agent. That is strong evidence of the same kind of startup failure, though the old report does not include enough detail to confirm identical conditions. Other reports such as #4557 have similar service-initializer errors, but don’t establish restricted backup as their cause.

    Your Java-only JobService shim is a reasonable workaround for the reported job case: it detects the restricted process and defers the job before the generated managed wrapper is loaded. A general SDK fix would need to intercept startup before that wrapper is initialized, and account for existing application/component factories and retry behavior. I haven’t validated a general SDK fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area: App RuntimeIssues in `libmonodroid.so`.Area: NativeAOTIssues that only occur when using NativeAOT.bugComponent does not function as intended.trimmable-type-map

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions