Document directory
- 1. Compile helloworld. c Android. mk
1. Compile helloworld. c Android. mk
[Root @ fontlose jni] # cat hello. c
[Cpp]
View plaincopyprint?
- # Include <stdio. h>
- Int main ()
- {
- Printf ("Hello World! \ N ");
- Return 0;
- }
[Root @ fontlose jni] # cat Android. mk
[Plain]
View plaincopyprint?
- LOCAL_PATH: = $ (call my-dir)
- Include $ (CLEAR_VARS)
- LOCAL_SRC_FILES: = hello. c
- LOCAL_MODULE: = helloworld
- LOCAL_MODULE_TAGS: = optional
- Include $ (BUILD_EXECUTABLE)
Ii. Compile 1) Use NDK r5
Directory structure-hello
── Jni
── Android. mk
── Hello. c
[Plain]
View plaincopyprint?
- [Root @ fontlose nativity] # cd hello
- [Root @ fontlose hello] # export NDK_PROJECT_PATH = 'pwd'
- [Root @ fontlose hello] # ndk-build
- Install: helloworld => libs/armeabi/helloworld
- [Root @ fontlose hello] # adb push libs/armeabi/helloworld/data
- 44 KB/s (2176 bytes in 0.047 s)
- [Root @ fontlose hello] # adb shell
- /# Cd/data
- /Data # ls-l
- -Rwxrwxrwx root 2176 2011-08-07 0:01 helloworld
- /Data #./helloworld
- Hello World!
2) use the general toolchain
Under the hello. c directory[Plain]
View plaincopyprint?
- [Root @ fontlose jni] # arm-none-linux-gnueabi-gcc-static hello. c-o helloworld
- [Root @ fontlose jni] # adb push helloworld/data
- 1718 KB/s (581219 bytes in 0.330 s)
- [Root @ fontlose jni] # adb shell
- /# Cd/data
- /Data # ls-l
- -Rwxrwxrwx Root 581219 helloworld
- /Data #./helloworld
- Hello world!
Windows:
Http://www.codesourcery.com/gnu_toolchains/arm/portal/package3400/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.exe
Linux:
Http://www.codesourcery.com/gnu_toolchains/arm/portal/package3399/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.bin
3) compile in the source code Environment
1. Create the hello directory under the development Directory of the source code.[Plain]
View plaincopyprint?
- [Root @ fontlose Android-2.3] # mkdir $ android_src/development/Hello
2. Copy helloworld. c Android. mk to the hello directory.
3. [root @ fontlose Android-2.3] #. build/envsetup. sh
4. [root @ fontlose Android-2.3] # mmm development/hello/
[Plain]
View plaincopyprint?
- ========================================================== ====
- Platform_version_codename = REL
- Platform_version = 2.3.2
- Target_product = generic
- Target_build_variant = ENG
- Target_simulator =
- Target_build_type = release
- TARGET_BUILD_APPS =
- TARGET_ARCH = arm
- HOST_ARCH = x86
- HOST_ OS = linux
- HOST_BUILD_TYPE = release
- BUILD_ID = GRH78C
- ========================================================== ====
- Make: Enter the directory "/opt/FriendlyARM/mini6410/android/Android-2.3"
- Target thumb C: helloworld <= development/hello. c
- Target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)
- Target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld)
- Target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld)
- Install: out/target/product/generic/system/bin/helloworld
- Make: Leave the directory "/opt/FriendlyARM/mini6410/android/Android-2.3"
5. After compilation is completed, the execution file is in out/target/product/generic/symbols/system/bin/helloworld.
Download To device[Plain]
View plaincopyprint?
- [Root @ fontlose Android-2.3] # adb push out/target/product/generic/symbols/system/bin/helloworld/data
- 135 KB/s (7608 bytes in 0.054 s)
- [Root @ fontlose Android-2.3] # adb shell
- /# Cd/data
- /Data # ls-l
- -Rwxrwxrwx Root 7608 helloworld
- /Data #./helloworld
- Hello world!
4) use the Toolchain provided by the source code
Before that, we need to clean the helloworld module in the previous step:[Plain]
View plaincopyprint?
- [Root @ fontlose Android-2.3] # Make clean-helloworld
Use the showcommands option to re-compile helloworld and view the specific commands. The output command line shows that, the cross-compilation tool chain used by the Android compiling environment is prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-gcc.[Plain]
View plaincopyprint?
- [Root @ fontlose Android-2.3] # Make helloworld showcommands
Use the compilation command output above to simplify the manual compilation of the helloworld program. First, manually delete the helloworld program compiled last time:[Plain]
View plaincopyprint?
- [Root @ fontlose Android-2.3] # rm out/target/product/generic/system/bin/helloworld
Gcc compilation to generate the target file[Plain]
View plaincopyprint?
- [Root @ fontlose Android-2.3] # prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-gcc-I development/hello-I out/target/product/generic/obj /EXECUTABLES/helloworld_intermediates-I dalvik/libnativehelper/include/nativehelper-I system/core/include-I hardware/libhardware/include-I hardware/ril/ include-I dalvik/libnativehelper/include-I frameworks/base/opengl/include-I frameworks/base/native/include-I external/skia/include -I out/target/product/generic/obj/include-I bionic/libc/arch-arm/include-I bionic/libc/include-I bionic/libstdc ++/include -I bionic/libc/kernel/common-I bionic/libc/kernel/arch-arm-I bionic/libm/include/arch/arm-I bionic/libthread_db/include-c-fno-exceptions-Wno-multichar-msoft-float-fpic-ffunction-sections-funwind-tables-fstack-protector-Wa, -- noexecstack-Werror = format-security-fno-short-enums-march = armv5te-mtune = xscale-d1_arm_arch_5 _-region _-d1_arm_arch_5te _-include system/core/include/arch/linux-arm/AndroidConfig. h-I system/core/include/arch/linux-arm/-Wno-psabi-mthumb-interwork-DANDROID-fmessage-length = 0-W-Wall-Wno-unused- winit-self-Wpointer-arith-Werror = return-type-Werror = non-virtual-dtor-Werror = address-Werror = sequence-point-DNDEBUG-g-Wstrict-aliasing = 2-finline-functions-fno-inline-functions-called-once-fgcse-after-reload-frerun-cse-after-loop-frename-registers-DNDEBUG-UDEBUG-mthumb- OS-fomit-frame-pointer-fno-strict-aliasing-finline-limit = 64-MD-o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello. o development/hello. c
Generate an executable file:[Plain]
View plaincopyprint?
- [Root @ fontlose Android-2.3] # prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/ARM-Eabi-G ++-nostdlib-bdynamic-wl,-t, build/CORE/armelf. x-wl,-dynamic-linker,/system/bin/linker-wl, -- GC-sections-wl,-Z, nocopyreloc-O out/target/product/generic/obj/executables/helloworld_intermediates/linked/helloworld-lout/target/product/generic/obj/lib-wl, -rpath-link = out/target/product/generic/obj/lib-LC-lstdc ++-lm out/target/product/generic/obj/lib/crtbegin_dynamic.o out/Target /product/generic/obj/executables/helloworld_intermediates/hello. o-wl,-Z, noexecstack-wl, -- no-undefined prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin /.. /lib/GCC/ARM-Eabi/4.4.3/libgcc. A out/target/product/generic/obj/lib/crtend_android.o
STRIP using out/host/linux-x86/bin/soslim Tool[Plain]
View plaincopyprint?
- [Root @ fontlose Android-2.3] # out/host/linux-x86/bin/soslim -- strip -- shady -- quiet out/target/product/generic/symbols/system/bin/helloworld -- outfile out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld
[Plain]
View plaincopyprint?
- [Root @ fontlose Android-2.3] # adb push out/target/product/generic/symbols/system/bin/helloworld/data
- 145 KB/s (7608 bytes in 0.051 s)
- [Root @ fontlose Android-2.3] # adb shell
- /# Cd/data/
- /Data # ls-l
- -Rwxrwxrwx root 7608 helloworld
- /Data #./helloworld
- Hello World!
Android does not use glibc as the C library, but uses the Bionic Libc developed by Google. it is feasible to use the general Toolchain to compile and transplant the Android Linux kernel, because the kernel does not need the C library, however, when developing Android applications, other Toolchain-compiled applications can only run in static compiling mode, and the execution files compiled in static mode are relatively large.