I am trying use the sample ndk application given with android ndk in unity.
public class HelloJni extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText( stringFromJNI() );
setContentView(tv);
}
/* A native method that is implemented by the
* 'hello-jni' native library, which is packaged
* with this application.
*/
public native String stringFromJNI();
This is my Jni
#include
#include
jstring Java_com_myownjni_jni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
I have built the whole project,i placed the libhello-jni inside plugin/Android,from Unity i tried to access the library like this below
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System;
public class CallJavaCode : MonoBehaviour {
[DllImport("hello-jni")]
private static extern string stringFromJNI();
private string myText= "Get value from Jni";
void OnGUI ()
{
if (GUI.Button(new Rect (15, 125, 450, 100), myText))
{
string textFromJni= stringFromJNI();
myText= textFromJni;
}
}
}
But i am not getting any result,should i place the jar of the java class???
Can anyone help
03-14 17:35:23.895: INFO/Unity(8319): EntryPointNotFoundException: stringFromJNI
this is the error im getting
This question is probably better asked in the Unity forum, but I might be able to give a couple pointers.
1) Have your native function return a const char * instead of a jstring, then your function can return a simple string (e.g. "hello") instead of calling NewStringUTF.
2) Then, in your Unity script, have your native function signature return an IntPtr, like this:
3) Finally, when you call the method in your Unity script, marshal the ptr to a string, like this:
There may very well be other ways to do this, but I believe this approach will work.
- Kim