PocketMagic

PocketMagic

Where Technology meets magic

PocketMagic RSS Feed
 

Android C native development – take full control!

By Radu Motisan Posted on May 12th, 2009

A recent project I've been working on required Bluetooth programming on the Android.
Having a quick look over the latest 1.5 Android SDK, I could see that bluetooth support was missing. Later research pointed out that Google expressively excluded Bluetooth APIs blaming lack of time.

On the other hand, I'm not a big Java fan, to say the least. I find Java unfriendly, and I don't like the look and feel of Java apps. Sorry, I prefer C/C++ since it provides better control and flexibility. So it's time to start doing C/C++ native applications for the Google Android.

How run a C program on Google Android?
First thing I'll show here, is to compile a simple C program for the Android.


#include <stdio.h>
int main()
{
printf("Hello Google Android world!\nwww.pocketmagic.net\n");
return 1;
}

Save this program as test.c. In the next steps we'll be compiling this sample for the Google Android using gcc.

The tools

1. Download ubuntu linux. I currently use Desktop edition 9.04 in a virtual machine.
Attention: You'll need to install Ubuntu on a machine with at least 1.5GB Ram, 10GB ext2 partition and 2GB Swap partition or you won't be able to use this tutorial's info (less then the minimum requirements will result in the impossibility of compiling the Android Code and we need that for the libraries).

2. Once Ubuntu is installed, download the Android Source code. On your linux box install additional packages:
$ sudo apt-get install git-core gnupg sun-java5-jdk flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl libncurses5-dev zlib1g-dev
$ sudo apt-get install valgrind

This one is only for Ubuntu Intrepid users:

$ sudo apt-get install lib32readline5-dev

3. Install Repo
Create a ~/bin directory in your home directory, and check to be sure that this bin directory is in your path:

$ cd ~
$ mkdir bin
$ echo $PATH

To add it to the Path, you can use:

$ PATH=$PATH:~/bin/

Download the repo script and make sure it is executable:

$ curl http://android.git.kernel.org/repo >~/bin/repo
$ chmod a+x ~/bin/repo

4. 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. You must specify a URL for the manifest:

$ repo init -u git://android.git.kernel.org/platform/manifest.git
$ repo init -u git://android.git.kernel.org/platform/manifest.git -b cupcake

When prompted, configure Repo with your real name and email address. If you plan to submit code, use an email address that is associated with a Google account.
A successful initialization will end with a message such as

repo initialized in /mydroid

Your client directory should now contain a .repo directory where files such as the manifest will be kept.

5. Getting the files
To pull down files to your working directory from the repositories as specified in the default manifest, run

$ repo sync

For more about repo sync and other Repo commands, see Using Repo and Git.
The Android source files will be located in your working directory under their project names.

6.Verifying Git Tags
Load the following public key into your GnuPG key database. The key is used to sign annotated tags that represent releases.

$ gpg --import

then paste the key(s) below, and press Control-D to end the input and process the keys.

key 9AB10E78: "The Android Open Source Project <initial-contribution@android.com>"
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

