Commit c4dbc85
[TrimmableTypeMap] Propagate deferred registerNatives to base classes (#11105)
## Summary
Fixes a startup crash (`UnsatisfiedLinkError: No implementation found for void mono.android.Runtime.registerNatives`) when running device tests with the trimmable typemap enabled.
## Background
Deferred `registerNatives` support was introduced across several PRs:
- **#10957** — Added `CannotRegisterInStaticConstructor` flag and set it in `JavaPeerScanner` for types with `[Application]` or `[Instrumentation]` attributes. The JCW generator skips the `static { registerNatives(...); }` block for these types and emits a lazy `__md_registerNatives()` helper instead.
- **#11037** — Extended deferred registration to manifest-rooted types: `RootManifestReferencedTypes` sets `CannotRegisterInStaticConstructor` on types matched by `<application>` or `<instrumentation>` manifest entries.
- **#11096** / **#11097** — Further scanner and JCW edge-case fixes, including deferred registration propagation for generated managed base types.
**None of these PRs propagated the flag to base classes in the Java type hierarchy.** When `NUnitInstrumentation` (listed in the manifest) correctly uses deferred registration, its base class `TestInstrumentation_1` still emits `static { registerNatives(...); }`. Since Java loads the base class `<clinit>` before the managed runtime registers the JNI native method, this crashes with `UnsatisfiedLinkError`.
## Changes
Add `PropagateDeferredRegistrationToBaseClasses()` in `TrimmableTypeMapGenerator`, called after `RootManifestReferencedTypes`. It walks each flagged peer's `BaseJavaName` chain with a simple linear scan and sets `CannotRegisterInStaticConstructor = true` on all base peers that have JCW stubs (i.e., `!DoNotGenerateAcw`). Framework MCW types are skipped.
In practice only 1–2 types need propagation (one Application, maybe one Instrumentation), each with a short base-class chain. A linear scan per ancestor is simpler and cheaper than building a `Dictionary<JavaName, List<Peer>>` lookup over all peers up front.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 8df9772 commit c4dbc85
2 files changed
Lines changed: 83 additions & 0 deletions
File tree
- src/Microsoft.Android.Sdk.TrimmableTypeMap
- tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator
Lines changed: 37 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| |||
207 | 208 | | |
208 | 209 | | |
209 | 210 | | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
210 | 247 | | |
211 | 248 | | |
212 | 249 | | |
| |||
Lines changed: 46 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
226 | 226 | | |
227 | 227 | | |
228 | 228 | | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
229 | 275 | | |
230 | 276 | | |
231 | 277 | | |
| |||
0 commit comments