AndroidJNI_CUSTOM_INTERNAL_CALL_ExceptionOccurred() errors when calling Java code from C#

Hi,

I’m working on Android and trying to get my C# Unity (v5.6.1f1) code to call some simple (test) methods in a Java class I’ve written. I’ll include a stripped-down version of my .java code. We use the Gradle build system. (This is part of my current task - to upgrade to the new Gradle build system).

The problem is that I am getting an exception every time I call one of my Java methods, even though the simple methods return valid results back to my C# code.

We export a package called com.examplecompany.androidutility and the class is called AndroidUtility. This is built into a .jar file using Android Studio v2.3.2. (I used Android Studio v2.3.2 to create a bare-bones project and then added a library to this project so I can create a .jar file called AndroidUtility.jar.)

Here’s the code in AndroidUtility.java

package com.examplecompany.androidutility;

import android.os.SystemClock;
import android.util.Log;

public class AndroidUtility {
    public AndroidUtility() {
    }

    // STATIC function
    public static int getStaticInt() {
        Log.d( "test", "getStaticInt() (.java code)" );
        return 1234;
    }

    // NON-STATIC function
    public int getNonStaticInt() {
        Log.d( "test", "getNonStaticInt() (.java code)" );
        return 5678;
    }
}

I build AndroidUtility.jar (in Android Studio), then copy this .jar file to my Unity project’s /Assets/Plugins/Android folder.

In Unity I then have a normal C# function which I call, and in this function I attempt to call the functions in the Java class. I’m using AndroidJavaObject to do this.

    // create a new jo. The package is com.examplecompany.androidutility and class is called AndroidUtility
    using (AndroidJavaObject jo = new AndroidJavaObject("com.examplecompany.androidutility.AndroidUtility"))
    {
        Debug.Log("jo new-ed");

        int result = jo.Call<int>("getNonStaticInt");
        Debug.Log("Result of getNonStaticInt() = " + result );

        int result2 = jo.Call<int>("testGetInteger");
        Debug.Log("Result of testGetInteger() = " + result2 );
    }

I then make a build of the title, and put the .apk on my Android phone, and run it with the USB connected to my PC.
I can then see the TTY output in Android Studio’s ‘Android Monitor’ window (aka logcat).

The problem
So I see that Unity prints out the results of my calls to the AndroidUtility Java class, and I see the integer results printing for both the non-static and static Java methods.
But I also get a tonne of junk printing about exceptions, such as the stuff below.

Does anyone have any idea why the exceptions are happening (in Unity code by the look of it)?

Thanks in advace.

05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_NewStringUTF()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_ExceptionOccurred()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_NewStringUTF()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_ExceptionOccurred()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_CallStaticObjectMethod(00000666, f381c408
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: >            , 0000066a
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: >            , 00100005
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: >            , 00100009
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: >            , 00000001
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: >            )
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: ? getMethodID("java.lang.Class", "forName", "(Ljava/lang/String;)Ljava/lang/Object;", static)
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: ! Class Class.forName(String);
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_ExceptionOccurred()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_DeleteLocalRef()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_DeleteLocalRef()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_FromReflectedMethod()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_ExceptionOccurred()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_DeleteLocalRef()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_NewStringUTF()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_ExceptionOccurred()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_CallStaticObjectMethod(0000066a, 717000b0
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: >            , 00200005
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: >            )
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_ExceptionOccurred()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_GetObjectClass()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_ExceptionOccurred()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_NewGlobalRef()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_NewGlobalRef()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_DeleteLocalRef()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_DeleteLocalRef()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_DeleteLocalRef()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_NewGlobalRef()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_NewStringUTF()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_ExceptionOccurred()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_CallStaticObjectMethod(00000666, f381c3b8
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: >            , 0020095e
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: >            , 00000005
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: >            )
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: ? getConstructorID("com.examplecompany.androidutility.AndroidUtility", "()V")
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: ! com.examplecompany.androidutility.AndroidUtility();
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_ExceptionOccurred()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_DeleteLocalRef()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_FromReflectedMethod()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_ExceptionOccurred()
05-30 15:17:09.961 30143-30163/com.examplecompany.exampleapp D/Unity: > AndroidJNI_CUSTOM_DeleteLocalRef()
05-30 15:17:09.961 30143-30163/? D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_NewObject(0020095e, ee9e2fa8)
05-30 15:17:09.961 30143-30163/? D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_ExceptionOccurred()
05-30 15:17:09.961 30143-30163/? D/Unity: > AndroidJNI_CUSTOM_INTERNAL_CALL_NewGlobalRef()
05-30 15:17:09.961 30143-30163/? D/Unity: > AndroidJNI_CUSTOM_DeleteLocalRef()
05-30 15:17:09.961 30143-30163/? D/Unity: > AndroidJNI_CUSTOM_DeleteGlobalRef()
05-30 15:17:09.961 30143-30163/? D/Unity: > AndroidJNI_CUSTOM_DeleteGlobalRef()

Hi all,

I figured out why this was happening, it was simply a debug flag, setting AndroidJNIHelper.debug = false got rid of all the spam. I think the exceptions are the GC doing its thing.

Unity’s documentation is here : Unity - Scripting API: AndroidJNIHelper.debug

Thing is - this flag is set nowhere in our c# code, so I think some plugin must be setting it somewhere.

Thanks.

I have just discovered your thread and this problem on a project, and I can indeed say that the Facebook Unity SDK does this: facebook-sdk-for-unity/Facebook.Unity/Mobile/Android/AndroidFacebookGameObject.cs at main · facebook/facebook-sdk-for-unity · GitHub

I find this somewhat frustrating because we use JNI frequently and it is spammy enough to get in the way of useful logging.

Nice find, thanks for the link. Found the culprit :slight_smile:

Described behavior exists - tons of exceptions to log.
is it normal?
or unity BUG?