Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Fatal Exception at UnityEngine.AndroidJNI:NewGlobalRef

Discussion in 'Scripting' started by vitaliano_fanatee, Oct 5, 2021.

  1. vitaliano_fanatee

    vitaliano_fanatee

    Joined:
    Oct 9, 2020
    Posts:
    37
    Hi folks,
    I'm using the AppsFlyer Unity Plugin to send some events, but every time that I'm going to send a purchase event, I'm getting this error:

    The code from the AppsFlyer Plugin below:

    Code (CSharp):
    1.         private static AndroidJavaObject convertDictionaryToJavaMap(Dictionary<string, string> dictionary)
    2.         {
    3.             AndroidJavaObject map = new AndroidJavaObject("java.util.HashMap");
    4.             IntPtr putMethod = AndroidJNIHelper.GetMethodID(map.GetRawClass(), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
    5.             jvalue[] val;
    6.             if (dictionary != null)
    7.             {
    8.                 foreach (var entry in dictionary)
    9.                 {
    10.                     val = AndroidJNIHelper.CreateJNIArgArray(new object[] { entry.Key, entry.Value });
    11.                     AndroidJNI.CallObjectMethod(map.GetRawObject(), putMethod,val);
    12.                     AndroidJNI.DeleteLocalRef(val[0].l);
    13.                     AndroidJNI.DeleteLocalRef(val[1].l);
    14.                 }
    15.             }
    16.            
    17.             return map;
    18.         }
    Thanks in advance,
    Vitaliano
     
  2. vitaliano_fanatee

    vitaliano_fanatee

    Joined:
    Oct 9, 2020
    Posts:
    37
    I found what was causing the crash, you cannot create a AndroidJavaObject outside the Unity MainThread.
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,608
    You can, but you have to Attach that thread to java first (and detach once you're done).
     
    vitaliano_fanatee likes this.