Android NDK Development Steps

Source: Internet
Author: User

NDK development is generally divided into several steps: (By default you have installed the NDK and Android development environment)

1. Create a project

2. Create a JNI directory

3. Writing the Nativejava layer method

4. Generating JNI header files

(1) Writing javanative code
(2) Execute command javah
(3) Add Android.jar package to environment variable

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

6. Add the Mk file under the JNI directory.

7. Finally execute the ndk-build command under the project path.

8. Finally run the code to see the results.

Here are the detailed steps and:

Create an Android project without saying it.

Then we declare a native method

Below we add a JNI directory below the project

Then go to the CMD console and enter the current working directory below:

Then execute the javah command to generate the. h file

(among them, D:\android\android_sdk_windows\platforms\android-19\android.jar,
is the directory file in the Android SDK directory, in order to be simple, you can add the Android.jar file directory to the path;
The last com.example.hellondk_1.MainActivity is the package name plus class name)

Refresh the project to see one more. h file under the JNI directory

Then we create a. c file used to implement the. h file in a defined way.

Create a. c file to import the appropriate header file
(
Such as: 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 jclass) {

Return (*ENV)->newstringutf (env, "Hello from C!....");
}
Which implements the method Java_com_example_hellondk_11_mainactivity_getstringfromc in the header file (jnienv *, jclass);
)

Add the Mk file under the JNI directory.

(This can be imported into the NDK under the example of the Mk file and then modify it) as follows
Local_path: = $ (call My-dir)

Include $ (clear_vars)

Local_module: = Hello
Local_src_files: = hello.c

Include $ (build_shared_library)
which
Local_module is the name of the build-generated so name
Local_src_files indicates which of the. c files to compile is

Finally, execute the ndk-build command under the project path.

Refresh the project after execution you can see that there is one more. so file in the obj directory below the project.
Then add the static code block in the Java file. Add the. so file.

You can see the result by running the code at the end.

Here if you are using the Genymotion simulator, then you need to configure the genymotion to install a genymotion arm translation, download and then directly drag to install.

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

Android NDK Development Steps

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.