- Home /
Calling .jar function in unity
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
Answer by hirenkacha · Mar 06, 2013 at 06:22 AM
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>
Would you be able to pack this into a example project? I'm working on implementing the IABHelper (in-app billing for android) and would need to access there .jar functions.
@hirenkacha - where did you placed your java lib? I have a java lib called - HelloWorldlib.jar which I placed in Assets/ folder of the project. However am always receiving "Null Ptr" while trying to fetch the java object. Here is the exception am getting - Exception: JNI: Init'd AndroidJavaClass with null ptr! UnityEngine.AndroidJavaClass..ctor (IntPtr jclass) (at /Users/builduser/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:556) UnityEngine.AndroidJavaObject.get_JavaLangClass () (at /Users/builduser/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:534) UnityEngine.AndroidJavaObject.FindClass (System.String name) (at /Users/builduser/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:525) UnityEngine.AndroidJavaClass._AndroidJavaClass (System.String className) (at /Users/builduser/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:545) UnityEngine.AndroidJavaClass..ctor (System.String className) (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/AndroidJavaBindings.gen.cs:94) PlayerController..ctor ()
Any idea or inputs?
Answer by andisopany · Oct 16, 2012 at 06:06 AM
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.
then where to use AndroidJNI or AndroidJavaCalss?? As TestJNI is a jar file not cs file.
Answer by Paulius-Liekis · Oct 15, 2012 at 01:27 PM
Where do you set value of "instance"? I can't see it here. Which line throws the exception?
actually I have not done JNI before. So I am totally blank in this.
Your answer
