This is an internal guide to the interop path used by CoreCLR and NativeAOT Android applications. The Java VM (ART) and the managed runtime are separate environments in one process. JNI is the boundary in both directions: bound C# types call Java methods through Java.Interop, and Java calls managed methods through generated Java callable wrappers (JCWs) with registered JNI entrypoints.
All applications use the trimmable TypeMap implementation. Earlier versions of this guide described a choice between reflection/delegate-based "dynamic" registration and native LLVM-generated marshal methods. Neither describes this path. Here, the build generates managed TypeMap assemblies (including proxy and marshal code) and Java sources; the managed runtime registers their native callbacks with ART. There is no application-level native TypeMap lookup table, generated Java_... symbol for each callback, or marshal-method callback rewriting.
| Earlier guide | Trimmable TypeMap path |
|---|---|
JCW static initializer built __md_methods and called Runtime.register with an assembly-qualified type name. |
A generated JCW calls Runtime.registerNatives(Class); the generated managed proxy supplies the registrations for that Java class. |
"Dynamic" registration used connector delegates and reflection/Reflection.Emit at runtime. |
The scanner reads connector metadata to locate existing callbacks, but generated UnmanagedCallersOnly forwarders register their function pointers without making per-method delegates. |
"Marshal methods" relied on JNI Java_... native exports, native LLVM code, and a callback assembly rewriter. |
The TypeMap emitter writes managed proxy/entrypoint IL; RegisterNatives binds method name/signature pairs to those entrypoints. |
JNI symbol mangling selected the native implementation; _mm_wrapper adapted booleans and exceptions. |
The generated registration table uses NUL-terminated UTF-8 names/signatures; generated managed entrypoints perform JNI ABI conversion and exception handoff. |
| Term | Meaning |
|---|---|
| Bound managed type (MCW) | A C# binding for an existing Java class, such as Android.App.Activity. Its JniPeerMembers invokes Java methods. |
| Java callable wrapper (JCW, or ACW) | A generated Java class for a managed subclass or Java-visible implementation. Java overrides call native n_* methods, which lead back to managed code. |
| TypeMap assembly | A generated managed DLL containing Java-name/managed-type associations, proxy types, and (for JCWs) registration and JNI entrypoint code. |
| Java peer / managed peer | Two objects representing one interop instance, with separate runtimes and garbage collectors. JNI references and peer bookkeeping link their lifetimes. |
For example, an application can subclass a bound Activity:
using Android.App;
using Android.OS;
namespace MyApp
{
[Activity (Name = "my.app.MainActivity", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle? savedInstanceState)
{
base.OnCreate (savedInstanceState);
// Application work here.
}
}
}The binding for Activity.OnCreate carries the Java name onCreate and JNI signature (Landroid/os/Bundle;)V. The app override need not repeat [Register]: the scanner follows the registered base method through the override.
The corresponding JCW is roughly the following Java. This excerpt omits constructor and peer-reference code; its class name, registration call, override, and native callback follow the JCW generator:
package my.app;
public class MainActivity extends android.app.Activity
implements mono.android.IGCUserPeer
{
static {
mono.android.Runtime.registerNatives (MainActivity.class);
}
@Override
public void onCreate (android.os.Bundle p0)
{
n_OnCreate_Landroid_os_Bundle_ (p0);
}
public native void n_OnCreate_Landroid_os_Bundle_ (android.os.Bundle p0);
}The Java name is my/app/MainActivity, not the assembly-qualified C# type name. The method descriptor means one android.os.Bundle argument and a void return; the L...; denotes an object reference. The generated callback name is derived from binding metadata. OnCreate can call base.OnCreate back into Java: that is a new managed-to-Java invocation, not a second JCW registration.
GenerateTrimmableTypeMap invokes the JavaPeerScanner on the application, references, and SDK/framework inputs. The scanner reads managed assembly metadata, including Java type names, [Register] methods, overrides of registered base/interface methods, constructors, [Export] members, and manifest components. The TrimmableTypeMapGenerator validates the resulting model and feeds both the Java and managed emitters. The app's generated JCWs do not come from the old JavaCallableWrapperGenerator / MarshalMethodsClassifier pipeline.
The main outputs are:
| Output | Role |
|---|---|
typemap/java/**/*.java |
Generated JCWs; JcwJavaSourceGenerator emits class declarations, constructors, overrides, native declarations, registration, and IGCUserPeer hooks. |
typemap/_*.TypeMap.dll |
Per-input-assembly managed mapping attributes, proxy types, and JNI registration methods emitted by TypeMapAssemblyEmitter. |
typemap/_Microsoft.Android.TypeMaps.dll |
Root assembly anchoring the per-assembly TypeMaps; its generated TypeMapLoader.Initialize() wires the mapping dictionaries (RootTypeMapAssemblyGenerator). |
typemap/typemap-assemblies.txt, java-files.txt, acw-map.txt |
Lists of actual generated assemblies/Java sources and Java-to-managed class mappings for the build. |
android/src/net/dot/android/ApplicationRegistration.java, merged AndroidManifest.xml |
Generated application/instrumentation registration and manifest output. The former is built from scan results, not copied from a handwritten template. |
The app build also generates needed platform JCWs from framework inputs alongside user JCWs: for example, the build tests check for typemap/java/android/app/ActivityTracker.java and its Runtime.registerNatives call. The reference pack no longer ships prebuilt mono.android.jar or mono.android.dex. The handwritten Java runtime support instead comes from java_runtime_trimmable.jar, selected by _CollectRuntimeJarFilenames; this is not a jar of pre-generated platform JCWs. ApplicationRegistration.java is generated separately during the app build.
The .NET 12 cleanup retires the standalone jcw-gen tool and the legacy public
JCW emitter in Java.Interop.Tools.JavaCallableWrappers: its CallableWrapper*
model/writers, reader/writer options, JavaPeerStyle, method classifier, and
Cecil/XML import/export adapters. That emitter produced __md_methods,
Runtime.register, and TypeManager.Activate references whose runtime
implementations had already been removed. The assembly's shared naming,
scanning, hashing, and type-name mapping utilities remain available, as does
the supported XAJavaInterop1 C# binding generator. Application JCWs are
generated exclusively by the trimmable pipeline.
The SDK likewise ships only java_runtime_trimmable.jar and its DEX companion.
The unused java_runtime, java_runtime_fastdev, java_runtime_clr, and
java_runtime_fastdev_clr artifact pairs, Java DebugRuntime/BuildConfig
classes, and old environment placeholder are retired. Runtime.init,
createNewContext, createNewContextWithData, switchToContext,
destroyContexts, MonoPackageManager.setContext, and getDependencies
are no longer supplied. These are intentional tooling/bootstrap compatibility
removals; custom build tools and Java sources must use the retained runtime
artifact and current startup path. Runtime.initInternal, registerNatives,
GC peers, and ApplicationRegistration.Context remain live.
Each per-assembly TypeMap contains assembly-level mapping attributes: a key such as my/app/MainActivity, a proxy type, and, for a trimmable mapping, the managed target type. The root references the per-assembly maps via TypeMapAssemblyTargetAttribute<T>. The emitter writes IL and metadata, not .cs files; the C# in the sections below is an explanatory translation of emitted code, not a file that a developer should edit. See TypeMapAssemblyGenerator, TypeMapAssemblyEmitter.EmitTypeMapAttribute, and the build-pipeline guide.
For the example Activity, think of a mapping like this conceptual C#:
// Assembly metadata, emitted as IL rather than source:
[assembly: TypeMapAttribute ("my/app/MainActivity",
typeof (MainActivityProxy), typeof (MyApp.MainActivity))]
// The emitted ACW proxy derives from JavaPeerProxy<T> and implements
// IAndroidCallableWrapper. Proxy names are chosen by the emitter.There is no source-level TypeMap attribute for application code to write. The real emitted TypeMapAttribute takes the Java key and proxy reference; its trimmable three-argument form also takes the target reference. An unconditional mapping has just the key and proxy reference, whereas the target reference on a conditional mapping lets the trimming pipeline discard entries for unused managed types. ACW proxies implement IAndroidCallableWrapper to register callbacks; other proxies need not. JavaPeerProxy provides peer creation without Activator.CreateInstance.
When GenerateR8JniRemapping scans linked managed assemblies, a surviving Java TypeMapAttribute retains only the class identity needed for forward and reverse type remapping. It does not retain every method and field in that class's R8 mapping. Member remapping instead depends on surviving JNI metadata, such as registered binding methods and fields; a proxy or target without that metadata must not restore unused member mappings.
The shared TypeMap targets run _GenerateTrimmableTypeMap after CoreCompile, compile the generated Java, and feed TypeMap DLLs to later steps. They track generated files and remove stale JCWs when the managed model changes. The target's stamp and output lists matter on incremental builds: a stale compiled Java class can otherwise outlive its managed callback.
CoreCLR's runtime-specific targets give the TypeMap DLLs to ILLink as trimmable inputs, with _Microsoft.Android.TypeMaps as the entry assembly. For PublishTrimmed=true, a second scan of linked assemblies regenerates JCWs in typemap/linked-java/ and records linked-java-files.txt; the pre-trim Java set is not necessarily the packaged set. NativeAOT's targets feed the generated DLLs to ILC. Its default Java shrinker policy retains the ACW map; retained-typemap trimming explicitly opts into reading external typemap records from the final ILC objects, not dependency graphs. See the R8 pipeline for both runtimes' policies. Both modes need reachable managed mapping/proxy/entrypoint code; they do not resurrect the native LLVM TypeMap.
Android bindings are managed wrappers for existing Java APIs. For example, Activity.ReportFullyDrawn() uses its bound member descriptor "reportFullyDrawn.()V" and calls _members.InstanceMethods.InvokeVirtualVoidMethod (...) on this. The essential C# shape (simplified) is:
const string id = "reportFullyDrawn.()V";
_members.InstanceMethods.InvokeVirtualVoidMethod (id, this, null);Java.Interop resolves the class and method, invokes JNI with the peer's reference, and converts values/references according to the binding. This call enters Java, not a JCW callback directly; Java virtual dispatch can subsequently reach a managed subclass's generated JCW override and call its registered n_* method back into managed code. The two directions can therefore form a managed-to-Java-to-managed round trip. Other bound methods use JniArgumentValue arrays and the appropriate virtual, nonvirtual, or static JniPeerMembers invocation. The TypeMap is not a per-call method dispatch table: it is involved when Java classes and managed peers must be mapped or created, not in place of this invocation.
When JNI returns an object or Java passes one into managed code, TrimmableTypeMapTypeManager uses the Java name and generated mappings to select a managed target and proxy. TrimmableTypeMapValueManager looks for an existing managed peer or constructs one from the JNI handle. In the reverse direction, the type manager can obtain a JNI type signature for a managed peer type. Do not confuse a bound Activity MCW with the MainActivity JCW that implements Java-to-managed overrides.
The generated Java onCreate calls its native n_OnCreate_Landroid_os_Bundle_; constructors similarly declare generated nctor_* native callbacks, and instantiate/activate the managed peer when appropriate. The class's static block calls mono.android.Runtime.registerNatives (MainActivity.class) when the class is first initialized. This is per Java class. Application and instrumentation JCWs instead use a guarded __md_registerNatives() helper called from their generated wrappers: registration in their static initializer can be too early in startup. The declaration of registerNatives(Class) in mono.android.Runtime is handwritten Java; individual JCWs and their registration calls are generated.
Abstract JCWs retain their Java constructors and super(...) calls for
subclasses, but do not declare or call native constructor bridges. Deferred
Application/Instrumentation hierarchies also omit those bridges and their
unused managed UCO constructor registrations; managed CreateInstance
activation remains available. A deferred class with no non-constructor
callbacks needs no lazy registration helper or flag. Callback-bearing
deferred classes still use the helper, and nonempty native registration,
concrete constructor guards, GC hooks, and startup registration remain intact.
There is no concatenated __md_methods string and no Runtime.register("managed.Type, Assembly", ...) registration in this path. The old JNI native-symbol mangling rules (Java_package_Class_method...) matter for symbol-lookup-based JNI code, but generated app JCW callbacks here are associated with their Java declarations through RegisterNatives: the Java method name and signature must match; no Java_... export is required for each method.
On CoreCLR, the native host captures the VM at JNI_OnLoad, initializes the managed runtime from Runtime.initInternal, and enters JNIEnvInit.Initialize. That installs TrimmableTypeMapTypeManager and TrimmableTypeMapValueManager in AndroidRuntime, then calls TrimmableTypeMap.RegisterNativeMethods(). The old Java Runtime.register declaration and its native entrypoint have been removed; each JCW now uses its generated managed IAndroidCallableWrapper.RegisterNatives implementation.
NativeAOT has its own entry: JavaInteropRuntime.JNI_OnLoad and init initialize the native host and a JreRuntime, then call JNIEnvInit.InitializeNativeAotRuntime(). That path also registers the TrimmableTypeMap callback. NativeAOT cannot rely on CoreCLR's optional reflection-based peer construction path when a generated mapping is absent. Its generated ApplicationRegistration.registerApplications() calls Runtime.registerNatives for deferred startup types; the NativeAotRuntimeProvider invokes it after JavaInteropRuntime.init(...).
Both hosts supply a GC-bridge initializer function pointer in JnienvInitializeArgs. After publishing the JniRuntime, managed startup calls JavaMarshalGCBridge.Initialize with the startup arguments. It supplies its private ProcessBridge callback to the native initializer and passes the returned native callback to JavaMarshal.Initialize. Initialization is guarded by a lock and a process-wide flag, so the native bridge-processing thread starts exactly once, before registering native methods or running CoreCLR startup hooks.
The first native binding is for mono.android.Runtime.registerNatives itself. TrimmableTypeMap.RegisterNativeMethods() uses actual C# UTF-8 string literals for its JNI name and signature:
fixed (byte* name = "registerNatives"u8,
sig = "(Ljava/lang/Class;)V"u8) {
var method = new JniNativeMethod (name, sig, onRegisterNatives);
JniEnvironment.Types.RegisterNatives (runtimeClass.PeerReference, [method]);
}When a JCW's static block (or deferred helper) calls this Java native method, TrimmableTypeMap.OnRegisterNatives obtains the class's JNI name, finds its mapping/proxy, and calls the proxy's IAndroidCallableWrapper.RegisterNatives(JniType). This registers the class's n_* and nctor_* methods with ART. The handwritten TrimmableTypeMap does the lookup and handoff; the per-class method table is in a generated managed TypeMap proxy. The CoreCLR host does not supply a table of exported native functions for these app methods.
The TypeMapAssemblyEmitter emits sealed JavaPeerProxy<T> types for peers that need proxies. A JCW proxy also implements IAndroidCallableWrapper. For each native method it emits a JniNativeMethod containing a pointer to a name, a pointer to a JNI signature, and a callable entrypoint pointer. Its RegisterNatives method stack-allocates the entries and passes a ReadOnlySpan<JniNativeMethod> to JniEnvironment.Types.RegisterNatives. In conceptual C# (field and callback identifiers are illustrative, and the emitter writes IL):
public unsafe void RegisterNatives (JniType jniType)
{
JniNativeMethod* entries = stackalloc JniNativeMethod [1];
delegate* unmanaged<IntPtr, IntPtr, IntPtr, void> callback = &OnCreateEntry;
entries [0] = new JniNativeMethod (
&MethodNameUtf8, &MethodSignatureUtf8, (IntPtr) callback);
JniEnvironment.Types.RegisterNatives (
jniType.PeerReference,
new ReadOnlySpan<JniNativeMethod> (entries, 1));
}The PEAssemblyBuilder emits the actual name/signature fields as NUL-terminated UTF-8 bytes in RVA data. It can reuse identical signature data. A representative registration pair for the Activity example is n_OnCreate_Landroid_os_Bundle_ and (Landroid/os/Bundle;)V; these identify the native Java declaration, not the C# method's GetOnCreate...Handler connector. There is no runtime decoding of __md_methods or per-callback Java_... export lookup. The handwritten registerNatives binding above likewise supplies byte* name/signature data, via C# u8 literals.
These name/signature bytes are not Java String argument marshalling. JNI's GetStringUTFChars/NewStringUTF APIs use modified UTF-8; the emitter stores the registration identifiers/descriptors as UTF-8 with explicit NUL terminators (the ASCII names/signatures above have the same bytes in either encoding). XAPeerMembers obtains legacy threshold class names through Java.Interop's JniEnvironment.Types.GetJniTypeNameFromClass, which reads Java Class.getName() and converts it to a JNI name. This lookup deliberately does not reverse-remap the name: dispatch needs the actual runtime Java class, not its original managed binding name. Separately, JniRemappingLookup encodes method names/signatures as UTF-8 with an explicit trailing NUL for native remapping lookup. Neither path is a substitute for marshalling Java String values across JNI.
JNI passes a JNIEnv*, the instance (jobject) or class (jclass), then the declared arguments. Java object handles appear as pointer-sized parameters; the generated managed entrypoints have JNI-ABI-compatible signatures and are marked UnmanagedCallersOnly. For the example callback, the ABI parameters correspond to IntPtr env, IntPtr self, and IntPtr savedInstanceState. This does not mean the app override accepts IntPtr: its bound callback/proxy converts the Bundle reference and dispatches the virtual C# OnCreate(Bundle?).
The scanner reads [Register ("onCreate", "(Landroid/os/Bundle;)V", "GetOnCreate_Landroid_os_Bundle_Handler")] on the bound method (or its base). The third string is connector metadata, not the Java name of the native callback. For a normal registered binding, the scanner uses it to find the callback's declaring type and name (such as n_OnCreate_Landroid_os_Bundle_) and, when available, its real CLR signature. The TypeMapAssemblyEmitter emits an UnmanagedCallersOnly forwarder to that existing callback, which then calls the virtual managed method. The old path made a delegate using GetOnCreate...Handler and JNINativeWrapper.CreateDelegate; the trimmable path derives callback metadata from the connector without creating a delegate per registration.
[Export] (and methods classified as directly callable) take a different branch. The ModelBuilder marks them for a generated direct dispatcher. The ExportMethodDispatchEmitter emits the JNI entrypoint and argument conversions, calls the managed target, converts the return value, and copies back applicable array outputs; it does not invoke a connector callback. For example, a Java-visible C# method can be written as:
[Java.Interop.Export ("handleClick")]
public bool HandleClick (Android.Views.View view, int action)
{
return action != 0;
}The application build tests exercise generated [Export] methods. The Java wrapper has a native declaration with its calculated JNI signature; the generated managed dispatcher, not the developer's C# method directly, is the unmanaged entrypoint.
JNI signatures are not CLR method signatures. JNI Z is an eight-bit jboolean, C a 16-bit jchar, I a 32-bit jint, and Landroid/os/Bundle; a Java object reference. The emitted entrypoint uses JNI ABI types (a byte for a boolean); conversions such as jniValue != 0 and managedResult ? (byte) 1 : (byte) 0 bridge C# bool. For normal registered callbacks the generator uses the captured original callback signature where available: older bindings may declare bool/char while newer ones can use sbyte/ushort. It must not assume that the callback signature is identical to the Java descriptor; see the scanner's callback signature handling. No assembly rewriter is needed to insert separate _mm_wrapper methods.
The generated entrypoint handles exceptions differently depending on the callback. A normal [Register] forwarder calls the bound n_* callback in a try and sends caught exceptions directly to AndroidEnvironment.UnhandledException in Mono.Android.dll. It does not wait for GC bridge processing, because the trimmable typemap's value manager does not require that wait. EmitUcoForwarderBody does not create a BeginMarshalMethod/EndMarshalMethod frame. Modern bound callbacks can establish that JNI transition inside JniMarshal.SafeInvoke or JniMarshalTyped. Generated direct [Export] dispatchers and constructor entrypoints instead call BeginMarshalMethod, pass caught exceptions to JniRuntime.OnUserUnhandledException, and call EndMarshalMethod in finally; see ExportMethodDispatchEmitter and the generated constructor body in TypeMapAssemblyEmitter. Inspect the callback kind to find the owner of JNI transition and exception handling.
The call sequence for a Java onCreate(Bundle) is therefore:
- ART enters the generated
MainActivity.onCreate. - That method calls its registered
n_OnCreate_Landroid_os_Bundle_. - JNI invokes the generated
UnmanagedCallersOnlyentrypoint for the registered name/signature. For a bound override, it forwards to the connector-derived bound callback; for a direct export it marshals and invokes the target itself. - Peer lookup/activation and argument conversion supply the managed
MainActivityandBundle; virtual dispatch runs the app override. A call tobase.OnCreatethen goes managed-to-Java throughJniPeerMembers.
A Java-side constructor and a managed-side constructor do not allocate one shared object. Generated JCW constructors call super(...) and, when the actual Java class matches the wrapper, a generated nctor_* callback to activate the matching managed peer. This class check matters when a JCW is further subclassed. For some startup types registration is deferred as described above. The JcwJavaSourceGenerator emits the appropriate constructor shape from its scan.
On the managed side, TrimmableTypeMap uses generated mapping attributes to resolve a Java class and target type. Its JavaPeerProxy can create the managed object from a handle without late-bound Activator.CreateInstance; activation also accounts for an existing replaceable peer. TrimmableTypeMapTypeManager resolves names/types, while TrimmableTypeMapValueManager checks for registered peers and handles construction/ownership.
Proxies for sealed managed types defined in Mono.Android pass isSealed: true to the JavaPeerProxy constructor. The resulting JavaPeerProxy.IsSealed property lets target-type lookups use FindClass and IsInstanceOf without inspecting Type.Assembly at runtime. Other peers pass isSealed: false and still use the object's actual Java class hierarchy, even for sealed app and library types: these classes can use a different class loader, so resolving their class by name could incorrectly reject a valid object.
Every generated JCW implements the Java IGCUserPeer hooks monodroidAddReference and monodroidClearReferences. Managed JavaMarshalRegisteredPeers tracks add/peek/remove/finalize operations, and the GC bridge coordinates the two collectors. Local and global JNI references have different lifetimes: a managed wrapper may need a global reference to keep its peer accessible after a JNI call returns. JniHandleOwnership records whether an incoming reference is transferred. Calling Dispose() can remove the association; a raw IntPtr retained afterward is not a safe substitute for a live managed peer. For invalid reference diagnostics, see Debugging JNI Object Reference Crashes.
Start at the boundary where the failure occurs; these files distinguish build output, registration, and peer activation:
Java.Interop.TypeManager.RegisterType is not a fallback for a missing generated mapping: it throws NotSupportedException with the trimmable TypeMap. Java.Interop.TypeManager and the older Android.Runtime.TypeManager facade are obsolete and will be removed in the next release. Give the peer a discoverable Java name ([Register] for a peer type, or the component Name attribute used above) so the scanner can generate the mapping instead.
- Missing or wrong Java class: inspect the merged manifest,
typemap/java/(or CoreCLR'stypemap/linked-java/after trimming),java-files.txt/linked-java-files.txt, andacw-map.txt. Confirm the expected Java name and base/override signatures. A stale.classis not proof that a corresponding TypeMap entry survived. - Unbound native method /
UnsatisfiedLinkError: compare the generated JCW'snativemethod name and JNI descriptor with the generated managedIAndroidCallableWrapper.RegisterNativesentry for that class. Check whethermono.android.Runtime.registerNativesran for the class (or its deferred helper did), and inspectadb logcatfor the registration exception. Do not search for a missingJava_...export in this pipeline. - Wrong managed type or activation failure: check
typemap-assemblies.txt, the root_Microsoft.Android.TypeMapsassembly,TrimmableTypeMaplookup, proxy construction, andJavaMarshalRegisteredPeers. Verify whether a Java callback came from a managed-created peer or needs activation from a Java-created peer. On NativeAOT a missing generated mapping cannot be rescued by CoreCLR-only reflection. - Release/trim-only failure: inspect the linked managed assemblies and CoreCLR post-trim JCWs, or NativeAOT ILC inputs and shrinker rules. The pre-trim generated Java is not necessarily packaged Java. The build-pipeline guide explains target stamps and stale-file handling.
- JNI reference or marshal failure: use
adb logcatand the JNI reference guide for ownership and collection problems. For unsupported TypeMap settings see the build property; duplicate Java-visible constructor signatures produce XA4259.
The TypeMap generator and JCW generator implement these paths; the application build tests cover generated files and packaging. Compare the emitted names/signatures against the generated DLL and Java files before changing runtime registration.