mQGiBEnnWD4RBACt9/h4v9xnnGDou13y3dvOx6/t43LPPIxeJ8eX9WB+8LLuROSV
lFhpHawsVAcFlmi7f7jdSRF+OvtZL9ShPKdLfwBJMNkU66/TZmPewS4m782ndtw7
8tR1cXb197Ob8kOfQB3A9yk2XZ4ei4ZC3i6wVdqHLRxABdncwu5hOF9KXwCgkxMD
u4PVgChaAJzTYJ1EG+UYBIUEAJmfearb0qRAN7dEoff0FeXsEaUA6U90sEoVks0Z
wNj96SA8BL+a1OoEUUfpMhiHyLuQSftxisJxTh+2QclzDviDyaTrkANjdYY7p2cq
/HMdOY7LJlHaqtXmZxXjjtw5Uc2QG8UY8aziU3IE9nTjSwCXeJnuyvoizl9/I1S5
jU5SA/9WwIps4SC84ielIXiGWEqq6i6/sk4I9q1YemZF2XVVKnmI1F4iCMtNKsR4
MGSa1gA8s4iQbsKNWPgp7M3a51JCVCu6l/8zTpA+uUGapw4tWCp4o0dpIvDPBEa9
b/aF/ygcR8mh5hgUfpF9IpXdknOsbKCvM9lSSfRciETykZc4wrRCVGhlIEFuZHJv
aWQgT3BlbiBTb3VyY2UgUHJvamVjdCA8aW5pdGlhbC1jb250cmlidXRpb25AYW5k
cm9pZC5jb20+iGAEExECACAFAknnWD4CGwMGCwkIBwMCBBUCCAMEFgIDAQIeAQIX
gAAKCRDorT+BmrEOeNr+AJ42Xy6tEW7r3KzrJxnRX8mij9z8tgCdFfQYiHpYngkI
2t09Ed+9Bm4gmEO5Ag0ESedYRBAIAKVW1JcMBWvV/0Bo9WiByJ9WJ5swMN36/vAl
QN4mWRhfzDOk/Rosdb0csAO/l8Kz0gKQPOfObtyYjvI8JMC3rmi+LIvSUT9806Up
hisyEmmHv6U8gUb/xHLIanXGxwhYzjgeuAXVCsv+EvoPIHbY4L/KvP5x+oCJIDbk
C2b1TvVk9PryzmE4BPIQL/NtgR1oLWm/uWR9zRUFtBnE411aMAN3qnAHBBMZzKMX
LWBGWE0znfRrnczI5p49i2YZJAjyX1P2WzmScK49CV82dzLo71MnrF6fj+Udtb5+
OgTg7Cow+8PRaTkJEW5Y2JIZpnRUq0CYxAmHYX79EMKHDSThf/8AAwUIAJPWsB/M
pK+KMs/s3r6nJrnYLTfdZhtmQXimpoDMJg1zxmL8UfNUKiQZ6esoAWtDgpqt7Y7s
KZ8laHRARonte394hidZzM5nb6hQvpPjt2OlPRsyqVxw4c/KsjADtAuKW9/d8phb
N8bTyOJo856qg4oOEzKG9eeF7oaZTYBy33BTL0408sEBxiMior6b8LrZrAhkqDjA
vUXRwm/fFKgpsOysxC6xi553CxBUCH2omNV6Ka1LNMwzSp9ILz8jEGqmUtkBszwo
G1S8fXgE0Lq3cdDM/GJ4QXP/p6LiwNF99faDMTV3+2SAOGvytOX6KjKVzKOSsfJQ
hN0DlsIw8hqJc0WISQQYEQIACQUCSedYRAIbDAAKCRDorT+BmrEOeCUOAJ9qmR0l
EXzeoxcdoafxqf6gZlJZlACgkWF7wi2YLW3Oa+jv2QSTlrx4KLM=
=Wi5D
-----END PGP PUBLIC KEY BLOCK-----

After importing the keys, you can verify any tag with

$ git tag -v tagname

7. Building the code
The Android code contains a bug that hasn't been solved up to the date of this article.
So before you start compiling the code, you'll need a few modifications, or the build will fail (after consuming some of your time and patience). The error is:

external/qemu/sockets.c: In function 'sock_address_init_resolve':
external/qemu/sockets.c:637: error: 'EAI_NODATA' undeclared (first use
in this function)
external/qemu/sockets.c:637: error: (Each undeclared identifier is
reported only once
external/qemu/sockets.c:637: error: for each function it appears in.)
make: *** [out/host/linux-x86/obj/EXECUTABLES/emulator_intermediates/
sockets.o] Error 1

To fix this, before compiling the android code, open ~/mydroid/external/qemu/sockets.c and add

#define __USE_GNU

just before the #include <netdb.h>

Now you can build the files. Run make from within your working directory:

$ cd ~/mydroid
$ make

On my virtual machine running ubuntu, the build process took several hours. The host PC is a 2.6GHz P4.

Compile test.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. You can download the original file here, and modify

my $TOOLCHAIN = "$DROID/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1";

to

my $TOOLCHAIN = "$DROID/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1";

Or download this updated version directly.

Copy agcc to your home directory, and chmod it:

chmod +x agcc

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

$ PATH=$PATH:~/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

See the pics for more details:

Next thing to do is to try to control the bluetooth functionality using native C code and the android toolchain.

More on native C apps for Android here.

Part of this article contains information presented by Google on http://source.android.com/download

Best regards,
Radu Motisan

By Radu Motisan

