使用NDK Build Android上的busybox


在Android上,为了某些需要,我们需要一个小而精减的busybox(如果不懂busybox是什么),请跳过此文。当然我们也可以用gcc的toolchains来build, 但生成出来的那个二进制文件的size会让你疯狂。而用NDK生成出来的二进制则是gcc生成的五分之一左右。我做过试验,同样一个busybox的配置,gcc生成的busybox是460k左右,NDK生成出来的则是84k。

如果研究过NDK,可以看到他有一个toolchains目录,这目录里装的就是用来编译生成能在android上运行的程序的编译器。例如目录

/opt/android-ndk-r6/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin


这目录下就是编译器,而编译链接需要的头文件和库则在对应的平台的目录里,例如

/opt/android-ndk-r6/platforms/android-9/arch-arm/usr

下的include和lib。

下面开始正题, 首先需要哪些环境:

1. Linux系统环境,我这里是(Ubuntu 10.x),你也可以用一个虚拟机,我就是用的虚拟机。

2. 安装了gcc,make,和NDK,可以从上面内容看到,我用的是r5c版。这些怎么配置这里略过,网上一搜一大把。

3. 从上下载busybox源代码

开始

1. 將/opt/android-ndk-r6/platforms/android-9/arch-arm/usr 下的include和lib覆盖拷贝/opt/android-ndk-r6/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/arm-linux-androideabi目录下。这里用哪个平台的头文件和库你自己可以看着办。差别不大。

2. 解压busybox包,然后进入解压后的busybox目录

3. make menuconfig

在配置里,有几项是要选上的。

Busybox Settings->Build Options->

[*] Build BusyBox as a static binary (no shared libs)
Cross Compiler prefix: /opt/android-ndk-r6/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-

Busybox Settings->General Configuration->

[*] Enable options for full-blown desktop systems                    │ │ 
  │ │[ ] Provide compatible behavior for rare corner cases (bigger code)  │ │ 
  │ │[*] Enable obsolete features removed before SUSv3                    │ │ 
  │ │[ ] Avoid using GCC-specific code constructs                         │ │ 
  │ │[*] Enable Linux-specific applets and features                       │ │ 
  │ │    Buffer allocation policy (Allocate with Malloc)  --->            │ │ 
  │ │[*] Show terse applet usage messages                                 │ │ 
  │ │[*]   Show verbose applet usage messages                             │ │ 
  │ │[*]   Store applet usage messages in compressed form                 │ │ 
  │ │[*] Support --install [-s] to install applet links at runtime        │ │ 
  │ │[*] Don't use /usr                                                   │ │ 
  │ │[ ] Enable locale support (system needs locale for this to work)     │ │ 
  │ │[ ] Support Unicode                                                  │ │ 
  │ │[*] Support for --long-options                                       │ │ 
  │ │[*] Use the devpts filesystem for Unix98 PTYs                        │ │ 
  │ │[ ] Clean up all memory before exiting (usually not needed)          │ │ 
  │ │[ ] Support wtmp file                                                │ │ 
  │ │[ ] Support utmp file                                                │ │ 
  │ │[ ] Support writing pidfiles                                         │ │ 
  │ │[ ] Support for SUID/SGID handling                                   │ │ 
  │ │[ ] Support NSA Security Enhanced Linux                              │ │ 
  │ │[ ] exec prefers applets                                             │ │ 
  │ │(/proc/self/exe) Path to BusyBox executable  


剩下的其他的按自己的需要来选配,

4.按ESC退出,最后会问你是否保存配置,别多按ESC,会不保存的。选择yes按回车保存。

5. 修改Makefile.flags.去 crypt库的链接,当然一些用到crpyt库的功能也得从第3步的配置里去掉,例如密码之类的。

  • 1
  • 2
  • 下一页

相关内容