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 "http://php.webtutor.pl/en/wp-content/uploads/2011/09/repo" > ~/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://codeaurora.org/platform/manifest.git -b gingerbread_house -m M7630AABBQMLZA414001.xml

4. Getting the Android source tree

repo sync


source build/envsetup.sh
lunch full-eng
make

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:
~/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1/bin:~/:~/mydroid/

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
./test

No comments:

Followers

Blog Archive

About Me

My photo
HD Multimedia Technology player