PocketMagic

May 12, 2009

Android C native development – take full control!

Filed under: Android, Research, Software, Windows Mobile — Tags: , , , — radumotisan @ 2:32 pm

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
$ 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. After importing the keys, you can verify any tag with

$ git tag -v tagname


key 9AB10E78: "The Android Open Source Project "

-----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-----

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.

Best regards,
Radu Motisan

Developing for Google Android – the SDK

Filed under: Android, Software — Tags: , , — radumotisan @ 1:23 pm

Following the recent posts on the Google Android platform, I thought it is time to go a little deeper in the programming world of this mobile device.

So the questions is:
- What do we need to start coding for Google Android?

Lucky for us, there are lots of tools available online, and the Android team has provided a comprehensive guide on this. You can read it here.

In just a few words:
1. you will be needing Eclipse, get it here. I’m currently using (actually I’m currently having installed, since I’ve moved to native C/C++ Android development), Eclipse IDE for Java EE Developers (163 MB) , jee-ganymede-SR2. Download it and install it.
2. the Google Android SDK, get it here . Current version at this time is 1.5.r1. To install the SDK you simply need to unzip it to a location on your PC. You will have a folder named android-sdk-windows-1.5_r1, and inside several subfolders (add-ons, docs, platforms, tools, usb_driver). Keep this structure intact.
3. Open Eclipse (start eclipse.exe from the installation folder), and go to menu, Help->Software Updates->Available Software->Add Site. Type in this address: http://dl-ssl.google.com/android/eclipse/, and press OK. Click the newly added check boxes and press Install. On the following Install window, “Android DDMS” and “Android Development Tools” must both be checked. Click Next/Accept/Finish and restart Eclipse when prompt to do so.

4. Add the SDK to Eclipse. Again in Eclipse menu, Click Window->Preferences->Android and browse to the location of the SDK folder you’ve unzipped earlier. Then click apply / ok.

First Android application
A good idea for a start is to try to compile one of the SDK samples. Android SDK comes with several samples developed in JAVA. Let’s see how we can get one running.
1. Open Eclipse, go to menu File -> New ->Project , select Android Project and click next. Now give it a name, eg. TestHello, and select “Create project from existing source”, and browse to %SDK_PATH%/platforms/android-1.5/samples select HelloActivity folder and click OK. You should get the source code imported.

2. Go to Menu Project -> uncheck Build automatically. Now you can right click your project name in the project explorer and select Build Project.
3. Go to \android-sdk-windows-1.5_r1\platforms\android-1.5\samples\HelloActivity\bin\ and locate TestHello.apk . This is the installer for your newly compiled app.
4. Using adb, from SDK tools, install the application:
adb install TestHello.apk
Next you can locate it in Android programs , and execute it with a simple tap.

Next you can modify the code, and learn new things. Hope you find this start-up useful.

Radu Motisan

May 6, 2009

Developing for Google Android – update the OS version

Filed under: Android, Software — Tags: , , , — radumotisan @ 7:43 pm

There are already some very good articles and tips and tricks on how to do this, but for the purpose of having my own information structured and available online, I will describe what you need to do to update the OS version of your Android.

In no event will I be liable to you for any damages arising from the use of this info, so its only your responsibility if you decide to do the upgrade. You might loose the warranty or even make your Android unusable.

On the other hand, being able to update the OS, brings several advantages, especially for developers. You can root your device, meaning that you can get a way of using your Android linux box as root, very useful, since it provides complete access to the device.

Also, if you hard-reset the device and don’t have a T-Mobile SIM with data plan, you’ll need to use this article to unlock your Android G1.

STEP 1: Flash your Android with OS version RC29. This is an older release, but we need it to take advantage of an exploit that will give us root.
Download the rom image (~90MB).
Copy DREAIMG.nbh to the root of a micro SD card. The file should appear like this:

STEP 2: Insert the MicroSD in the android, and turn off the phone (you can unplug the battery and then replug it). Hold the Camera button, and press Power button to entry the bootloader. You should get a white screen with instructions to flash your phone with the update on your SD card.
Follow those instructions and press the Power button. The update will start and it looks like this:

Be careful not to interrupt this update!
When the update is complete, restart the phone.

STEP 3: You should now be running the RC29 OS version, and if you don’t have a SIM card in your Android, you should get this screen:

RC29 (and RC7 as well) have an interesting vulnerability that we’ll use for our purposes: all the characters you type get executed in a background shell as ROOT!
For instance type [enter]restart[enter] w/o the square brackets . The device will reboot.
To take advantage of this and unlock your device type:
[enter]telnetd[enter] (again, no square brackets, [enter] refers to the enter key on the Android keyboard). This will start the telnet daemon process (you won’t see it as it is running in the background).

