Issue
I am trying creating simple jni but app is crashing .I am not able to find the mistake I have done .Here is my code :
Example.c
#include <time.h>
double My_variable = 3.0;
int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}
int my_mod(int x, int y) {
return (x%y);
}
char *get_time()
{
time_t ltime;
time(<ime);
return ctime(<ime);
}
Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := example
LOCAL_CFLAGS := -Werror
LOCAL_SRC_FILES := example.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
After this I have build the folder using NDK.Files generated in lib is libexample.so.And example.o ,example.o.d in obj folder.when I load lib by name example it is giving error while calling method only but when I load by name l*ibexample* it is giving error while loading lib as well as while calling method.
JNI
package com.example.pjtest;
public class JniClass {
public final static native char get_time();
}
MainActivity
package com.example.pjtest;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
try {
System.loadLibrary("example");
} catch (UnsatisfiedLinkError e) {
e.printStackTrace();
}
try{
System.out.print(JniClass.get_time());
}
catch(Exception e )
{
e.printStackTrace();
}
}
}
Here is log :
04-01 12:32:18.964: E/AndroidRuntime(5325): java.lang.UnsatisfiedLinkError: Native method not found: com.example.pjtest.JniClass.get_time:()C
04-01 12:32:18.964: E/AndroidRuntime(5325): at com.example.pjtest.JniClass.get_time(Native Method)
04-01 12:32:18.964: E/AndroidRuntime(5325): at com.example.pjtest.MainActivity.onCreate(MainActivity.java:18)
04-01 12:32:18.964: E/AndroidRuntime(5325): at android.app.Activity.performCreate(Activity.java:5066)
04-01 12:32:18.964: E/AndroidRuntime(5325): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
Solution
You have to load your library in your Activity
static {
System.loadLibrary("example");
}
Make sure your .so file in your lib folder or /system/lib/ in the device where your try to run the application.
Add this to your Example.c
jint Java_com_example_pjtest_get_time(JNIEnv *env)
{
time_t ltime;
time(<ime);
return ctime(<ime);
}
Answered By - Yuvaraja
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.