編輯:關於 Android 開發
對做上層 App 的工程師來說,「在 Android 上跑 C 程式」聽起來有點陌生,畢竟應用層開發繞不開 Java(以及後來的 Kotlin)。但對底層驅動與系統層的人來說這是日常:Android 是 Linux 分支,底層世界說的是 C 與 C++。
驗證某些機制時,我們確實需要一支直接跑在 Android 終端下的原生程式。前提有兩個:一套 Android 交叉編譯器,以及裝置的 root 權限。
NDK 為 native 開發做了很多事,但我們只需要其中的交叉編譯工具。以 64 位元的 Ubuntu 14.04 為例,下載對應版本的 NDK:
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
腳本會把預編譯的執行檔、sysroot 標頭檔與函式庫打包成 tar.bz2,解開後即可使用:
build-tools$ tar jxvf arm-linux-androideabi-4.9.tar.bz2
寫一支最簡單的 C 程式 main_test.c:
#include <stdio.h>
int main() {
printf("just a test, linc! hello, native!\n");
return 0;
}
用剛剛產生的工具鏈編譯:
$ ~/bin/build-tools/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc \
-o main_test main_test.c
推到裝置上執行:
$ adb push main_test /data/app $ adb shell root@hammerhead:/ # cd /data/app root@hammerhead:/data/app # chmod 755 main_test root@hammerhead:/data/app # ./main_test just a test, linc! hello, native!
就跟在 Linux 上一模一樣。順帶一提,/data/local/tmp 是更常見的暫存位置,某些 SELinux 較嚴格的版本對 /data/app 的執行權限會擋下來。
把排序邏輯與測試進入點拆成兩個檔案, header 放在 shooter.h:
/* shooter.c */
#include "shooter.h"
#include <stdio.h>
void bubble_sort(int *array, int n) {
int i, j, tmp;
for (i = 0; i < n - 1; i++) {
for (j = n - 1; j > i; j--) {
if (array[j - 1] > array[j]) {
tmp = array[j - 1];
array[j - 1] = array[j];
array[j] = tmp;
}
}
}
}
int A(int a) {
int array[] = {54, 12, 346, 5, 23, 67, 234, 324, 45, 98};
bubble_sort(array, 10);
return array[0];
}
/* shooter_tester.c */
#include <stdio.h>
#include "shooter.h"
int main() {
int result = A(0);
printf("A result: %d\n", result);
return 0;
}
編譯時把兩個 .c 一起交給 gcc 即可,不必先各自編譯成 .o:
$ arm-linux-androideabi-gcc -o test shooter_tester.c shooter.c $ adb push test /data/app/ $ adb shell root@hammerhead:/data/app # ./test A result: 5
把原本的固定陣列改成用 rand() 產生:
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int A(int a) {
int n = 10, i;
int array[n];
srand(time(NULL));
for (i = 0; i < n; i++) {
array[i] = rand() % 100;
}
bubble_sort(array, n);
return array[0];
}
編譯時卻出現了:
error: undefined reference to `srand' error: undefined reference to `rand'
原因很單純:rand 與 srand 的宣告在 stdlib.h 裡,只引入 time.h 是不夠的。補上標頭檔後就正常了:
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 來玩了。交叉編譯的門檻比想像中低,真正的成本在於理解目標環境的檔案系統權限與 SELinux 政策。祝你折騰愉快。
Android 學習指南之二十五:用 SurfaceView 打造高流暢動畫(下)
這一節談的是 SurfaceView 的動畫繪製。作為 View 的子公司類別,SurfaceView 有自己獨立的繪圖表面,並在背景執行緒上繪製,因此速度比一般 Vie
Android 特效專輯(八)——打造心型氣泡飛舞特效,讓你的 App 瞬間暖心
Android 特效專輯(八)——實現心型氣泡飛舞的特效,讓你的 APP 瞬間暖心 馬上也要放年假了,家裡估計會沒網,更完這篇可能要到年後了。
DrawableView 第三方開源畫板:手寫、調色、縮放與存檔
元件能力概覽 DrawableView 是一個 Android 手寫板元件,開箱就能用。它支援:調整畫筆顏色、調整線條粗細、手勢縮放與拖曳畫布、復原上一筆,以及將手繪結果
Android 搜尋功能實作:SQLite 保存搜尋歷史與即時篩選
搜尋框不只是輸入框 在許多 App 中,搜尋功能不只是把關鍵字送出,還需要記住使用者最近搜尋過的內容。適當的歷史紀錄能減少重複輸入,也能讓使用者快速回到感興趣的主題。本文