Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android實戰技巧之四十四:Hello,Native!

Android實戰技巧之四十四:Hello,Native!

編輯:關於android開發

Android實戰技巧之四十四:Hello,Native!


在Android上運行C程序對於做上層App的童鞋來說有些陌生,因為目前的Android應用開發怎麼還是繞不過Java。
但對於底層驅動開發者,這就是家常便飯一樣,因為Android是Linux分支,底層是C/C++的世界。

有時為了測試一些功能,我們也會編寫直接運行在Android終端下的C程序。前提是有Android交叉編譯器以及Android系統的root權限。

交叉編譯工具

ndk為我們開發native程序做了很多工作,下面我們將Android交叉編譯工具從ndk中分離出來。
我的系統是64位的Ubuntu 14.04,所以我就下載了64位ndk(android-ndk-r10e-linux-x86_64.bin)。

ndk$ chmod a+x android-ndk-r10e-linux-x86_64.bin
ndk$ ./android-ndk-r10e-linux-x86_64.bin

此時,ndk就可以工作了。讓我們將交叉編譯工具變出來吧。

$ ./build/tools/make-standalone-toolchain.sh --platform=android-19 --toolchain=arm-linux-androideabi-4.9
Copying prebuilt binaries...
Copying sysroot headers and libraries...
Copying c++ runtime headers and libraries...
Creating package file: /tmp/ndk-linc/arm-linux-androideabi-4.9.tar.bz2
Cleaning up...
Done.

找到合適的路徑,解壓:

build-tools$ tar jxvf arm-linux-androideabi-4.9.tar.bz2
arm-linux-androideabi-4.9/
...

hello,native

編譯c文件main_test.c


#include 

int main() {
    printf(just a test,linc!hello, native!
);
    return 0;
}

編譯它:

$ ~/bin/build-tools/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc -o main_test main_test.c

跑起來:
將main_test拷到Android中運行:

$ adb push main_test /data/app
137 KB/s (6192 bytes in 0.043s)
$ adb shell
root@hammerhead:/ # cd data/app                                               
root@hammerhead:/data/app # ls
main_test
root@hammerhead:/data/app # ./main_test                                       
just a test,linc!hello, native!

如我們所願,程序順利的運行起來,跟在Linux系統中一樣。下面我們來編譯兩個文件的程序。
shooter.c

#include shooter.h
#include 

void bubble_sort(int *array,int n) {
    int i,j,tmp;
    for(i=0;ii;j--) {
            if(array[j-1]>array[j]) {
                tmp = array[j-1];
                array[j-1]=array[j];
                array[j]=tmp;
            }
        }
    }
}

int A(int a) {
    int n = 10;
    int i;
    int array[] = {54,12,346,5,23,67,234,324,45,98};    
    for(i=0;i

shooter_tester.c

#include 
#include shooter.h

int main() {
    int result = A(0);
    printf(A result: %d
,result);
    return 0;
}

編譯運行之:

$ ~/bin/build-tools/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc -o test shooter_tester.c shooter.c 
$ adb push test /data/app/
143 KB/s (6344 bytes in 0.043s)
$ adb shell
root@hammerhead:/ # cd data/app                                                
root@hammerhead:/data/app # ./test                                            
54, 12, 346, 5, 23, 67, 234, 324, 45, 98, 
A result: 5

使用隨機數

接下來嘗試將《做一個動態鏈接庫》中的代碼在Android下測試,准備將此so移植到Android平台。
shooter.c只是將上面的程序用rand和srand生成隨機數。

#include shooter.h
#include 
#include 

void bubble_sort(int *array,int n) {
    int i,j,tmp;
    for(i=0;ii;j--) {
            if(array[j-1]>array[j]) {
                tmp = array[j-1];
                array[j-1]=array[j];
                array[j]=tmp;
            }
        }
    }
}

int A(int a) {
    int n = 10;
    int i;
    int array[n];

    srand(time(NULL));
    for(i=0;i

只是編譯的時候遇到了問題:

 error: undefined reference to 'srand'
 error: undefined reference to 'rand'

原來是隨機數方法的頭文件變成了stdlib,引入就可以了。
《Android問題集錦之四十五:undefined reference to ‘srand’》
編譯運行結果如下:

$ ~/bin/build-tools/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc -o test shooter_tester.c shooter.c 
$ adb shell
root@hammerhead:/ # cd data/app                                               
root@hammerhead:/data/app # ./test                                            
18, 95, 91, 55, 13, 37, 74, 85, 83, 66, 
A result: 13
root@hammerhead:/data/app # ./test                                            
59, 100, 84, 32, 26, 46, 11, 50, 44, 83, 
A result: 11

尾聲

現在,可以把Android當成Linux玩了。祝你愉快!

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved