Featured post
java - Native functions throw UnsatisfiedLinkError in custom view, despite working in main activity -
for reason can call native functions main activity , not custom views i've created. here example file (i followed tutorial, renamed classes http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/)
see usage of native function "getnewstring".
package com.example.native; import android.app.activity; import android.app.alertdialog; import android.content.context; import android.graphics.bitmap; import android.graphics.canvas; import android.os.bundle; import android.view.view; public class nativetestactivity extends activity { static { system.loadlibrary("nativetest"); } private native string getnewstring(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.setcontentview(new bitmapview(this)); string hello = getnewstring(); // line works fine new alertdialog.builder(this).setmessage(hello).show(); } } class bitmapview extends view { static { system.loadlibrary("nativetest"); } private native string getnewstring(); public bitmapview(context context) { super(context); string hello = getnewstring(); // line throws unsatisfiedlinkerror new alertdialog.builder(this.getcontext()).setmessage(hello).show(); } }
how can call native functions in custom views?
i've built application android 2.2 app. i'm running application on htc desire. have latest sdk (9) , latest ndk (r5).
your problem trying call native function class dont belongs to.
you defined following jni function in c file:
jstring java_com_example_native_nativetestactivity_getnewstring()
this states native function when loaded bind method declared native in nativetestactivity class. when try call view class doesn't find function bind to.
in case following function (which of course not exist in .so):
jstring java_com_example_native_bitmapview_getnewstring()
if still want able call same function different classes can declare in container class can accessed class want.
eg:
java code:
package com.example.native; public class nativehelper { public native string getnewstring(); static { system.loadlibrary("nativetest"); } }
c code:
jstring java_com_example_native_nativehelper_getnewstring(jnienv* env, jobject javathis) { return (*env)->newstringutf(env, "hello native code!"); }
- Get link
- X
- Other Apps
Comments
Post a Comment