44 Responses to “Android C native development – take full control!”

  1. 1
    Anders:

    Great. Thanks for the instructions.
    Any idea how start your C program when the system boots up, or after it boots up?

  2. 2
    Ash09:

    Hi
    when i run the following on my Ubuntu 9.04:
    $ sudo apt-get install lib32readline5-dev
    I am getting the following error:
    E: Couldn’t find package lib32readline5-dev
    how did u resolve this?

  3. 3
    Radu Motisan:

    That’s only for ubuntu intrepid users.

    You won’t need it in 9.04 desktop.

    I’ve modified the post to reflect this as well.

  4. 4
    Dorian:

    Hi,

    Thanks for the instructions.
    Just a little modification, you need to change agcc, as the toolchain is not correct libgcc.a is not anymore in interwork folder, so 2 lines needs to be changed too.

    “$TOOLCHAIN/lib/gcc/arm-eabi/4.3.1/libgcc.a”, on line 117 and 132
    Thanks

  5. 5
    Stephan:

    Great. works like a charm.
    @Dorian: thanks for the correction.

  6. 6
    Anupam:

    Hi,
    I have the environment for building a C code for android 1.0 in the ubuntu machine. Now I have downloaded Android 1.5 SDK for linux and want to compile the same C code for Android 1.5. What changes I have to make ?

  7. 7
    marius:

    Hi,
    I am using android X86 (taken on 10 oct ‘09).
    I get: adb: not found

    Then. I’ve mounted the resulted system.img, copied explicit the *hello in
    /system/bin and data/, then I’ve build the iso and boot it up.
    I can see my hello, but.

    #./hello
    hello: not found

    #hello
    hello: not found

    #adb
    adb not found

    The shell is dumb or….

  8. 8
    Radu Motisan:

    hi marius,
    what output do you get from ls -l
    ?
    also, what do you mean by “then I’ve build the iso and boot it up.”

  9. 9
    To build the environment for developing Android for freerunner | allenc.tw:

    [...] 相關資料:  Link1、Link2、Link3 [...]

  10. 10
    Perumal:

    I did exactly the same as mentioned here but keep on getting the following error:

    “/mydroid/out/target/product/generic/obj/lib/crtbegin_dynamic.o: In function `_start’:
    bionic/libc/arch-arm/bionic/crtbegin_dynamic.S:(.text+0×14): undefined reference to `main’
    collect2: ld returned 1 exit status”

    Any idea how to resolve it?

    Thanks in Advance.

  11. 11
    Radu Motisan:

    Make sure you’ve put the following folders in the path:

    $DROID/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1

    $DROID/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1

  12. 12
    Perumal:

    The both folders are in the path but I am still getting the same error. It was working well few months back but now when I tried again I am getting the above error.

  13. 13
    Tayefeh:

    Hey, that’s really great! Just checked it out and it worked … now I can port all my ancient C programms to android lol

  14. 14
    Radu Motisan:

    Hey there Tayefeh, funny to see that isn’t it?
    The truth is you can make sore very powerful apps using the JNI.
    I’ve implemented a complete bluetooth l2cap interface for Java using it. Will post more soon.

  15. 15
    mbt:

    I like the idea, cool!

  16. 16
    lea:

    Very nice explanation! you saved my time. All in one place solution! Thanks!

  17. 17
    Radu Motisan:

    I’m glad you like it. Make sure you read about JNI as well: it provides some more options.

  18. 18
    lea:

    One more thing – I’m trying to run a sample ‘hello world’ program using a .cpp file instead of .c file. Compiler complains of not finding iostream.h. Could you tell me how to use your script to call a c++ file?

  19. 19
    a:

    Thanks for taking the time to provide this information ;-) It’s most helpful

  20. 20
    Radu Motisan:

    You’re welcome, a .

  21. 21
    Kaushik:

    Can you write the same agcc script for mips based system?

  22. 22
    Radu Motisan:

    May I ask, what MIPS android hardware do you have?

  23. 23
    Kaushik:

    I have MIPS Malta board with MIPSr2

  24. 24
    Kaushik:

    I already have android running on it but my 3rd party software which is generally used in glic environment will not work under bionic based android. So I was wondering can we make such wrapper for mips based systems.

    -Kaushik

  25. 25
    sabin:

    Can,t install additional packages of android in ubuntu 10.4

  26. 26
    Tanis:

    Hi Radu! Nice guide, but i have a problem with the agcc compiler:

    ~$ agcc helloword.c -o helloword
    arm-eabi-gcc: /home/tanisdlj/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1/lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a: No such file or directory

    This is my PATH:

    tanisdlj@tanisdlj-andev:~$ echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/tanisdlj/bin/:/home/tanisdlj/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1/bin:/home/tanisdlj/:/home/tanisdlj/mydroid/

    I follow your guide step by step, but i have this problem. Any suggestion? >.<

  27. 27
    Suhas:

    Hi Radu

    I followed all the steps and in the final step
    agcc hello.c -o hello I get this error

    /localhome/user/bin/mipsandroid/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1/bin/../lib/gcc/arm-eabi/4.3.1/../../../../arm-eabi/bin/ld: /localhome/user/bin/mipsandroid/out/target/product/generic/obj/lib/crtbegin_dynamic.o: Relocations in generic ELF (EM: 8)
    /localhome/user/bin/mipsandroid/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1/bin/../lib/gcc/arm-eabi/4.3.1/../../../../arm-eabi/bin/ld: /localhome/user/bin/mipsandroid/out/target/product/generic/obj/lib/crtbegin_dynamic.o: Relocations in generic ELF (EM: 8)
    /localhome/user/bin/mipsandroid/out/target/product/generic/obj/lib/crtbegin_dynamic.o: could not read symbols: File in wrong format
    collect2: ld returned 1 exit status

    I have set the PATH too correctly. Kindly please help me.

  28. 28
    Suhas:

    /localhome/user/bin/mipsandroid/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1/bin/../lib/gcc/arm-eabi/4.3.1/../../../../arm-eabi/bin/ld: /localhome/user/bin/mipsandroid/out/target/product/generic/obj/lib/crtbegin_dynamic.o: Relocations in generic ELF (EM: 8 )
    /localhome/user/bin/mipsandroid/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1/bin/../lib/gcc/arm-eabi/4.3.1/../../../../arm-eabi/bin/ld: /localhome/user/bin/mipsandroid/out/target/product/generic/obj/lib/crtbegin_dynamic.o: Relocations in generic ELF (EM: 8 )
    /localhome/user/bin/mipsandroid/out/target/product/generic/obj/lib/crtbegin_dynamic.o: could not read symbols: File in wrong format
    collect2: ld returned 1 exit status

  29. 29
    mavci:

    sabin
    I installed in ubuntu 10.04 and now I can compile c files for android donut 1.6
    if you want I can explain

  30. 30
    mavci:

    I compiled a test.c for and run on adb shell but how to run on my android phone?(DSTL1)

  31. 31
    Radu Motisan:

    hello , please do, I think that will help the community.

  32. 32
    Radu Motisan:

    mavci, you can use the terminal application for android.

  33. 33
    mavci:

    thanks Radu!
    I will write all explainations tomorrow.

  34. 34
    Radu Motisan:

    Ok, then I can add them to this post, so others can use it too.

  35. 35
    quincy:

    this is so dope. I can compile all kinds of console programs now!

    Dude, you rock!

  36. 36
    quincy:

    @Tanis: you mixed-up your EABI versions in your PATH; there are references to 4.2.1 and 4.3.1

  37. 37
    Radu Motisan:

    glad you like it, Quincy .

  38. 38
    Chris:

    Can’t seem to get the gpg –import section to work. How exactly do you paste the key in?

  39. 39
    qds:

    This tutorial is cool, but I have problems when I try to compile static

    when I add -static directive I get error: “cannot find -lc” “collect2: ld returned 1 exit status”

    do you know how to solve this problem? Thanks

    regards

  40. 40
    Anup:

    Gcc 4.3 is required for building android. 4.4 has strict has more strict rules so it will through fprintf declaraion errors..

    To fix,

    $ sudo apt-get install gcc-4.3 g++-4.3
    $ sudo rm /usr/bin gcc
    $ sudo rm /usr/bin g++
    $ sudo ln -s gcc-4.3 gcc
    $ sudo ln -s g++-4.3 g++

  41. 41
    Radu Motisan:

    Hello Anup, Thanks for your contribution!

  42. 42
    android Phone:

    I am not at all a technical person, so when it comes to understanding code, this feels foreign to me. However, from what I’ve read, you’ve found a way to include blue tooth functionality with C++? That’s actually kind of cool in my opinion. If you don’t have bluetooth, you make it yourself.

  43. 43
    android trickery exploit for root:

    [...] Android C native development – take full control! [...]

  44. 44
    Radu Motisan:

    See this NDK/JNI tutorial as well:

    http://www.pocketmagic.net/?p=1332

Leave a Reply

Please copy the string k4rd8z to the field below:

Blog options

Translator

English flagFrench flagGerman flagItalian flagPortuguese flagRussian flagSpanish flag
By N2H

Categories

Archives

Tags

Sponsored Links

Subscribe!

Get more followers

Our Sponsors