/android
This page is dedicated to native application development for Android (primarily using C/C++).
Required software
Optional software
Contents
Hello, world
Let's start off with the trivial "Hello, world!" example. There's nothing special about doing this on Android, other than perhaps how you compile and deploy the program.
hello.c
Required software
- ARM tool chain (get the one targeted at "GNU/Linux"):http://www.codesourcery.com/gnu_toolchains/arm/download.html
Optional software
- Cygwin (helpful for Windows, contains make and other tools): http://www.cygwin.com/
- gdbserver built for arm-none-linux-gnueabi (alternative source) (for target debugging with gdb)
- strace built for arm-none-linux-gnueabi (alternative source) (useful for tracing syscalls and signals)
- unyaffs (prebuilt Windows version) (for extracting files from yaffs2 images)
- Yaffs2 tool: x86 Linux version, ARM version (alternative source) (for creating yaffs2 images)
Contents
- Hello, world
- Reading the keypad
- Building SDL
- Dynamic linking
- Using OpenGL ES
- Debugging
- Bitmap color reduction and GIF encoding
- A practical example
Hello, world
Let's start off with the trivial "Hello, world!" example. There's nothing special about doing this on Android, other than perhaps how you compile and deploy the program.
hello.c
#include int main(int argc, char **argv) { printf("Hello, world!\n"); return 0; }
Compile with arm-none-linux-gnueabi-gcc -static -o hello hello.c
After starting the Android device/emulator you need to push your program to the file system using the adbtool located in the tools directory of the Android SDK.
From the command line, do:
adb push hello /data/misc/hello
You can push the program to some other directory if you prefer. If adb complains about the file system being read-only (or "no such file or directory"), try remounting it in read-write mode. For example, if you wanted to be able to push the program to somewhere in /system:
adb shell mount -o remount,rw -t yaffs2 /dev/block/mtdblock0 /system
The next thing to do is to make sure that your program has the right attributes to be executed:
adb shell chmod 777 /data/misc/hello
Then you can execute your program through the shell:
adb shell /data/misc/hello
Reading the keypad
Getting input from the shell can be done simply by reading from stdin. It might be more interesting, however, to get input from the device's keypad, and this requires some extra steps. The keypad is located at/dev/input/event0, so what you want to do is read from that device. Below I show one way of doing this:
andkeys.h
After starting the Android device/emulator you need to push your program to the file system using the adbtool located in the tools directory of the Android SDK.
From the command line, do:
adb push hello /data/misc/hello
You can push the program to some other directory if you prefer. If adb complains about the file system being read-only (or "no such file or directory"), try remounting it in read-write mode. For example, if you wanted to be able to push the program to somewhere in /system:
adb shell mount -o remount,rw -t yaffs2 /dev/block/mtdblock0 /system
The next thing to do is to make sure that your program has the right attributes to be executed:
adb shell chmod 777 /data/misc/hello
Then you can execute your program through the shell:
adb shell /data/misc/hello
Reading the keypad
Getting input from the shell can be done simply by reading from stdin. It might be more interesting, however, to get input from the device's keypad, and this requires some extra steps. The keypad is located at/dev/input/event0, so what you want to do is read from that device. Below I show one way of doing this:
andkeys.h
#ifndef ANDKEYS_H #define ANDKEYS_H #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> typedef struct { struct timeval time; unsigned short type; unsigned short code; unsigned int value; } input_event; // Opens the Android keypad device for reading void andkeys_open(); // Polls the Android keypad device for new data. Returns 0 if there was no new data. // Otherwise non-zero is returned and the input_event struct pointed to by 'ie' is // filled with the latest data. int andkeys_get(input_event *ie);
没有评论:
发表评论