The NDK development Eclipse Integration Environment uses the NDK to develop Android

Source: Internet
Author: User

Today I learned about the Android NDK, so write it down. It is said that the NDK starts with a compiler from R7 and does not need to configure the Cygwin environment on Windows. Now I'm using the NDK R10 to develop.

The NDK was built in the morning and wrote an example, but it did not have a good use of Eclipse functionality, which could be slower if developed.

1) First, configure the NDK path in eclips
In Eclipse, click the menu bar window-perferences to configure the path to the NDK. Figure one is the path where the NDK is located.

(Figure I)

2) Add native Lib in the project

Create a new blank Android project, right-click on the root directory adnroid tools-add Native support ...

3) in the Pop-up dialog box, enter the name of the Lib to be created, click Finish, in the project directory will be more than a JNI folder, there is a blank CPP file and a android.mk file

This androd.mk file is very short, let's take a line-by-row explanation:

Local_path: = $ (call My-dir)

A android.mk file must first define the Local_path variable. It is used to find source files in the development tree. In this example, the macro function ' My-dir ', provided by the compilation system, is used to return the current path (that is, the directory containing the android.mk file).

Include $ (clear_vars)

Clear_vars is provided by the build system, specifying that GNU makefile clears many local_xxx variables for you (such as Local_module, Local_src_files, local_static_libraries, etc...) , except Local_path. This is necessary because all the compilation control files are in the same GNU make execution environment, and all the variables are global.

Local_module: = Testjni

To compile the target object, the Local_module variable must be defined to identify each module that you describe in the Android.mk file. The name must be unique and does not contain any spaces.

Note: The compilation system will automatically generate the appropriate prefix and suffix, in other words, a shared library module named ' Hello-jni ' will generate ' libtestjni.so ' files.

Important NOTE: If you name the library ' Libtestjni ', the compilation system will not add any LIB prefixes and will generate ' libtestjni.so ', This is to support the Android.mk file from the source code of the Android platform, if you really need to do so.

Local_src_files: = TestJni.cpp

The local_src_files variable must contain a C or C + + source code file that will be compiled into a module. Note that you do not have to list header files and include files here, because the compilation system will automatically find the dependent files for you; Just list the source code files that are passed directly to the compiler.

Note that the default C + + source file extension is '. cpp '. It is also possible to specify a different extension, as long as the local_default_cpp_extension variable is defined, do not forget to start the small dot (i.e. '. Cxx ' instead of ' cxx ')

Include $ (build_shared_library)

Build_shared_library means compiling a shared library, which is a compiled system-supplied variable, pointing to a GNU makefile script, responsible for collecting since the last call to ' include $ (clear_vars) ', defined in Local_ XXX variable, and decide what to compile and how to do it correctly. There are also build_static_library variable representations to generate a static library: lib$ (Local_module). A, build_executable represents the build executable.

The Mainactivity.java code is as follows:

Package Com.zhw.testjni;import Android.os.bundle;import Android.app.activity;import android.widget.textview;public Class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); TextView TextView = new TextView (this); Textview.settext (Hellofromjni ()); Setcontentview (TextView);} Public native String Hellofromjni (); static {system.loadlibrary ("Testjni");}}

The TestJni.cpp code is as follows:

5) Run the program directly, you can see the output of the NDK build in the console window

The operation results are as follows

As for how C + + compiles are implemented, we can look at the properties of the project

There are two more items in builders, one is CDT builder, and the other is scanner Configuration builder.
Add Builder to existing project
If there is already a JNI directory in the project, but do not have the compiler to configure C + +, the master can add their own builder, for my love opportunistic little rookie, Project right-click menu, new Add a blank so, and then delete, CDT Builder and scanner The Configuration Builder is out.

The previous article was to use Add Builder.

The NDK development Eclipse Integration Environment uses the NDK to develop Android

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.