Android ndk Development

Source: Internet
Author: User
The android ndk development environment has been set up in Ubuntu, and there are a lot of network materials, but I still keep my own records. About ndk

Ndk, all called Native Development Kit, is a collection of tools.

* Ndk provides a series of tools to help developers quickly develop C (or C ++) dynamic libraries and automatically package so and Java applications into APK. These tools are of great help to developers.

* The ndk integrates the cross compiler and provides MK files to isolate differences such as CPU, platform, and Abi, developers can create so simply by modifying the MK file (indicating "which files need to be compiled" and "Compilation feature requirements.

Google explicitly states that this API is stable and supports the currently released API in all subsequent versions. From the ndk version, we can see that these Apis support very limited functions, including: C standard library (libc), standard Math Library (libm), and compression library (libz), log Library (liblog )."

Download and install

Http://dl.google.com/android/ndk/android-ndk-r7b-linux-x86.tar.bz2

Then, perform the following operations:

gavin@gavin-desktop:~$ tar -xvf android-ndk-r7b-linux-x86.tar.bz2 -C ~/gavin@gavin-desktop:~$ sudo vi /etc/profile

Add the following two lines at the end of the file

Ndk_home = $ home/android-ndk-r7b/export Path = $ path: $ ndk_home

Enter the ndk-build command to verify the success

Gavin @ Gavin-desktop :~ $ Ndk-build Android ndk: cocould not find application project directory! Android ndk: Please define the ndk_project_path variable to point to it./home/Gavin/android-ndk-r7b/build/CORE/build-local.mk: 130: *** Android ndk: aborting. Stop.

You can also compile the official sample program for testing.

gavin@gavin-desktop:~$ cd android-ndk-r7b/samples/hello-jni/gavin@gavin-desktop:~/android-ndk-r7b/samples/hello-jni$ ndk-build Gdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserverGdbsetup : libs/armeabi/gdb.setupCompile thumb : hello-jni <= hello-jni.cSharedLibrary : libhello-jni.soInstall : libhello-jni.so => libs/armeabi/libhello-jni.sogavin@gavin-desktop:~/android-ndk-r7b/samples/hello-jni$ ls libs/armeabi/gdbserver gdb.setup libhello-jni.so

Hello ndk

Create an android Activity project named hellondk in eclipse and place a textview on the interface. Set the ID to mytext. The content of the Java code file hellondkactivity. Java is as follows:

Package COM. gavin. example; import android. app. activity; import android. OS. bundle; import android. widget. textview; public class hellondkactivity extends activity {private textview mtextview;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mtextview = (textview) findviewbyid (R. id. mytext); mtextview. settext (sayhello (); // call the local code function here to obtain a string} public native string sayhello (); // The local code function static {system. loadlibrary ("hellondk"); // import the shared library hellondk }}

The next step is to generate the C Shared Library hellondk. Create a directory named JNI in the project directory and use the javah command to generate an imported function interface file.

gavin@gavin-desktop:~/workspace/HelloNDK$ javah -classpath bin/classes com.gavin.example.HelloNDKActivity

Copy the generated header file com_gavin_example_hellondkactivity.h to the jni directory.

Create a file com_gavin_example_hellondkactivity.c in the jni directory to implement the function declared in the header file. The content is as follows:

#include <string.h>#include <jni.h>#include <string.h>#include <jni.h>jstring Java_com_gavin_example_HelloNDKActivity_sayHello( JNIEnv* env,jobject thiz ){return (*env)->NewStringUTF(env, "Hello NDK");}

Create the file Android. mk in the jni directory. The content is as follows:

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

Now return to eclipse and integrate ndk-build into eclipse. properties-> builders-> New is configured as follows:

The working directory does not have a clear meaning. Save and exit, refresh the project, compile and start the simulator.

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.