Ndk development is not as difficult as you think

Source: Internet
Author: User

Ndk: Native Development Kit Native Development Tool

What can ndk do: ndk enables Java to call the C function library in Android.

Why ndk is used: we all know that Java is a semi-explanatory language, and it is easy to obtain source code files after being decompiled. When developing some important protocols, we should be secure, use C language to write these important parts to increase the security of the system. Also, in some close-to-hardware environments, I believe everyone knows the advantages and disadvantages of C and Java. By the way, ndk does not significantly improve application efficiency. Why? We all think that C language is much more efficient than Java. On the one hand, with the continuous update of JDK, Java's efficiency also increases; on the other hand, even if C language encoding is used to improve the application efficiency, however, when Java and C are called to each other, it increases the overhead.

I will not talk about these questions here. If you want to learn more, please search for them by yourself.

The first step in ndk development is to build the environment.

First, go to http://developer.android.com/tools/sdk/ndk/index.html to download your corresponding platform development tools

Next, we need to implement the Linux environment to download cygwin http://www.cygwin.com/(for 64-bit users, you can directly download I have downloaded, Baidu's link should be faster than online installation, uploading to my online storage. Put the address in the reply later)

Select a friend who downloads online and the recommended address (domestic)

After the download source is selected, the download directory is selected. Click the "devel" branch in the component list. There are many components under this branch,

We must: binutils, GCC, GCC-mingw, GDB

After these four directories are selected, it will be a long wait. You can have a meal.

The configuration environment variables below: ndk environment variables need to ndk root directory (in fact, is the ndk-builder.cmd file directory) to add the system environment variables

Cygwin environment variables need to add the bin directory to the system environment variables

For example, my path is: C: \ Java \ android-ndk-r7b and c: \ Java \ cygwin \ bin

After the environment is configured, you can start encoding.

1. Create an android Project

2. Add a folder named JNI under the project directory (required)

3. Create Your. c file (my name is hello. C) in the JNI folder)

4. Create a file named Android. mk in the jni folder.

Add the. mk File

?
12345 LOCAL_PATH := $(call my-dir)    // Current path (you can easily understand the shell LANGUAGE)include $(CLEAR_VARS)LOCAL_MODULE    := Hello        // Name Of The. So library to be generatedLOCAL_SRC_FILES := Hello.c        // Your. c file nameinclude $(BUILD_SHARED_LIBRARY)

Now we can start writing our C code. Of course we cannot start writing from the main function here, but there is a fixed naming method.

, My function name is java_com_example_testndk_mainactivity_helloworldfromc (Java _ package name_classname_function name)

A friend who writes C may complain. When have I written such a long function name. No way. This is the JNI specification. It starts with Java _, followed by the Java application package name plus the class name, all of which are separated by a line below, and finally the C function name is followed by our c function name.

As for the parameter format and return value type, you can go to the JDK directory to view JNI. h file (My JNI file directory: C: \ Java \ jdk1.7.0 _ 25 \ include \ JNI), there are many function templates (different from C ++ templates)

Since there are too many source code, you can check it yourself. I will not map it.

In JNI. the 104th lines of the H file here we can see that the returned jstring is essentially a struct pointer. From the C code, we can see that it is a pointer to a string, and in Java it is also an array.

Now, the C Code is fully explained, and we will return to our android project.

From the name of the C-code function, you should be able to know my Java class name (this is required, because it must be one-to-one)
Note that the static code block in the red box in the figure


As we all know after learning Java, the first thing a class executes during initialization is not a construction method but a static code block. That's why system. loadlibrary () is put into a static code block. From the name we can guess, load the Library ("hello ")

Do you still remember the hello we declared in Android. mk, that is, the name there?

Next, I saw 12th lines of code, recalling the Java knowledge, and using native to indicate the local Java method, which is our c function. (In fact, this function is not uncommon in the android SDK). For example, we usually use the multimedia mediaplayer. You can check the source code and I will not release it here. There are many native methods in it, because you need to call the audio driver.



Now, the ndk project is over. Let's test it. First, compile our C code. Open CMD and switch to the project directory (project directory? Right-click the project name, properties, for example, and enter ndk-Builder (is the environment variable set successfully at that time? Check if there is an additional libhello. So file in the libs folder of the android project)
Then run our android project.



Finally, let me talk about my opinion. The first is the foundation of C language. The structure pointer must be well mastered. Take a good look at JNI. which functions are provided in the H file, and functions supporting interaction between C and Java? To use ndk well, use JNI

Ndk development is not as difficult as you think

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.