NDK development is not as difficult as you think. NDK development and imagination

Source: Internet
Author: User

NDK development is not as difficult as you think. NDK development and imagination

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


Success is not as difficult as you think.

To be a success is not as difficult as you think!
To be a success is not as difficult as it is in your mind!

If you work very hard but the result is not as good as you think, what will you do?

Don't think that you are a genius, don't expect too high, such results may bring satisfaction to yourself even if not the best, vice versa, the expectation is too high. If the result is not so good, it will be very frustrating, but I will always do my best to do what I want to do. I will always pay attention to a peaceful heart when I keep a peaceful mind, in addition, this mentality does not change with the changes of external things. At this time, people can be casual, and the external influence on their hearts will not be so great, the idea of success or failure is no longer so strong. The so-called "Seeking for success" means ". If we use the best effort and all the methods to achieve the expected goal, we can't help it. It may be that the goal is too high, the most important thing in life is to use the principle of being able to naturally integrate into any circumstances and in any environment without changing the heart, we need to actively work hard to do our jobs without your grief. At the same time, we also need to find ways to improve our abilities and use such abilities. However, we should not be so ambitious and grasp the principles of good degree, to do everything, you must have a degree of principle and take yourself as a scholar. There are too many things you need to learn, don't think that you have read dozens of books, read some professional courses, even if you are a talented person, you have nothing to learn. Your expectation for yourself is that the more you learn, the better you don't know, at the same time, we must accept the ideas in the book and form our own thinking system.
 

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.