Android JNI Programming

Source: Internet
Author: User

JNI function names are divided into three parts:
The first is the Java keyword for Java Virtual Machine recognition; for example, Java_com_example_hellojni_HelloJni_stringFromJNI
Java: keyword
Com_example_hellojni: package name
HelloJni: File Name (originally named hello_jni)
StringFromJNI: function name
Then the caller Class Name (fully qualified class names, with underlines instead of name delimiters );
Finally, it is the corresponding method name, which is separated by underscores.

The JNI function _ parameters are also composed of three parts:
The first is JNIEnv *, which is a pointer to the JNI runtime environment;
The second parameter varies with whether the local method is static or non-static. The second parameter of the non-static local method references the object, the second parameter of the static local method is a reference to its Java class;
Other parameters correspond to common Java method parameters. The parameter types need to be mapped according to certain rules.
Jstring // return type, String
Java_com_example_hellojni_HelloJni_stringFromJNI (JNIEnv * env, jobject thiz)
{
Return (* env)-> NewStringUTF (env, "Hello from JNI! ");
}

Compile the make file:
LOCAL_PATH: = $ (call my-dir) // source file address, which must be in the first line

Include $ (CLEAR_VARS) // clear some variables except LOCAL_PATH

LOCAL_MODULE: = hello-jni // specify the name of the current compilation Module
LOCAL_SRC_FILES: = hello-jni.c // source file

Include $ (BUILD_SHARED_LIBRARY) // compile the current module as a shared link library with the prefix lib and suffix. so

Finally, compile the file in the Cygwin cross-compilation environment and generate the file in the libs directory of the root directory.
Armeabi/libhello-jni.so files

Copy the libs directory to the root directory of Eclipse to use the compiled local method.
Code:
Public class HelloJni extends Activity
{
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
TextView TV = new TextView (this );
TV. setText (stringFromJNI ());
SetContentView (TV );
}
Public native String stringFromJNI ();
Public native String unimplementedStringFromJNI ();
Static {
System. loadLibrary ("hello-jni ");
}
}


From the xtcpcgx Column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.