Skip to content

Commit 7acf328

Browse files
authored
Changes: dotnet/java-interop@57f7bc8...d62008d * dotnet/java-interop@d62008d1: [Java.Interop] GetSimpleReferences(): fallback for GetTypeSignatures() (dotnet/java-interop#1305) Context: 25d1f00 If you have a Debug build of .NET for Android, and you enable assembly logging: % adb shell setprop debug.mono.log default,assembly then `adb logcat` will contain messages such as: I monodroid-assembly: typemap: unable to find mapping to a Java type from managed type 'Java.Interop.ManagedPeer, Java.Interop' W monodroid: typemap: failed to map managed type to Java type: Java.Interop.ManagedPeer, Java.Interop, Version=10.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065 (Module ID W monodroid-assembly: typemap: called from W monodroid-assembly: at Android.Runtime.JNIEnv.TypemapManagedToJava(Type type) W monodroid-assembly: at Android.Runtime.AndroidTypeManager.GetSimpleReference(Type type) W monodroid-assembly: at Java.Interop.JniRuntime.JniTypeManager.GetTypeSignature(Type type) W monodroid-assembly: at Java.Interop.JniPeerMembers..ctor(String jniPeerTypeName, Type managedPeerType, Boolean checkManagedPeerType, Boolean isInterface) W monodroid-assembly: at Java.Interop.JniPeerMembers..ctor(String jniPeerTypeName, Type managedPeerType) W monodroid-assembly: at Java.Interop.ManagedPeer..cctor() W monodroid-assembly: at Java.Interop.JniRuntime..ctor(CreationOptions options) W monodroid-assembly: at Android.Runtime.AndroidRuntime..ctor(IntPtr jnienv, IntPtr vm, IntPtr classLoader, IntPtr classLoader_loadClass, Boolean jniAddNativeMethodRegistrationAttributePresent) W monodroid-assembly: at Android.Runtime.JNIEnvInit.Initialize(JnienvInitializeArgs* args) … I monodroid-assembly: typemap: unable to find mapping to a Java type from managed type 'Java.Interop.JavaObject, Java.Interop' W monodroid: typemap: failed to map managed type to Java type: Java.Interop.JavaObject, Java.Interop, Version=10.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065 (Module ID: 90427bf1-700a-4676-b23a-fb68e2e2e9da; Type token: 33554439) W monodroid-assembly: typemap: called from W monodroid-assembly: at Android.Runtime.JNIEnv.TypemapManagedToJava(Type type) W monodroid-assembly: at Android.Runtime.AndroidTypeManager.GetSimpleReference(Type type) W monodroid-assembly: at Java.Interop.JniRuntime.JniTypeManager.GetTypeSignature(Type type) W monodroid-assembly: at Java.Interop.JniPeerMembers..ctor(String jniPeerTypeName, Type managedPeerType, Boolean checkManagedPeerType, Boolean isInterface) W monodroid-assembly: at Java.Interop.JniPeerMembers..ctor(String jniPeerTypeName, Type managedPeerType) W monodroid-assembly: at Java.Interop.JavaObject..cctor() W monodroid-assembly: at Java.Interop.TypeManager.Activate(IntPtr jobject, ConstructorInfo cinfo, Object[] parms) W monodroid-assembly: at Java.Interop.TypeManager.n_Activate(IntPtr jnienv, IntPtr jclass, IntPtr typename_ptr, IntPtr signature_ptr, IntPtr jobject, IntPtr parameters_ptr) W monodroid-assembly: at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLLL_V(_JniMarshal_PPLLLL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1, IntPtr p2, IntPtr p3) `ManagedPeer` and `JavaObject` are types within `Java.Interop.dll` which uses `JniPeerMembers`, and [Debug builds of `JniPeerMembers`][0] try to verify that the typemap entry is consistent: partial class JniPeerMembers { JniPeerMembers string jniPeerTypeName, Type managedPeerType, bool checkManagedPeerType, bool isInterface = false) { // … #if DEBUG var signatureFromType = JniEnvironment.Runtime.TypeManager.GetTypeSignature (managedPeerType); if (signatureFromType.SimpleReference != jniPeerTypeName) { Debug.WriteLine ("WARNING-Java.Interop: ManagedPeerType <=> JniTypeName Mismatch! javaVM.GetJniTypeInfoForType(typeof({0})).JniTypeName=\"{1}\" != \"{2}\"", managedPeerType.FullName, signatureFromType.SimpleReference, jniPeerTypeName); Debug.WriteLine (new System.Diagnostics.StackTrace (true)); } #endif // DEBUG } } However, `Java.Interop.dll` is not considered to be a .NET for Android assembly, as it's built with e.g. `$(TargetFramework)=net9.0` and not `$(TargetFramework)=net9.0-android`. As such, it is skipped during typemap processing. Update `XAJavaTypeScanner.SpecialAssemblies` so that `Java.Interop.dll` is explicitly processed. This will ensure that there is a typemap entry for `ManagedPeer` and that the `JniPeerMembers` checks pass. This allows typemap entries for `JavaObject` and `ManagedPeer` to be created, removing the above warning messages. This also requires updating `XAJavaTypeScanner.GetJavaTypes()` so that in Debug builds, `Mono.Android.dll` is processed *before* any other assemblies, ensuring that `java/lang/Object` is typemap'd to `Java.Lang.Object, Mono.Android.dll`. See also 25d1f00, which did the same thing for *Release* builds. Add a new `ObjectTest.java_lang_Object_Is_Java_Lang_Object()` unit test which explicitly verifies the mapping for `java/lang/Object`. [0]: https://github.com/dotnet/java-interop/blob/6bc87e8b55bc00ae1423a5ae92cf5db573fc76ed/src/Java.Interop/Java.Interop/JniPeerMembers.cs#L45-L54
1 parent c7f9f5c commit 7acf328

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/BuildReleaseArm64SimpleDotNet.apkdesc

+13-13
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55
"Size": 3036
66
},
77
"classes.dex": {
8-
"Size": 22456
8+
"Size": 22484
99
},
1010
"lib/arm64-v8a/lib__Microsoft.Android.Resource.Designer.dll.so": {
1111
"Size": 18288
1212
},
1313
"lib/arm64-v8a/lib_Java.Interop.dll.so": {
14-
"Size": 86264
14+
"Size": 86688
1515
},
1616
"lib/arm64-v8a/lib_Mono.Android.dll.so": {
17-
"Size": 115072
17+
"Size": 117712
1818
},
1919
"lib/arm64-v8a/lib_Mono.Android.Runtime.dll.so": {
20-
"Size": 22432
20+
"Size": 22384
2121
},
2222
"lib/arm64-v8a/lib_System.Console.dll.so": {
23-
"Size": 24400
23+
"Size": 24392
2424
},
2525
"lib/arm64-v8a/lib_System.Linq.dll.so": {
26-
"Size": 25368
26+
"Size": 25336
2727
},
2828
"lib/arm64-v8a/lib_System.Private.CoreLib.dll.so": {
29-
"Size": 628960
29+
"Size": 628216
3030
},
3131
"lib/arm64-v8a/lib_System.Runtime.dll.so": {
3232
"Size": 20056
@@ -38,16 +38,16 @@
3838
"Size": 20024
3939
},
4040
"lib/arm64-v8a/libarc.bin.so": {
41-
"Size": 18832
41+
"Size": 18872
4242
},
4343
"lib/arm64-v8a/libmono-component-marshal-ilgen.so": {
4444
"Size": 36440
4545
},
4646
"lib/arm64-v8a/libmonodroid.so": {
47-
"Size": 1524624
47+
"Size": 1524752
4848
},
4949
"lib/arm64-v8a/libmonosgen-2.0.so": {
50-
"Size": 3101160
50+
"Size": 3101112
5151
},
5252
"lib/arm64-v8a/libSystem.Globalization.Native.so": {
5353
"Size": 71976
@@ -62,10 +62,10 @@
6262
"Size": 165000
6363
},
6464
"lib/arm64-v8a/libxamarin-app.so": {
65-
"Size": 18064
65+
"Size": 19536
6666
},
6767
"META-INF/BNDLTOOL.RSA": {
68-
"Size": 1223
68+
"Size": 1221
6969
},
7070
"META-INF/BNDLTOOL.SF": {
7171
"Size": 3266
@@ -98,5 +98,5 @@
9898
"Size": 1904
9999
}
100100
},
101-
"PackageSize": 3074581
101+
"PackageSize": 3078677
102102
}

