Friday, October 7, 2011

Gingerbread Android C/C++ Programming on Ubuntu 11.04

For using open source libraries or developing a device driver and a game, native C or C++ programming sometime has to be applied. Google suggests Android NDK should be used for this purpose. The following visual tutorials are very helpful:
Android developers website showed some examples. Frank Ableson wrote a tutorial to reuse existing C code with the Android NDK. Eclipse and Android Developer Tools are suggested to be used.

Charles Wilde also gave a very good tutorial, "Android Native Development Using the Android Open Source Project".

credentiality showed a simple way to do native C program using NDK after downloading Android SDK and NDK. He applied a perl script agcc to setup build environment.

In 2009 Radu Motisan posted "Android C native development – take full control!" which is very interesting to me. Follow this instruction, I installed Ubuntu 11.04 on a virtual machine and tried a hello.c on Gingerbread Android. Here was how I did:

1. Prepare additional packages for Ubuntu 11.04.

(1) Install Sun java6

sudo add-apt-repository ppa:ferramroberto/java
sudo apt-get update
sudo apt-get install sun-java6-jre sun-java6-jre-plugin sun-java6-jdk

(2) Install other packages

sudo apt-get install git-core gnupg flex bison gperf libsdl1.2-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl libncurses5-dev zlib1g-dev

2. Install repo

Make sure you have a bin/ directory in your home directory, and that it is included in your path:

mkdir ~/bin

in .bashrc add

export PATH=~/bin:${PATH}

And source .bashrc.

Download the Repo script and ensure it is executable:

curl "" > ~/bin/repo
chmod a+x ~/bin/repo

3. Initializing a Repo client

Create an empty directory to hold your working files:

mkdir mydroid
cd mydroid

Run repo init to bring down the latest version of Repo with all its most recent bug fixes. We must specify a URL for the manifest. I tried the code aurora repo mirror site.

repo init -u git:// -b gingerbread_house -m M7630AABBQMLZA414001.xml

4. Getting the Android source tree

repo sync

source build/
lunch full-eng

6. Compile Hello.c with gcc for the Android platform

Android uses a simplified version of libc, called bionic. We need to compile using Android's prebuilt cross-compiler arm-eabi-gcc, and use the bionic library on the phone. The easy way to do this is to use the agcc perl wrapper. We also need to change the libgcc.a path from 4.2.1 to 4.3.1. Put this agcc in ~/bin and make it executable.

chmod +x agcc

Then set the PATH to the bionic libs and the agcc location:

Now you can compile the test.c file:
agcc test.c -o test

Take the resulting test binary and upload it to your android using:

adb push test /data/local/test

Then run it:

adb shell
chmod 775 test

1 comment:

The Geeks said...

hi..Im college student, thanks for sharing :)


Blog Archive

About Me

My photo
HD Multimedia Technology player