Use ndk to build busybox on Android.

Source: Internet
Author: User

On Android, we need a small and refined busybox for some purposes (if you do not know what busybox is), skip this article. Of course, we can also use the toolchains of GCC to build, but the size of the generated binary file will make you crazy. The binary data generated using ndk is about 1/5 generated by GCC. I have tried it. In the same busybox configuration, the busybox generated by GCC is about K, and the ndk is about 84 K.

If you have studied ndk, you can see that it has a toolchains Directory, which contains the compiler used to compile the program that can run on Android. For example, directory

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

This directory is the compiler, And the header files and libraries required for the compilation link are in the directory of the corresponding platform, such

/Opt/android-ndk-r6/platforms/Android-9/arch-arm/usr

Include and Lib.

Which of the following environments are required first:

1. Linux system environment. Here I am (Ubuntu 10.x). You can also use a virtual machine.

2. Install GCC, make, and ndk. You can see from the above content that I use r5c. How to configure these items is skipped here.

3. Download The busybox source code from www.busybox.net.

Start

1. replace include and Lib under/opt/android-ndk-r6/platforms/Android-9/arch-arm/usr with/opt/android-ndk-r6/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/ARM-Linux -Under the androideabi directory. The header files and libraries of which platform can be used here. The difference is not big.

2. decompress the busybox package and enter the extracted busybox directory.

3. Make menuconfig

There are several items to be selected in the configuration.

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

The remaining items are selected based on your needs,

4. Press ESC to exit. At last, you will be asked if you want to save the configuration. Do not press ESC to save the configuration. Select Yes and press enter to save.

5. Modify makefile. Flags. Go to the link of the crypt library. Of course, some functions that use the crpyt library must be removed from step 1 configuration, such as passwords.

6. Make

During this period, some compilation errors may occur.

The sys/user cannot be found. h error I put/opt/android-ndk-r6/tmakeoolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/ARM-Linux-androideabi/include/Linux/user. h copy

/Opt/android-ndk-r6/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/ARM-Linux-androideabi/include/sys.

Another error that does not find sys/KD. H is also handled.

We also encountered a definition that could not find the strchrnul function. I replaced it with strchr. For example, Editor/sed. c

// Unsigned idx = strchrnul (pai_letters, sed_cmd-> cmd)-pai_letters; // original code

// Replace the code.

Char * tmp_p = strchr (pai_letters, sed_cmd-> cmd );
Unsigned idx = tmp_p? Tmp_p-names _letters: strlen (tmp_p );

There is also libbb/obscure. C line 113, comment out the three lines.

If an error occurs during compilation and cannot be solved, deselect the corresponding compilation error tool and make it again, for example, Editors/VI. c. If a function cannot be found, I reconfigure busybox and remove VI from editors. Then re-compile.

I also removed init utilities-> bootchartd, poweroff, halt, and reboot,

Busybox settings-> busybox library tuning->

[] Command line editing

Of course, it is best to select only coreutils.

7. busybox is generated and copied to the Android mobile phone.

Note: In fact, all compilation errors can be solved. Replace strchrul with strchr and fputs_unlocked with other functions. If you are interested, you can study it on your own.

These errors are caused by differences between the header files and libraries in ndk and the header files in GNU gcc, or by removing some files.

Thank you.

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.