Android NDK development steps, androidndk

Source: Internet
Author: User

Android NDK development steps, androidndk

NDK development is generally divided into the following steps: (you have installed the NDK and android development environments by default)

1. Create a project

2. Create a JNI directory

3. Compile the nativejava Layer Method

4. Generate the JNI header file

(1) Compile javaNative code
(2) run the javah command.
(3) Add the android. jar package to the Environment Variable

5. Create a. c file to import the corresponding header file

6. Add the mk file in the jni directory.

7. Finally, run the ndk-build command in the project path.

8. Run the code to view the result.

The following are detailed steps and:

Create an android project.

Then we declare an native method.

Next we will add a jni directory under the project.

 

Then go to the cmd console to enter the current working directory:

Then run the javah command to generate a. h file.

(Where, D: \ android \ android_sdk_windows \ platforms \ android-19 \ android. jar,
Is the directory file under the android sdk directory. To make it simple, you can add the android. jar file directory to the path;
Com. example. hellondk_1.MainActivity is the package name and class name)

Refresh the project and you can see that there is an additional. h file under the jni directory.

Then we create a. c file to implement the. h file definition method.

Create a. c file and import the corresponding header file
(
For example, create a hello. c file.
# Include <stdio. h>
# Include <stdlib. h>
# Include "com_example_hellondk_1_MainActivity.h"

JNIEXPORT jstring JNICALL Java_com_example_hellondk_11_MainActivity_getStringFromC
(JNIEnv * env, jclass ){

Return (* env)-> NewStringUTF (env, "Hello from C !.... ");
}
The method Java_com_example_hellondk_11_MainActivity_getStringFromC (JNIEnv *, jclass) in the header file is implemented );
)

 

Add the mk file in the jni directory.

(You can import the mk file under the ndk instance and modify it .)
LOCAL_PATH: = $ (call my-dir)

Include $ (CLEAR_VARS)

LOCAL_MODULE: = hello
LOCAL_SRC_FILES: = hello. c

Include $ (BUILD_SHARED_LIBRARY)
Where,
LOCAL_MODULE indicates the name of The so generated by compilation.
LOCAL_SRC_FILES indicates the. c file to be compiled

Finally, run the ndk-build command in the project path.

After the execution, refresh the project. You can see that there is an additional. so file in the obj directory under the project.
Then add a static code block to the java file. Add the. so file.

 

Finally, you can see the result by running the code.

If you are using the genymotion simulator, You need to configure genymotion to install a genymotion arm translation. After downloading it, drag and drop it to install it.

:

Http://download.csdn.net/detail/u014132820/9044875

 

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.