Plugin Current Activity

Hey all,

I am working on some plugins starting with Android, mainly I am creating a plugin for Flurry. I am currently overriding the activity with my own activity class. and onStart() I am having it call a C# script which calls the Java function on the activity and start’s the session. But the issue is on the Call from Unity to the activity.

if i do the following, where flurry plugin is the overriding activity class.

AndroidJavaClass jc = new AndroidJavaClass("net.company.FlurryPlugin");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call<string>("startFlurrySesson", new object[] {APIKEY});

Error

E/Unity   (26512): getFieldID("currentActivity", "Ljava/lang/Object;") FAILED!
I/Unity   (26512): JNI: Unable to find field id for 'currentActivity' (static)
I/Unity   (26512):
I/Unity   (26512): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
I/Unity   (26512):
I/Unity   (26512): JNI: Init'd AndroidJavaObject with null ptr!
I/Unity   (26512):
I/Unity   (26512): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
I/Unity   (26512):
I/Unity   (26512): JNI: Unable to find method id for 'startFlurrySesson'
I/Unity   (26512):
I/Unity   (26512): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
I/Unity   (26512):

But if i use

AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call<string>("startFlurrySesson", new object[] {APIKEY});

I only get the following error, which makes sense because this method doesnt exist in the base class.

 I/Unity   (26120):
    I/Unity   (26287): JNI: Unable to find method id for 'startFlurrySesson'
    I/Unity   (26287):
    I/Unity   (26287): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
    I/Unity   (26287):

@appearance.

As usual you create the boilerplate code as follows:

        if (Application.platform == RuntimePlatform.Android)
        {
            jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            currentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
            flurryHelper = currentActivity.Get<AndroidJavaObject>("flurryHelper");
        }

Then you can call methods on the flurryHelper class like so:

        AndroidJavaObject flurryDataList = flurryHelper.Call<AndroidJavaObject>("getDataList");
        int dataTableSize = flurryDataList.Call<int>("size");

Note that this example is not Flurry specific but a general way of writing code that passes parameters and get return values back from c# to Java.

In the last code snippet, the first line is calling a method getDataList() and returning a generic ‘AndroidJavaObject’. No need to cast it or otherwise convert the object if you just want to call methods on it.

The final line is calling a method on the returned object (above), this time the return value is an int.

Finally, if you want to pass in params to the Java methods you do something like this:

flurryData = flurryList.Call<AndroidJavaObject>("get", index);

In this case index is an int but we can pass in any type the receiving method accepts. In the code you were enquiring about, object{APIKEY} is an array of generic objects, this will be converted into strings on the other side.

Hope this helps.
Netlander

Alright so I figured it out. And it was just so obvious after I reread the documentation.

My Java function is Void and not String and so the method doesn’t exist, what is awesome is that the JavaClass that is grabbed must end up with your new class as you call methods that you make with the new extended class.

AndroidJavaClass jc = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);
AndroidJavaObject jo = jc.GetStatic(“currentActivity”);
jo.Call(“startFlurrySesson”, new object {APIKEY});

Hi,
i am implementing InAppPurchase in unity for android. i downlkoad sample project from this website. http://ge.tt/51dEEEO/v/0
when i run this sample project on unity project it gives error "JNI Init’d AndroidJavaClass with null pointer " and when i run it on device it crash some times and one times it run fine. when same code i implement in my own game project it always crash on device. please help me.
Thanks in advance.
Rana.Suave