[Android Study Notes] Learning JNI Programming

Source: Internet
Author: User

Since I have been engaged in C ++ development, it is inevitable to use the JNI mode for development when I transfer to the Android platform.

My ndk version is r5b. The directory contains a hello-JNI sample program, which breaks the cost of importing the project to eclipse,

According to the online tutorial, it did not work. Later I checked the official documents to complete the import and compilation.

(For details about how to build the eclipse + ndk + cygwin platform, see this document: Integrating cygwin with eclipse in windows and using Android ndk R5 (supporting opengles), integrating (transferring)
)

Official documentation:

In eclipse:

  1. ClickFile> New Android Project...
  2. SelectCreate project from existing source
    Radio button.
  3. Select any API level above Android 1.5.
  4. InLocation
    Field, clickBrowse...
    And select
    The<ndk-root>/samples/hello-jni
    Directory.
  5. ClickFinish
    .

Then you can compile it.

If you want to build a JNI project by yourself, it is also very easy to create a new Android project, and then manually create a "JNI" directory under the project directory (level with the src directory ), then create an android. MK (makefile of Android) and your c file or CPP file. For function declaration and call, refer to the sample program Hello-JNI, note that the function name must follow the rule "java_packetname_projectname_functionname". Otherwise, a link error is reported during running.

 

A small note when writing C or C ++ programs:

1. C code. Use the Env parameter as in the instance, (* env)-> newstringutf (ENV, "hello from JNI! ");

2. c ++ code, you need to call env-> newstringutf ("hello from JNI! ");

The reason is that the jnienv statement in JNI. H is different for C and C ++, as follows:

# If defined (_ cplusplus)

Typedef _ JNIEnv;

# Else

Typedef const struct JNINativeInterface * JNIEnv;

# Endif

 

Related Article

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.