Repository navigation
Description
Android framework version
net12.0-android (Preview)
Affected platform version
main (trimmable type map)
Description
A user Java peer can survive managed trimming without anything in managed code creating it, for example when only a static field is used. In that case ILLink/ILC remove its constructors and its type map proxy (<Type>_Proxy with RegisterNatives and the nctor_* UCOs), but the Java callable wrapper is still packaged. When Java then creates it (reflection, Java code, a library that instantiates it by name), the JCW's static initializer finds no proxy, and the constructor fails with:
No implementation found for void net.dot.orphan.OrphanPeer.nctor_0() (tried Java_net_dot_orphan_OrphanPeer_nctor_0 ...)
The managed side was trimmed correctly, so the Java class should be removed too. The app would then get a clean ClassNotFoundException instead of an unregistered native method, and a smaller DEX.
Repro (the managed type is kept only by a static field, and Java creates it by name):
[Activity (MainLauncher = true)]
public class MainActivity : Activity {
protected override void OnCreate (Bundle? b) {
base.OnCreate (b);
Android.Util.Log.Info ("Orphan", $"last: {OrphanPeer.Last}");
var type = Java.Lang.Class.ForName ("net.dot.orphan.OrphanPeer", true, ClassLoader);
type.GetConstructor ()?.NewInstance ();
}
}
[Android.Runtime.Register ("net/dot/orphan/OrphanPeer")]
public class OrphanPeer : Java.Lang.Object {
public static string? Last;
public OrphanPeer () => Last = "created";
}Results (Release, emulator API 35):
| Configuration | Java class in DEX | Class.forName (...).newInstance () |
|---|---|---|
CoreCLR TrimMode=full, D8 (default) |
yes | UnsatisfiedLinkError (no nctor_0) |
CoreCLR TrimMode=full, AndroidLinkTool=r8 (retained-typemap rules) |
no | ClassNotFoundException ✅ |
CoreCLR, r8 + AndroidR8ObfuscationMode=runtime-remapping |
yes (-keep,allowobfuscation from acw-map.txt) |
UnsatisfiedLinkError |
| NativeAOT (default) | yes (-keep from acw-map.txt) |
UnsatisfiedLinkError |
NativeAOT + runtime-remapping |
yes | UnsatisfiedLinkError |
NativeAOT + _AndroidEnableTypemapR8Trimming=true |
no | ClassNotFoundException ✅ |
So the retained-typemap R8 rules (#12827/#12829) already remove orphaned JCWs. The other paths still keep every type in acw-map.txt:
- NativeAOT: retained-key rules are opt-in because key extraction uses the NDK's
llvm-readobj([R8] Activate retained typemap rules and retire DGML generation #12829). runtime-remapping: forces_AndroidEnableTypemapR8Trimming=falsebecause it needs R8-assigned names (Xamarin.Android.Common.targets), so it falls back to keeping every ACW. It could instead use the retained typemap keys as-keep,allowobfuscationclass roots.- D8 (no R8): nothing removes Java classes. On CoreCLR, the
typemap/linked-javaJCWs are generated from the linked assemblies, so they could be limited to peers whose type map entry survived.
Related: CoreCLR + runtime-remapping also warns XA4304: ProGuard configuration file '.../proguard_project_references.cfg' was not found.
Found while adding tests/TrimmingTests cases (#13618 follow-up).
Steps to Reproduce
Build the repro above with each configuration in the table, inspect classes*.dex with dexdump, and launch the app.
Did you find any workaround?
Use AndroidLinkTool=r8 on CoreCLR, or _AndroidEnableTypemapR8Trimming=true on NativeAOT (needs the NDK). Otherwise make sure managed code creates the peer (or root it with [DynamicDependency]).
Relevant log output
E clrfulltrimming: No implementation found for void net.dot.trimmingtests.CausedException.nctor_3(java.lang.Throwable) (tried Java_net_dot_trimmingtests_CausedException_nctor_13 and ...)
Activity
[tests] Add trimming regression tests for past bugs
[tests] Add trimming regression tests for past bugs
[tests] Add trimming regression tests for past bugs
[tests] Add trimming regression tests for past bugs