Android C Programming Tips
Running the emulator
Emulator-console
* Write files to the emulator's userdata.img file
ADB push
* Copy a directory to the emulator, including subdirectories
ADB push
* Copy a directory from the simulator
ADB pull
* Allows the simulator to run arm code.
Use the gnu/arm Linux compiler to compile your application.
* Run the shell inside the simulator, you need to run the emulator first
ADB shell
* Run a console program in the emulator
ADB shell
* Console for Connection simulator
telnet localhost 5554/6/8
Running C Programs
Reference documents
Native C "Hello World" working in emulator
http://groups.google.com/group/a ... wse_thread/threa ...
Native C Applications for Android
Http://benno.id.au/blog/2007/11/13/android-native-apps
Steps
* Download Gnu/arm Compilation tool
Http://www.codesourcery.com/gnu_toolchains/arm/download.html
* Code for C + + is written.
* Use Gnu/arm Linux tools to create an application that does not use the dynamic link library
ex. arm-none-linux-gnueabi-g++.exe -static -o hello HelloAndroid.cpp
* Start simulator
$SDK_ROOT/tools/emulator.exe
* Run Abd in the command line window to put the compiled Hello program into the emulator's disk
adb push hello /system/sbin/hello
* Make the Hello file executable and do not use chmod ugo+x
adb shell chmod 777 /system/sbin/hello
* Run Hello Program
adb shell
cd /system/sbin/
hello
EXAMPLE HELLO WORLD CODE
//
// HelloAndroid.cpp
//
//
#include
using std::cin;
using std::cout;
using std::endl;
class MyName
{
public:
void getname( void );
void sayhello( void );
private:
char name[ 255 ];
};
void MyName::getname( void )
{
cout << "What is your name? ";
cin >> name;
}
void MyName::sayhello( void )
{
cout << "Welcome " << name << " to the world of Android" << endl;
}
MyName name;
int main( int argc, char *argv[] )
{
name.getname();
name.sayhello();
return 0;
}
Android compiled native C + + program method
The program runs in Java on the Android platform on the Dalvik emulator, but Android is fully capable of executing navtive C + + programs as a Linux kernel system, the main steps are as follows:
1. Download Arm C + + cross compiler http://www.codesourcery.com/gnu_toolchains/arm/portal/subscription3057
2. Write native C + + code, such as Hello wolrd, which can use the standard library STL. The compiled command line is as follows
arm-none-linux-gnueabi-g++.exe -static -oandroid123 android123.cpp
First run the Arm-none-linux-gnueabi-g++.exe program-static parameter represents the static library,-O is the output name android123, and the last Android123.cpp is the source code.
3. Run the simulator with CMD in the Sdktools directory of the summer star ADB pushandroid123/system/sbin/android123
4. Set access rights through the Linux shell, under cmd Set all user Full Control permissions adb shell chmod 777/system/sbin/android123
5. Execute the ANDROID123 program and enter it adb shell cd /system/sbin/android123
Test your C + + programs and libraries on the Android platform
int main( int argc, char *argv[] )
{
name.getname();
name.sayhello();
return 0;
Android platform with the standard C library, we can write a C program to try to run on the above ...
First download and install the cross-compilation tool Gnu/arm Linux GCC:
Http://www.codesourcery.com/gnu_toolchains/arm/download.html
The installation of the direct decompression on the line, to set the PATH environment variable.
Simple C-code:
test.c
#include <stdio.h>
int main()
{
int i,j;
for(i=0;i<=10;i++)
{
for(j=0;j<=i;j++)
printf(”*”);
printf(”n”);
}
return 0;
}
Compile the source code with the cross-compilation tool you just downloaded:
# ARM-NONE-LINUX-GNUEABI-GCC Test.c-o test-static
The-static option is required here, otherwise the Android platform will not run this program.
This also shows that the C + + libraries on this platform cannot be dynamically connected by C/
Go to the Tools directory, download the ADB tool to the Android platform, and put it in the/data/data directory.
#./ADB Push Test/data/data
Go to the/data/data directory to run the program.
# Cd/data/data
#./test
*
**
***
****
*****
******
*******
********
*********
**********
***********
Ok,it ' s done!
C + + programs, except that the compiler replaced: arm-none-linux-gnueabi-g++
Attached C + + sample code:
//
// HelloAndroid.cpp
//
//
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
class MyName
{
public:
void getname( void );
void sayhello( void );
private:
char name[ 255 ];
};
void MyName::getname( void )
{
cout << “What is your name? “;
cin >> name;
}
void MyName::sayhello( void )
{
cout << “Welcome “ << name << ” to the world of Android” << endl;
}
MyName name;
}
The above application must be compiled with the-static option, which means that the functions are compiled statically into the program at compile time, and the runtime is not dynamically connected. If this option is not added, it will not be allowed to run on the Android platform.
After testing, put your own library into the/system/lib directory, and then write a main program to dynamically connect, is also unable to run.
It appears that the platform has been restricted from dynamically connecting C/C + + programs when they are running.