Generate so files with NDK compilation under Eclipse

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)

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

< Span style= "COLOR: #3333FF" >local_module   : = Hello-jni indicates that a name will be generated called libhello-jni . So"

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)->newstringutf (env, "Hello from JNI!");
}

< Span style= "COLOR: #3333FF" > What principle does this function generate from? is based on the function in the Hellojni.java file under src  public Native string  Stringfromjni (); Generated by the. The naming rules are:

< Span style= "COLOR: #3333FF" > java_ (fixed start) com_example_hellojni (package name connected with" _ ") _hellojni (class name) _stringfromjni (function name)

     This function can be generated using Javah commands: Enter the bin\classes directory of the project directory with the command line, run javah   Com.example.hellojni. < Span style= "COLOR: #000000" >hellojni Span style= "Color:rgb (0,0,153)" > 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 automatically compile, the correct generation will be able to find in the project directory two more folders, folder Libs\armeabi directory generated a call Lib Hello-jni. So's file. 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");
}

You can load the so file with LoadLibrary, and you do not need to write libhello-jniwhen loading. So, just write Hello-jni.

Generate so files with NDK compilation under Eclipse

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.