使用NDK build android上的busybox.

來源:互聯網
上載者:User

在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. 從www.busybox.net上下載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步的配置裡去掉,例如密碼之類的 。

6.make

這期間會有些編譯錯誤

我碰到過一個找不到sys/user.h的錯誤,我把/opt/android-ndk-r6/tmakeoolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/arm-linux-androideabi/include/linux/user.h拷貝到

/opt/android-ndk-r6/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/arm-linux-androideabi/include/sys下搞定。

還有一個 找不到sys/kd.h的錯誤也同樣處理

還碰到過一個找不到strchrnul函數的定義。我用strchr替代之。例如editor/sed.c

//      unsigned idx = strchrnul(cmd_letters, sed_cmd->cmd) - cmd_letters; //原代碼

// 替換代碼。

        char* tmp_p = strchr(cmd_letters, sed_cmd->cmd);
        unsigned idx = tmp_p ? tmp_p - cmd_letters : strlen(tmp_p);

還有libbb/obscure.c line 113,注釋掉那3行。

如果編譯碰到錯誤無法解決時,把對應編譯出錯的工具打勾去掉再重新make,例如editors/vi.c有個函數找不到,我就重新設定了一下busybox,把Editors裡的vi給去掉了。然後重新編譯。

我還去掉了Init Utilities->bootchartd, poweroff,halt, and reboot,

Busybox Settings->Busybox Library Tuning->

[ ] Command line editing

當然最好是只選CoreUtils

7.產生出來了busybox,將其拷貝到android手機上,就能用了。

備忘: 其實那些編譯錯誤都是可以解決的,用strchr替換strchrul, 還有用其他函數替代fputs_unlocked,大家有興趣可以自己研究下。

這些錯誤都是因為NDK裡的標頭檔和庫與GNU GCC的標頭檔有些差異或者給去掉了一些 檔案導致的。

謝謝

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.