Skip to content

[Java.Interop.Tools.TypeNameMappings] fix ToJniName() #1198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2024

Conversation

jonathanpeppers
Copy link
Member

The following test in xamarin/xamarin-android:

[Test]
public void NewObjectArrayWithNonJavaTypeAndEmptyArray ()
{
    //empty array gives the right type
    var array = JNIEnv.NewObjectArray<Type> (new Type [0]);

Throws with:

Java.Lang.ClassNotFoundException : crc64d04135c992393d83.Type
----> Java.Lang.ClassNotFoundException : Didn't find class "crc64d04135c992393d83.Type" on path: DexPathList[[zip file "/data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/base.apk", zip file "/data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/split_config.x86_64.apk", zip file "/data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/split_config.xxhdpi.apk"],nativeLibraryDirectories=[/data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/lib/x86_64, /data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/base.apk!/lib/x86_64, /data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/split_config.x86_64.apk!/lib/x86_64, /data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/split_config.xxhdpi.apk!/lib/x86_64, /system/lib64, /system/product/lib64]]

It turns out, this type is completely wrong? It should be looking for java/lang/Object instead?

In 56b7eeb, we lost an important detail from the expression:

if (!type.GetInterfaces ().Any (t => t.FullName == "Android.Runtime.IJavaObject"))

We are missing the ! !!!

I was able to reproduce this behavior in a test:

[Test]
[TestCase (typeof (System.Type), "java/lang/Object")]
public void ToJniName (Type type, string expected)
{
    string actual = JavaNativeTypeManager.ToJniName (type);
    Assert.AreEqual (expected, actual);
}

Where ToJniName() was returning crc64d04135c992393d83/Type instead of java/lang/Object! After fixing the problem, the test passes.

The following test in xamarin/xamarin-android:

    [Test]
    public void NewObjectArrayWithNonJavaTypeAndEmptyArray ()
    {
        //empty array gives the right type
        var array = JNIEnv.NewObjectArray<Type> (new Type [0]);

Throws with:

    Java.Lang.ClassNotFoundException : crc64d04135c992393d83.Type
    ----> Java.Lang.ClassNotFoundException : Didn't find class "crc64d04135c992393d83.Type" on path: DexPathList[[zip file "/data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/base.apk", zip file "/data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/split_config.x86_64.apk", zip file "/data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/split_config.xxhdpi.apk"],nativeLibraryDirectories=[/data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/lib/x86_64, /data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/base.apk!/lib/x86_64, /data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/split_config.x86_64.apk!/lib/x86_64, /data/app/Mono.Android.NET_Tests-VrfrXDHT2r32zDG95kFOiw==/split_config.xxhdpi.apk!/lib/x86_64, /system/lib64, /system/product/lib64]]

It turns out, this type is completely wrong? It should be looking for
`java/lang/Object` instead?

In 56b7eeb, we lost an important detail from the expression:

    if (!type.GetInterfaces ().Any (t => t.FullName == "Android.Runtime.IJavaObject"))

We are missing the `!` !!!

I was able to reproduce this behavior in a test:

    [Test]
    [TestCase (typeof (System.Type), "java/lang/Object")]
    public void ToJniName (Type type, string expected)
    {
        string actual = JavaNativeTypeManager.ToJniName (type);
        Assert.AreEqual (expected, actual);
    }

Where `ToJniName()` was returning `crc64d04135c992393d83/Type` instead
of `java/lang/Object`! After fixing the problem, the test passes.
@jonathanpeppers
Copy link
Member Author

I updated dotnet/android#8751, to test this.

@jonpryor jonpryor merged commit bd793f1 into main Feb 24, 2024
@jonpryor jonpryor deleted the FixJavaNativeTypeManager branch February 24, 2024 00:29
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants