Adding native functions to Android project

Hello all

I want to add some native methods to make the Android application communicate with the OpenFrameworks native C++ classes.

for example: I want to send a String from my Android Activity to be drawn with OpenFrameworks.

would you please give me details about how to achieve this: make files, where to add native functions and so on ?

you need to use jni, check the hello-jni sample in the android ndk also the OFAndroid and ofxAndroid source has some examples on how to do this. it can be quite tricky so my advice is to try to avoid it as much as possible. Also there’s some utility functions in ofxAndroidUtils.h that can solve some common problems

Thanks Arturo for the hints.

I noticed that in the example programs in main.cpp there is the following method:

extern "C" {  
	void Java_cc_openframeworks_OFAndroid_init( JNIEnv* env, jobject thiz ) {  
		 __android_log_print(ANDROID_LOG_VERBOSE, "", "Entering main");  

Which is called from Java.
The question is, what Java class in the project or the referenced library project does this methog get called ?

I want to create a similar method, so understanding how this works will help me a lot

this is a special function that gets automatically called when you load a dynamic library from java so it’s not a very good example but the syntax is something like:

Java inside OFActivity:

public native  void callC(int param);  


extern "C" {    
    void Java_cc_openframeworks_appname_OFActivity_callC( JNIEnv* env, jobject thiz, jint param) {    
        ofLogNotice() << param;   

accessing strings for example is slightly more complicated since a String is an object

Thanks a lot Arturo, it worked