Configuration steps for creating a dynamic library file for Java

Source: Internet
Author: User

We installed the NDK while installing the environment, so files can be generated directly under Eclipse. The NDK's zipped package comes with some sample projects, and the NDK files are extracted directly into a directory.

The first time we generated the so file, we first used the example of Hello-jni under the NDK's sample.

1, start eclipse, through the Create project from existing source, select Hello-jni directory, by default option to create the project;

2, observe the HELLO-JNI project directory structure, found a root directory called Jni folder, the folder below there are two files, android.mk and HELLO-JNI.C.

ANDROID.MK is the configuration file used to generate so files, let's see what's Inside:

Local_path: = $ (call My-dir) include $ (clear_vars)

Local_module: = Hello-jni local_src_files: = Hello-jni.c

Include $ (build_shared_library)

The include $ (build_shared_library) indicates that a dynamic-link library, the so file, is generated, and the resulting library file name is called lib*.so

Local_module: = Hello-jni indicates that a library file named libhello-jni.so will be generated

Local_src_files: = HELLO-JNI.C indicates that the source file that generated the library file is Hello-jni.c

Open the Hello-jni.c file and you will see a function inside it:

Jstring Java_com_example_hellojni_hellojni_stringfromjni (jnienv* env, Jobject thiz) {return (*env)->NewS     Tringutf (env, "Hello from JNI!"); }

What principle does this function generate from? is based on the function public native String Stringfromjni () in the Hellojni.java file under SRC, generated. The naming rules are:

Java_ (fixed start) Com_example_hellojni (the package name connected with "_") _hellojni (class name) _stringfromjni (function name)

This function can be generated using the Javah command: Enter the bin\classes directory of the project directory using the command line mode, run the Javah Com.example.hellojni.HelloJni, all native functions are generated according to the rules in a header file called HelloJni.h.

3. Create and configure a new builder

1) Click Project->properties->builders->new to create a new builder. Click Program on the popup dialog, click OK;

2) in the popup dialog box "Edit Configuration", configure Tab "Main":

The path in the location where the nkd-build.cmd is required (under the NDK installation directory).

Workingdiretcoty need to fill in the Hellojni's engineering root directory.

3) in "Editconfiguration", Configure Tab "Refresh":

Tick "Refresh Resources upon completion",

Tick "The entire workspace",

Tick "recuresively include sub-folders".

4) in "Editconfiguration", Configure tab "Build Options":

Tick "After a" and "clean",

Tick "During Manual builds",

Tick "During Auto Builds",

Tick "Specify working set of relevant resources".

Click "Specify Resources ..." to check the "JNI" directory of the TESTNDK project, finish!

Save the settings and click OK.

4. Generate So File

Because we tick "During Auto builds", so when the project has changed, so file will be automatically compiled, the correct generation will be able to find more than two folders in the project directory, folder libs\ A file called libhello-jni.so is generated under the Armeabi directory. At this point, the work of generating so files with the NDK is complete.

5. Calls to so files

There is a section of code in the Hellojni.java file:

static {system.loadlibrary ("Hello-jni"); }

Use LoadLibrary can load the so file, loading the time do not need to write libhello-jni.so, as long as the Hello-jni can be written.

This article transferred from: blog.csdn.net/laczff21/article/details/7542236

This article is from the Android Development Learning Network, the original URL is: http://www.android-study.net/listxx.aspx?id=394

Configuration steps for creating a dynamic library file for Java

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.