src/Xamarin.Android.Build.Tasks/Utilities/XAJavaTypeScanner.cs

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.IO;
5-
5+
using System.Linq;
66
using Java.Interop.Tools.Cecil;
77
using Microsoft.Android.Build.Tasks;
88
using Microsoft.Build.Framework;
@@ -17,6 +17,7 @@ class XAJavaTypeScanner
1717
// Names of assemblies which don't have Mono.Android.dll references, or are framework assemblies, but which must
1818
// be scanned for Java types.
1919
static readonly HashSet<string> SpecialAssemblies = new HashSet<string> (StringComparer.OrdinalIgnoreCase) {
20+
"Java.Interop.dll",
2021
"Mono.Android.dll",
2122
"Mono.Android.Runtime.dll",
2223
};
@@ -37,11 +38,15 @@ public XAJavaTypeScanner (AndroidTargetArch targetArch, TaskLoggingHelper log, T
3738
public List<TypeDefinition> GetJavaTypes (ICollection<ITaskItem> inputAssemblies, XAAssemblyResolver resolver)
3839
{
3940
var types = new List<TypeDefinition> ();
40-
foreach (ITaskItem asmItem in inputAssemblies) {
41-
if (!ShouldScan (asmItem)) {
42-
log.LogDebugMessage ($"[{targetArch}] Skipping Java type scanning in assembly '{asmItem.ItemSpec}'");
43-
continue;
44-
}
41+
var inputItems = inputAssemblies
42+
.Where (a => ShouldScan (a))
43+
.ToList ();
44+
var monoAndroid = inputItems.FirstOrDefault (a => Path.GetFileName (a.ItemSpec) == "Mono.Android.dll");
45+
if (monoAndroid != null) {
46+
inputItems.Remove (monoAndroid);
47+
inputItems.Insert (0, monoAndroid);
48+
}
49+
foreach (ITaskItem asmItem in inputItems) {
4550
log.LogDebugMessage ($"[{targetArch}] Scanning assembly '{asmItem.ItemSpec}' for Java types");
4651

4752
AndroidTargetArch arch = MonoAndroidHelper.GetTargetArch (asmItem);

tests/Mono.Android-Tests/Java.Lang/ObjectTest.cs

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using Android.Runtime;
1111
using Android.Widget;
1212

13+
using Java.Interop;
14+
1315
using NUnit.Framework;
1416

1517
namespace Java.LangTests
@@ -83,6 +85,14 @@ public void NestedDisposeInvocations ()
8385
value.Dispose ();
8486
value.Dispose ();
8587
}
88+
89+
[Test]
90+
public void java_lang_Object_Is_Java_Lang_Object ()
91+
{
92+
var jloType = global::Java.Interop.JniEnvironment.Runtime.TypeManager.GetType (new JniTypeSignature ("java/lang/Object"));
93+
Assert.AreSame (typeof (Java.Lang.Object), jloType,
94+
$"`java/lang/Object` is typemap'd to `{jloType}`, not `Java.Lang.Object, Mono.Android`!");
95+
}
8696
}
8797

8898
/*

0 commit comments

Comments
 (0)