Skip to content

Commit

Permalink
Bump to xamarin/Java.Interop/main@cba61370 (dotnet#5530)
Browse files Browse the repository at this point in the history
Fixes: dotnet#5400

Changes: dotnet/java-interop@a8f68e5...cba6137

  * dotnet/java-interop@cba61370: [Java.Interop] Add JniRuntime.JniValueManager.ActivatePeer() (dotnet#784)
  * dotnet/java-interop@8c7194fd: [AzDO] Switch default branch from master to main. (dotnet#786)
  * dotnet/java-interop@b0d170c1: [generator] Only apply ConstSugar to Mono.Android.dll (dotnet#780)

dotnet/java-interop@cba61370 removed the the previously required use
of System.Linq.Expressions from the internal `ManagedPeer` class,
replacing with a call to `JniRuntime.JniValueManager.ActivatePeer()`.

Override `JniRuntime.JniValueManager.ActivatePeer()` within
`Android.Runtime.AndroidRuntime`, implementing
`AndroidRuntime.ActivatePeer()` in terms of
`Java.Interop.TypeManager.Activate()`, thus avoiding the need for
System.Linq.Expressions.

This saves us ~15% in assembly size; size difference of
BuildReleaseArm64False on net6:

	Size difference in bytes ([*1] apk1 only, [*2] apk2 only):
	  +          96 assemblies/Mono.Android.dll
	  -         331 assemblies/System.Collections.Concurrent.dll
	  -         907 assemblies/Java.Interop.dll
	  -       1,003 assemblies/System.Linq.dll
	  -       3,856 assemblies/System.ObjectModel.dll *1
	  -       4,496 assemblies/System.Collections.dll *1
	  -       7,824 assemblies/System.Private.CoreLib.dll
	  -     115,284 assemblies/System.Linq.Expressions.dll *1
	Summary:
	  -     133,605 Assemblies -15.33% (of 871,776)
  • Loading branch information
radekdoulik authored Jan 29, 2021
1 parent 1ec865d commit c33f557
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion external/Java.Interop
5 changes: 5 additions & 0 deletions src/Mono.Android/Android.Runtime/AndroidRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,11 @@ internal void RemovePeer (IJavaPeerable value, IntPtr hash)
return null;
}

public override void ActivatePeer (IJavaPeerable? self, JniObjectReference reference, ConstructorInfo cinfo, object? []? argumentValues)
{
Java.Interop.TypeManager.Activate (self, reference.Handle, cinfo, argumentValues);
}

protected override bool TryUnboxPeerObject (IJavaPeerable value, [NotNullWhen (true)]out object? result)
{
var proxy = value as JavaProxyThrowable;
Expand Down
7 changes: 5 additions & 2 deletions src/Mono.Android/Android.Runtime/ConstructorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ internal static class ConstructorBuilder {
static FieldInfo Throwable_handle = typeof (Java.Lang.Throwable).GetField ("handle", BindingFlags.NonPublic | BindingFlags.Instance)!;


internal static Action <IntPtr, object? []?> CreateDelegate (Type type, ConstructorInfo cinfo, Type [] parameter_types) {
internal static Action <IntPtr, object? []?> CreateDelegate (ConstructorInfo cinfo) {
var type = cinfo.DeclaringType;
var handle = handlefld;
if (typeof (Java.Lang.Throwable).IsAssignableFrom (type)) {
handle = Throwable_handle;
Expand All @@ -38,7 +39,9 @@ internal static class ConstructorBuilder {
il.Emit (OpCodes.Stfld, handle);

il.Emit (OpCodes.Ldloc_0);
for (int i = 0; i < parameter_types.Length; i++) {

var len = cinfo.GetParameters ().Length;
for (int i = 0; i < len; i++) {
il.Emit (OpCodes.Ldarg, 1);
il.Emit (OpCodes.Ldc_I4, i);
il.Emit (OpCodes.Ldelem_Ref);
Expand Down
10 changes: 8 additions & 2 deletions src/Mono.Android/Java.Interop/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,18 @@ static void n_Activate (IntPtr jnienv, IntPtr jclass, IntPtr typename_ptr, IntPt
cinfo.Invoke (o, parms);
return;
}

Activate (o, jobject, cinfo, parms);
}

internal static void Activate (IJavaPeerable? o, IntPtr jobject, ConstructorInfo cinfo, object? []? parms)
{
try {
var activator = ConstructorBuilder.CreateDelegate (type, cinfo, ptypes);
var activator = ConstructorBuilder.CreateDelegate (cinfo);
activator (jobject, parms);
} catch (Exception e) {
var m = string.Format ("Could not activate JNI Handle 0x{0} (key_handle 0x{1}) of Java type '{2}' as managed type '{3}'.",
jobject.ToString ("x"), JNIEnv.IdentityHash! (jobject).ToString ("x"), JNIEnv.GetClassNameFromInstance (jobject), type.FullName);
jobject.ToString ("x"), JNIEnv.IdentityHash! (jobject).ToString ("x"), JNIEnv.GetClassNameFromInstance (jobject), cinfo.DeclaringType.FullName);
Logger.Log (LogLevel.Warn, "monodroid", m);
Logger.Log (LogLevel.Warn, "monodroid", CreateJavaLocationException ().ToString ());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,8 @@ public void CheckIncludedAssemblies ()
"Java.Interop.dll",
"Mono.Android.dll",
"System.Console.dll",
"System.Linq.Expressions.dll",
"System.ObjectModel.dll",
"System.Private.CoreLib.dll",
"System.Collections.Concurrent.dll",
"System.Collections.dll",
"System.Linq.dll",
"UnnamedProject.dll",
} :
Expand Down

0 comments on commit c33f557

Please sign in to comment.