運行模擬器
emulator -console
* 將文件寫入到模擬器的userdata.img文件中
adb push
*將一個目錄拷貝到模擬器中,包括子目錄
adb push
* 將一個目錄從模擬器中拷出來
adb pull
* 使得模擬器可以運行arm代碼.
使用GNU/ARM Linux編譯器編譯你的應用程序就可以了
* 在模擬器裏面運行shell,需要先運行模擬器
adb shell
*運行模擬器中的一個控制檯程序
adb shell
*連接模擬器的控制檯
telnet localhost 5554/6/8
運行C程序
參考文獻
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
步驟
* 下載GNU/ARM編譯工具
http://www.codesourcery.com/gnu_toolchains/arm/download.html
* 編寫c/c++代碼.
* 使用GNU/ARM Linux 工具創建一個應用程序,不使用動態鏈接庫
ex. arm-none-linux-gnueabi-g++.exe -static -o hello HelloAndroid.cpp
* 啓動模擬器
$SDK_ROOT/tools/emulator.exe
* 在命令行窗口運行 abd將編譯好的hello程序放入模擬器的磁盤
adb push hello /system/sbin/hello
* 讓hello文件變成可執行程序,不要使用 chmod ugo+x
adb shell chmod 777 /system/sbin/hello
* 運行hello程序
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編譯本地C++程序方法
在Android平臺上程序以Java形式運行在Dalvik模擬器上,但Android作爲一個Linux內核系統完全可以執行Navtive
C++程序,主要的步驟如下:
1.下載ARM C++交叉編譯器http://www.codesourcery.com/gnu_toolchains/arm/portal/subscription3057
2.編寫本地C++代碼,如Hello Wolrd,可以使用標準庫STL。編譯的命令行如下
arm-none-linux-gnueabi-g++.exe -static -oandroid123 android123.cpp
首先運行arm-none-linux-gnueabi-g++.exe程序-static 參數代表靜態庫,-o爲輸出名稱android123,最後的android123.cpp爲源代碼。
3.運行模擬器,用cmd在sdkTools目錄夏之星 adb pushandroid123 /system/sbin/android123
4.設置訪問權限,通過Linux的Shell,在cmd下設置所有用戶完全控制權限adb shell chmod 777 /system/sbin/android123
5.執行這個android123程序,輸入adb shell cd /system/sbin/android123即可
在android平臺上測試C/C++程序及庫
int main( int argc, char *argv[] )
{
name.getname();
name.sayhello();
return 0;
android平臺上帶有標準C庫,我們可以寫個C程序來試試看能不能在上面運行。。。
首先下載並安裝交叉編譯工具GNU/ARM Linux gcc:
http://www.codesourcery.com/gnu_toolchains/arm/download.html
安裝時 直接解壓就行了,要設置好PATH環境變量。
簡單的C代碼:
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;
}
用剛下載的交叉編譯工具編譯源代碼:
# arm-none-linux-gnueabi-gcc test.c -o test -static
-static選項在這裏是必須的,不然android平臺就不運行此程序。
這也說明了此平臺上的C/C++庫是不能被C/C++程序動態連接的 。
進入tools目錄,用adb工具下載到android平臺,放到/data/data目錄。
# ./adb push test /data/data
進入/data/data目錄運行程序。
# cd /data/data
# ./test
*
**
***
****
*****
******
*******
********
*********
**********
***********
ok,It’s done !
C++程序一樣的方法,只不過編譯器換成:arm-none-linux-gnueabi-g++
附C++示例代碼:
//
// 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;
}
上面的應用程序在編譯時必須加上-static選項,也就是在編譯時將函數都靜態編譯到程序中了,運行時不用再動態連接。如果不加此選項,在android平臺上就不讓運行。
經過測試,將自己寫的庫放到/system/lib目錄下,然後寫個主程序來動態連接,也是無法運行。
看來此平臺做了限制,不讓C/C++的程序運行時動態連接到這些C/C++庫。