Sunday, June 16, 2013

Native C programs for ARM based Android phones

You can always use Android SDK to program and build Java programs for android phone which are running on Dalvik Virtual Machine. Recently I wanted to build some native C applications for my phone. As Android is based on Linux kernal, it's matter of finding an appropriate cross compiler for the PC. As I found out there are many toolchains for this and I used Linaro ARM tool chain for Windows (WIN32). This is based on GCC and  I downloaded 2013.05 version and installed it using the installer. Please make sure to tick "add to the PATH" when you installing this.

I wrote this small code to test and saved as count.c ,

#include <stdio.h>
int main()
{
int x=0;
for(x=0;x<11;x++){
printf("%i\n",x);
}
}


Then I compiled it using following command in command prompt,

D:\>arm-linux-gnueabihf-gcc count.c -o count -static

The I copied the binary file to mobile using ADB (Android Debug Bridge), you can do it using a file manager too. Make sure to put the compile binary to /system because you can execute binaries only at there in android system.

D:\>adb push count /system
3049 KB/s (463524 bytes in 0.148s)

Then I logged into android shell using ADB, You may use a Terminal Emulator software on the phone itself.

D:\>adb shell

I got root access and changed the file attributes of "count" binary to make it executable (same as on any Linux distribution). (You can either use chmod +x or chmod 777)

~ # su
root@android:/ # cd /system
root@android:/system # chmod 777 count

I executed "count" and tested ,


root@android:/system # ./count
./count
0
1
2
3
4
5
6
7
8
9
10
11|root@android:/system #

I also tested the program on phone using a terminal emulator too,





No comments:

Post a Comment