STEP 4: Connect your Android to the PC and use adb shell to open a remote shell command prompt. Here’s an article showing how to do that.
If you get an error (device not found), you’ll need to start the ADB server on your android. Do do that we’ll use the same background shell exploit – simply use the keyboard and type:
[enter]setprop persist.service.adb.enable 1[enter]
Retry connecting with adb.

STEP 5: Download busybox and use adb push to copy busybox binary to your G1:
adb push busybox /data/local/busybox
In the adb shell, type chmod 755 /data/local/busybox to make it executable and then /data/local/busybox telnet 127.0.0.1.

Congrats, YOU Got root!

STEP 6: Disable registration and enable WLAN connectivity. Type in the adb shell:
setprop app.setupwizard.disable 1
and then:
am start -a android.intent.action.MAIN -n com.android.settings/.Settings (works without root as well)

Now you can use your device and you can stop here. But if you want more, hit continue :)

Flash to latest OS build, but keep root access:
STEP 7: Copy recovery.img and update.zip (this is the Hard SPL) to the root of your MicroSD card.

STEP 8: using adb shell as root (at the # prompt), type the following commands:
mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system
cd sdcard
flash_image recovery recovery.img
cat recovery.img > /system/recovery.img

STEP 9: Install update.zip form sdcard (restart device while pressing HOME, then ALT+L, and finally Alt+s)

STEP 10: Upgrade the Radio. Download Radio ROM zip (9 MB) , rename to update.zip and put it on the SDCard and install it (restart device while pressing HOME, then ALT+L, and finally Alt+s)

STEP 11: Upgrade the OS ROM to latest version. Download OS ROM zip (42 MB) , rename to update.zip and put it on the SDCard and install it (restart device while pressing HOME, then ALT+L, and finally Alt+s)

Try to carefully follow these steps. It will keep you out of trouble. Should you have any questions, use the Comments form below.

Radu Motisan

Developing for Google Android – introduction

Filed under: Android, Software — Tags: , , — radumotisan @ 11:46 am
Android, the mobile platform developed by Google, offers a robust communicator with some exciting features.
Taking Windows Mobile as a reference, the Android comes with some powerful new concepts:
- smooth functionality – no more wait icons, the unit is quite fast
- fingerfriendly everything, especially the Mobile browser is by far better then the WM PIE. Now I can actually USE the mobile internet browser
- some google specific services like google maps, IM, and Gmail, easy to use as well
- slim design, including a full keyboard
- long battery life

All in one, this finally is what a mobile companion should be like, meaning that it does what it’s advertised to do – thanks to the way the interface is organized, and the touchscreen, it allows easy and painless interaction with the device. Direct information interchange between the user and the device, taking advantage of the small size. No more need for a sharp stylus, and this is wow!

For this article, here are a few things I’d like to show:
- how to connect the Android to your Windows PC
- how to take screen captures from your Android
- how to install new software on your mobile device

1) First thing after you unbox your device, you need to download a set of tools provided by google.
These tools are for developers, and come as an SDK package, but anyone can install and use them.
So go to this link and click the latest version for the Windows SDK (at the time I wrote this, it was android-sdk-windows-1.5_r1.zip)
2) Once you got the file, unzip it to a local folder on your PC. You should have a folder like android-sdk-windows-1.5_r1.
3) Connect the Android to the PC using the USB cable. When notified that a new hardware has been detected, instruct Windows to look for drivers into the SDK folder: android-sdk-windows-1.5_r1/usb_driver/x86
If you have a micro SD card in your Android, it will appear as a new drive on your PC. Sweet.

Things you can do now: install new software, remove unwanted packages, get a shell on your Android (yes, it’s an embedded linux box), or take screen captures (useful for reviews).

So to take a screen capture of your Android, go to the SDK folder and enter Tools. Start ddms.bat .
This utility will allow you to perform various control tasks:

To take a screenshot, go to the Menu, click Device and select “Screen Capture” or directly hit Control+S. Here’s what you get:

You can save the file as a PNG image file, and you can also see that the screen has a resolution of 480×320.

To install new software, make sure you’re in the same Tools folder of the SDK package (using Total Commander), and type cmd and hit enter. You’ve just opened a command prompt. Make sure you know where the application installer is (the .apk file) then type:
adb install [APK_FILE_PATH]

And here is the installed software running on the Android G1:

In a similar way you can perform other commands:
adb devices should list the ID of the Android connected to your PC.
adb push [LOCAL_FILE_PATH] [REMOTE_FILE_PATH] will upload a file from the PC to the Android
adb pull [REMOTE_FILE_PATH] [LOCAL_FILE_PATH] downloads a file from the Android to the PC
adb shell a very neat command, actually opens a shell on your Android linux box:

To uninstall software from the Android you can use the device’s GUI:
Home->Settings->Applications->Manage Applications or directly the adb uninstall command.

Hope this helps for a start,

Radu Motisan

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.