Ndk Development notes, ndk Development

Source: Internet
Author: User

Ndk Development notes, ndk Development

Ndk development: 1.Two types of Android Compilation Program. q2.install the ndkcompilation tool. 3.compile the android.apk program. 4. compile the jni interface. define the application interface, 5. compile a Java file to generate the corresponding bytecode file. 6. use the javah-jni Test command to generate the c header file corresponding to the java file. 7. use the ndk-build command to generate the corresponding library file. 1. Create an arm local program. directly use arm-linux-gcc for compilation. If the library is used, use the-static keyword for static link library files. 1. three methods for compiling android local programs: 1. use the ndk development tool for compilation. a) install the ndk android-ndk-r9d-linux-x86_64.tar.bz2 using tar-xvf android-ndk-r9d-linux-x86_64.tar.bz2. b) to decompress the package, you must configure the environment variable vim ~ /. Add the following row to bashrc. export PATH =/home/zshh/android-ndk-r9d: $ PATH // This is the directory of the unzipped ndk file/home/zshh/android-ndk-r9d c) copy an ndk sample file to the test directory. the sample file exists in/home/zshh/android-ndk-r9d/samples. $ cd/home/zshh/android-ndk-r9d/samples. $ cp-a hello-jni /~ /Work/android/JNI/$ zshh @ HP :~ /Work/android/JNI/hello-jni/jni $ vim Android. mk LOCAL_PATH: = $ (call my-dir) // LOCAL_PATH indicates the directory to be compiled. cd ~ /Work/android/JNI/hello-jni/use ndk-build. This directory refers ~ /Work/android/JNI/hello-jni/directory. include $ (CLEAR_VARS) // when ndk is used for compilation. this item must be specified. it clears all variable values in Makefile. this item cannot be used when android source code is used for compilation. LOCAL_MODULE: = hello-jni // name of the module after compilation. LOCAL_SRC_FILES: = hello-jni.c // required to generate the module. c file. // select either of the two. include $ (BUILD_SHARED_LIBRARY) // specifies the file to be generated. or dynamic library files. include $ (BUILD_EXECUTABLE) // specifies the file to be generated. is the executable file to modify the hello-jni.c output Hello character. 2. modify the file. generate an executable file. zshh @ HP :~ /Work/android/JNI/hello-jni/jni $ vim hello-jni.c # if 0 # endif annotate other files. change to: # include <string. h> # include <jni. h> int main (void) {printf ("Hello \ n"); return 0;} zshh @ HP :~ /Work/android/JNI/hello-jni/jni $ ndk-build/home/zshh/work/android/JNI/hello-jni/hello-jni.c: 57: 2: warning: incompatible implicit declaration of built-in function 'printf' [enabled by default] compiling will output the above warning. it indicates that printf does not contain stdlib. h header file. # include <stdlib. h> header file. zshh @ HP :~ /Work/android/JNI/hello-jni/jni $ ndk-build to generate the following file. zshh @ HP :~ /Work/android/JNI/hello-jni/jni $ ndk-build [armeabi-v7a] Gdbserver: [arm-linux-androideabi-4.6] libs/armeabi-v7a/gdbserver [armeabi-v7a] Gdbsetup: libs/armeabi-v7a/gdb. setup [armeabi] Gdbserver: [arm-linux-androideabi-4.6] libs/armeabi/gdbserver [armeabi] Gdbsetup: libs/armeabi/gdb. setup [x86] Gdbserver: [x86-4.6] libs/x86/gdbserver [x86] Gdbsetup: libs/x86/gdb. setup [mips] Gdbserver: [mipsel-linux -Android-4.6] libs/mips/gdbserver [mips] Gdbsetup: libs/mips/gdb. setup [armeabi-v7a] Install: hello-jni => libs/armeabi-v7a/hello-jni [armeabi] Install: hello-jni => libs/armeabi/hello-jni [x86] Install: hello-jni => libs/x86/hello-jni [mips] Install: hello-jni => libs/mips/hello-jni he will compile and generate executable files for the three platforms, the platform is above. x86, mips, armeabi-v7a, 3. modify the compilation to generate executable files for the specified platform. zshh @ HP :~ /Work/android/JNI/hello-jni/jni $ vim Application. mk APP_ABI: = all: generate executable files for the three supported platforms. APP_ABI: = armeabi-v7a only generates executable code for the armeabi-v7a platform. 4. download the generated file to the development version for execution. // only after the system is started. check whether the usb is plugged in. after entering the system, run the mount command. is zshh @ HP :~ /Work/android/JNI/hello-jni/jni $ adb shell mount-o remount, rw/system // The command is used to re-mount the file with the read/write permission. // switch to the directory where the compiled executable file is located. zshh @ HP: $ cd ~ /Work/android/JNI/hello-jni/libs/armeabi-v7a zshh @ HP :~ /Work/android/JNI/hello-jni/libs/armeabi-v7a $ adb push hello-jni/system // download the application to the development/system path. 208 KB/s (9500 bytes in 0.044 s) 5. finally, test whether the output is successful. Hello. zshh @ HP :~ /Work/android/JNI/hello-jni/libs/armeabi-v7a $ adb shell // log on to the android operating system through the android debug bridge. root @ android: cd/system root @ android:/system #. /hello-jni Hello. The test is completed. output Hello.6. if the compiled android source code is used for compilation. android. MK file,. modify Android. mkzshh @ HP :~ /Work/android/JNI/hello-jni/jni $ vim Android. mk LOCAL_PATH: = $ (call my-dir) // LOCAL_PATH indicates the directory to be compiled. cd ~ /Work/android/JNI/hello-jni/use ndk-build. This directory refers ~ /Work/android/JNI/hello-jni/directory. # include $ (CLEAR_VARS) // This variable needs to be commented out. It is used to clear the variable values in the MK file. this is not required when compiling with android source code. note. LOCAL_MODULE: = hello-jni // name of the module after compilation. LOCAL_SRC_FILES: = hello-jni.c // required to generate the module. c file. // select either of the two to specify the file to be generated. include $ (BUILD_SHARED_LIBRARY) // This is to generate a dynamic library file. include $ (BUILD_EXECUTABLE) // This is the generated Executable File zshh @ HP $ cd/home/zshh/work/arm/ARM1/Android/android-4.2.2_r1 B. set the execution environment of the current shell. zshh @ HP :~ /Work/arm/ARM1/Android/android-4.2.2_r1 $ source ~ /. Bashrczshh @ HP :~ /Work/arm/ARM1/Android/android-4.2.2_r1 $ source setenv including device/asus/grouper/vendorsetup. shincluding device/asus/tilapia/vendorsetup. shincluding device/friendly-arm/tiny4412/vendorsetup. shincluding device/generic/armv7-a-neon/vendorsetup. shincluding device/generic/armv7-a/vendorsetup. shincluding device/generic/mips/vendorsetup. shincluding device/generic/x86/vendorsetup. shincluding device/lg E/mako/vendorsetup. shincluding device/samsung/maguro/vendorsetup. shincluding device/samsung/manta/vendorsetup. shincluding device/samsung/toroplus/vendorsetup. shincluding device/samsung/toro/vendorsetup. shincluding device/ti/panda/vendorsetup. shincluding sdk/bash_completion/adb. bash c. use mmm to compile the directory to be compiled. zshh @ HP :~ // Work/arm/ARM1/Android/android-4.2.2_r1 $ mmm ~ /Work/android/JNI/jni_source_build/jni/target Executable: hello-jni (out/target/product/tiny4412/obj/EXECUTABLES/hello-jni_intermediates/LINKED/hello-jni) /home/zshh/work/arm/ARM1/Android/android-4.2.2_r1/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin /.. /lib/gcc/arm-linux-androideabi/4.6.x-google /.. /.. /.. /.. /arm-linux-androideabi/bin/ld: out/target/product/tiny4412/obj/lib/crtbegin_dyn Amic. o: in function _ start: crtbrand. c (. text + 0x60): error: undefined reference to '_ libc_init' if this error occurs. then you need to include the libc library, LOCAL_SHARED_LIBRARIES: = Android after libc is added. the mk file is as follows: LOCAL_PATH: = $ (call my-dir) // LOCAL_PATH indicates the directory to be compiled. cd ~ /Work/android/JNI/hello-jni/use ndk-build. This directory refers ~ /Work/android/JNI/hello-jni/directory. # include $ (CLEAR_VARS) // This variable needs to be commented out. It is used to clear the variable values in the MK file. this is not required when compiling with android source code. note. LOCAL_MODULE: = hello-jni // name of the module after compilation. LOCAL_SRC_FILES: = hello-jni.c // required to generate the module. c file. LOCAL_SHARED_LIBRARIES: = libc // You can select either of the two to specify the file to be generated. include $ (BUILD_SHARED_LIBRARY) // This is to generate a dynamic library file. include $ (BUILD_EXECUTABLE) // This is to generate the executable file zshh @ HP :~ // Work/arm/ARM1/Android/android-4.2.2_r1 $ mmm ~ /Work/android/JNI/jni_source_build/jni/'Install: out/target/product/tiny4412/system/bin/hello-jni if compilation is successful, the generated target file contains the hello-jni file out/target/product/tiny4412/system/bin/hello-jni d. next steps, and Step 5. yes. 2. Create an NDK local program. the function of this program is to control the led lighting. 1.build a eclipseenvironmental .eclipse.tar.gz. decompress the file. zshh @ HP :~ /Work/android $ tar-xvf eclipse.tar.gz-C software // decompress zshh @ HP :~ /Work/android $ cd software/eclipse/zshh @ HP :~ /Work/android/software/eclipse $. /eclipse & // run eclipse B. use this ide environment to create an android apk application. the general function is to provide four buttons. When one of the buttons is pressed, a light is lit up. c. create a Test file, including the interface file: 1. defines the interface between c and java. control light-off, 1. 1. the first interface should be to open the device file. native int openLed (); 1. 2. the last one is to close the device file. native int closeLed (); 1. 3. the second interface should be bright, native int ledOn (int no); 1. 4. the third interface should be disabled. native int ledOff (int no); 2. zshh @ HP :~ /Work/android/JNI/NDKnative $ vim Test. java /************************************** * **********************************> File Name: test. java> Author: zshh0604> Mail: zshh0604 @. com> Created Time: mon 22 Dec 2014 10:48:09 ********************************** * ***********************************/public class Test {native int openLed (); native int closeLed (); native int onLed (int no); native int OffLed (int no); static {System. loadLibrary ("led");} public static void main (String [] args) {}} 3. compile and generate bytecode files. zshh @ HP :~ /Work/android/JNI/NDKnative $ javac Test. java 4. Generate the corresponding header file. After the command is executed, the Test. h header file. zshh @ HP: ~ will be generated :~ /Work/android/JNI/NDKnative $ javah-jni Test 5. copy an NDK code example. and Test. h. Copy the header file to the jni directory of the current file. zshh @ HP :~ /Work/android/JNI $ mkdir Jni6. sample code for obtaining the ndk development tool. zshh @ HP :~ /Work/android/JNI/Jni $ cp-a.../../android-ndk-r9d/samples/hello-jni/./zshh @ HP :~ /Work/android/JNI/Jni/jni $ cp .. /.. /NDKnative/Test. h 7. set Test. h is renamed as led. c, mv Test. h led. c zshh @ HP :~ /Work/android/JNI/Jni/jni $ mv Test. h led. c8. obtain the complete Class Name of the apk application. com_embsky_MainActivity // Replace the current Test class name with this name. use vim to open the led. run the following command in command line mode to replace Test with com_embsky_MainActivity: % s/Test/com_embsky_MainActivity/g. obtain the following file: # include <jni. h> # include <sys/types. h> # include <stdlib. h> # include <fcntl. h> # include <errno. h> static int fd; static int flags = 0;/** Class: com_embsky_MainActivity * Method: openLed * Signature :() I */JNIEXPORT jint JNICALL Java_com_embsky_MainActivity_openLed (JNIEnv * env, jobject obj) {if (flags = 0) {fd = open ("dev/leds", O_RDWR ); if (fd <0) {return-EPERM;} flags = 1; return 0;} return-EBUSY;}/** Class: com_embsky_MainActivity * Method: closeLed * Signature :() I */JNIEXPORT jint JNICALL Java_com_embsky_MainActivity_closeLed (JNIEnv * env, jobject obj) {if (flags = 1) {close (fd); flags = 0; Return 0;} return-ENODEV;}/** Class: com_embsky_MainActivity * Method: onLed * Signature: (I) I */JNIEXPORT jint JNICALL inline (JNIEnv * env, jobject obj, jint no) {int ret; if (flags = 1) {ret = ioctl (fd, 1, no); if (ret <0) {return-EPERM ;} return 0;} return-ENODEV;}/** Class: com_embsky_MainActivity * Method: offLed * Signature: (I) I */JNIEXPORT jint JNICALL Java_c Om_embsky_MainActivity_offLed (JNIEnv * env, jobject obj, jint no) {int ret; if (flags = 1) {ret = ioctl (fd, 0, no ); if (ret <0) {return-EPERM;} return 0;} return-ENODEV;} 9. modify Android. mk file, as follows: LOCAL_PATH :=$ (call my-dir) include $ (CLEAR_VARS) LOCAL_MODULE: = ledsLOCAL_SRC_FILES: = led. cinclude $ (BUILD_SHARED_LIBRARY) 10. zshh @ HP :~ /Work/android/JNI/Jni/jni $ ndk-build 11. the dynamic library is generated as follows: [armeabi-v7a] Install: libleds. also => libs/armeabi-v7a/libleds. so 12. push the dynamic library to the/system/lib directory of the Development Board. zshh @ HP :~ /Work/android/JNI/Jni/libs/armeabi-v7a $ adb push libleds. so/system/lib 13. dynamic library creation completed. 14. create an apk application and a class: com. embsky. mainActivity. load and call the local library led in this class.

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.