Rather long post follows. It's fairly complex, needs someone who has worked with Unity Plugins before (creating them, not using them) and I'm not looking to get downvoted because a few common search terms show up. So, please read the entire post.
So, I've been having a rather weird issue happening when I try to call native functions through Unity's AndroidJavaObject
I've built native plugins in the past before, I'm trying to remove my dependancies on having to update multiple JARs each time I need to update my plugins. So, I'm moving a lot of my native Java code from plugins, into Unity itself.
Specifically, there's a way to call native Android functions (i.e. system functions) straight from C#, rather than creating a plugin and calling a function in the plugin that invokes the native function.
For example, earlier, if I had to create a Toast, I'd have to write some native code in Java, export the plugin and then call the function. These days, one can just call the Toast.makeText straight from C# as follows.
Here's how I make the call for a Toast.
public static void Toast (string text, int duration) {
#if UNITY_ANDROID && !UNITY_EDITOR
CreateActivity();
activity.Call("runOnUiThread", new AndroidJavaRunnable ( () => {
new AndroidJavaObject("android.widget.Toast", activity)
.CallStatic<AndroidJavaObject>("makeText", activity, text, duration)
.Call("show");
}) );
#endif
}
and here's the CreateActivity method
private static void CreateActivity () {
#if UNITY_ANDROID && !UNITY_EDITOR
if(activity == null)
activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").
GetStatic<AndroidJavaObject>("currentActivity");
#endif
}
This works perfectly. However, when I try the same approach for creating an AlertDialog, I get an androidjavaexception java.lang.classnotfoundexception
Here's the relevant code
public static void ShowAlert (string title, string message, string buttonText) {
#if UNITY_ANDROID && !UNITY_EDITOR
CreateActivity();
activity.Call("runOnUiThread", new AndroidJavaRunnable ( () => {
var builder = new AndroidJavaObject("android.app.AlertDialog.Builder", activity);
***//Exception happens in the above line***
builder.Call<AndroidJavaObject>("setMessage", message)
.Call<AndroidJavaObject>("setTitle", title)
.Call<AndroidJavaObject>("setCancelable", false)
.Call<AndroidJavaObject>("setNeutralButton", buttonText)
.Call("show");
}) );
#endif
}
What I've tried
- Used android.support.v7.app.AlertDialog.Builder instead of android.app.AlertDialog.Builder
- Added the support JARs for v7 & v4 (Specifically android-support-v4 & android-support-v7-appcompat)
- Tried creating my own implementation for Unity's JNI and invoking that
All the above failed. (The third one, rather spectacularly)
Now, I tried other classes as well, and most of them work (such as ProgressDialog, which is a subclass of AlertDialog), and those work!
At this point, I'm rather stumped and I can't think of a VALID reason for certain functions to work, and the others to fail.
Any thoughts are appreciated.
EDIT 1: Added LogCat
I/Unity: AndroidJavaException: java.lang.ClassNotFoundException: android.app.AlertDialog.Builder
at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <filename unknown>:0
at UnityEngine.AndroidJNISafe.CallStaticObjectMethod (IntPtr clazz, IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <filename unknown>:0
at UnityEngine.AndroidJavaObject._CallStatic[AndroidJavaObject] (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0
at UnityEngine.AndroidJavaObject.CallStatic[AndroidJavaObject] (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0
at UnityEngine.AndroidJavaObject.FindClass (System.String name) [0x00000] in <filename unknown>:0
at UnityEngine.AndroidJavaObject._AndroidJavaObject (System.String className, System.Object[] args) [0x00000] in <filename unknown>:0
at UnityEngine.AndroidJavaObject..ctor (System.String className, System.Object[] args) [0x00000] in <filename unknown>:0
at AndroidNative+<ShowAlert>c__AnonSto