I want to integrate .jar files which has facebook sdk for android. Also want to integrate flurry and twitter functionality using JNI.
The problem is I dont have any idea about JNI. I have read the classes in reference but could not make out. When to use AndroidJNI, AndroidJNIHelper?
for example I have jar file having code,
package jni;
public class TestClass
{
private String a;
public TestClass()
{
a="Hi all";
}
public String func()
{
return a;
}
}
I tried to do this but not getting
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class TestJNI : IDisposable
{
public static TestJNI instance;
private String a;
private AndroidJavaClass cls_Test = new AndroidJavaClass("jni.TestClass");
public String print()
{
a=cls_Test.Call<string>("func");
return a;
}
public void Dispose()
{
cls_Test.Dispose();
}
}
I tried printing string using
print(TestJNI.instance.print());
but got this : “NullReferenceException: Object reference not set to an instance of an object”
Thanks
I got it working for sample code if I have a jar file having class name “classA” with static function “func_A()” which is supposed to be called. Package “com.research.pkgA”
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class JNIcall : IDisposable
{
private static JNIcall _instance;
public static JNIcall Instance
{
get
{
if(_instance == null)
_instance = new JNIcall ();
return _instance;
}
}
private AndroidJavaClass cls_jni = new AndroidJavaClass("com.research.pkgA.classA");
public void Share()
{
using(AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using(AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
cls_jni.CallStatic("func_A",obj_Activity);
}
}
}
public void Dispose()
{
cls_jni.Dispose();
}
};
And your AndroidManifest.xml file should include following activity.
<activity android:name=".classA"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation" >
</activity>
Where do you set value of “instance”? I can’t see it here. Which line throws the exception?
In the least you need to instantiate your TestJNI class.
TestJNI test_class = new TestJNI();
then you can call
test_class.print();
The “instance” variable in your class is never assigned a value, that is why you get the exception.