"Hello World NDK" for Android NDK Development"

Source: Internet
Author: User

For example, next we will talk about the development of a small column of NDK, so that you can understand the process of developing Android NDK. In my work, what my boss taught me and what I understand, if you have any questions, please advise me!

(1) first introduce your development environment: Develop eclipse + android + ndk under Ubuntu. There must be many online development environments for configuring android, ndk can be downloaded by yourself, including windows and linux versions. I still need some basic knowledge about JNI, so I won't talk much about it as I just learned it.

(2) create an android project ndk_hello and configure

Create a new class NDKLib under the ndk. test package, and write the code:

public class NDKLib {static{System.loadLibrary("ndk_hello");}public native String hello();}

Here we need to use the knowledge of JNI, The nativie keyword. We will not use java to compile the hello () method throughout the project, is to call the hello () method of C ++.

The static content "ndk_hello" corresponds to the class C found, which is consistent with the subsequent android. mk.

(3) Find the downloaded ndk, open the apps folder, and create a new file named ndk_hello. Then, click F3 to open another window and find your working directory, copy the shortcut you created to ndk_test, change the shortcut you copied to the project (only required), and then open it on the terminal: (run the following command)

Create a new file in apps/ndk_hello: Application. mk. The content is as follows: (you should create the file first in the previous step to succeed. Sorry, the order is wrong)

APP_PROJECT_PATH := $(call my-dir)/projectAPP_MODULES      := ndk_hello

(4) After that, process the previously written NDKLib. The folder name in my ubuntu is yandong, which is processed by your machine, my working directory is in yandong/java/workplace

The sequence is as follows: ① find the new project, project/bin, compile ndk. test. NDKLib (javah ndk. test. NDkLib), and generate something automatically under the lib file of the project.

② Mkdir jni: Create a jni folder

③ Copy the. h file generated by Step 1 to the jni folder.

(5) refresh your project under eclipse and find the. h file under jni. The content of this. h file is as follows:

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class ndk_test_NDKLib */#ifndef _Included_ndk_test_NDKLib#define _Included_ndk_test_NDKLib#ifdef __cplusplusextern "C" {#endif/* * Class:     ndk_test_NDKLib * Method:    hello * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_ndk_test_NDKLib_hello  (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif

Create a. c file in the jni folder, which is the same as the. h file name. The content is as follows:

#include "ndk_test_NDKLib.h"JNIEXPORT jstring JNICALL Java_ndk_test_NDKLib_hello  (JNIEnv * env, jobject obj) {return (*env)->NewStringUTF(env, "Hello World NDK!!");}

The include file imports the self-generated header file. The method name must be the same as the method name in the header file. The header file is similar to the java interface.

(6) create a new file: Android. mk under jni. The content is as follows:

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := ndk_helloLOCAL_SRC_FILES := ndk_test.cinclude $(BUILD_SHARED_LIBRARY)

Ndk_test is the name defined in the NDKLib class, And ndk_test.c is the C file created under jni.

(7) So far, the NDK development process has basically ended. The last step is to display Hello World NDK!

This step is very simple. Set a TextView in the activity and call this:

package ndk.test;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class NDKTest extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);TextView tv = (TextView) this.findViewById(R.id.text);tv.setText(new NDKLib().hello());}}

Shown as follows:

Here, Hello World NDK is over. In order to demonstrate the difference between the newly created Project, it may be a bit different, but it is best to ensure that the project name and android under jni. (LOCAL_MODULE: = ndk_hello) in mk and Application in apps in ndk. mk (APP_MODULES: = ndk_hello) and so on.

I also want to discuss about how to learn about NDK development. I feel that there are few examples of developing NDK. Please give me some advice!

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.