Android編譯環(huán)境(1) - 編譯Native C的helloworld模塊
Android編譯環(huán)境本身比較復(fù)雜,且不像普通的編譯環(huán)境:只有頂層目錄下才有Makefile文件,,而其他的每個(gè)component都使用統(tǒng)一標(biāo)準(zhǔn)的Android.mk. Android.mk文件本身是比較簡單的,,不過它并不是我們熟悉的Makefile,而是經(jīng)過了Android自身編譯系統(tǒng)的很多處理,,因此要真正理清楚其中的聯(lián)系還比較復(fù)雜,,不過這種方式的好處在于,編寫一個(gè)新的Android.mk來給Android增加一個(gè)新的Component會(huì)比較簡單,。
編譯Java程序可以直接采用Eclipse的集成環(huán)境來完成,,這里就不重復(fù)了。我們主要針對(duì)C/C++來說明,,下面通過一個(gè)小例子來說明,,如何在Android 中增加一個(gè)C程序的Hello World:
1. 在$(YOUR_ANDROID)/development 目錄下創(chuàng)建hello目錄,其中$(YOUR_ANDROID)指Android源代碼所在的目錄,。
- # mkdir $(YOUR_ANDROID)/development/hello
2. 在$(YOUR_ANDROID)/development/hello/目錄編寫hello.c文件,,hello.c的內(nèi)容當(dāng)然就是經(jīng)典的HelloWorld程序:
#include <stdio.h>
int main() { printf("Hello World!\n");
return 0; }
|
3. 在$(YOUR_ANDROID)/development/hello/目錄編寫Android.mk文件。這是Android Makefile的標(biāo)準(zhǔn)命名,,不要更改,。Android.mk文件的格式和內(nèi)容可以參考其他已有的Android.mk文件的寫法,針對(duì)helloworld程序的Android.mk文件內(nèi)容如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
hello.c
LOCAL_MODULE := helloworld
include $(BUILD_EXECUTABLE)
|
注意上面LOCAL_SRC_FILES用來指定源文件,;,,LOCAL_MODULE指定要編譯的模塊的名字,下一步驟編譯時(shí)就要用到,;include $(BUILD_EXECUTABLE)表示要編譯成一個(gè)可執(zhí)行文件,,如果想編譯成動(dòng)態(tài)庫則可用BUILD_SHARED_LIBRARY,,這些可以在$(YOUR_ANDROID)/build/core/config.mk查到。
4. 回到Android源代碼頂層目錄進(jìn)行編譯:
# cd $(YOUR_ANDROID) && make helloworld
|
注意make helloworld中的目標(biāo)名helloworld就是上面Android.mk文件中由LOCAL_MODULE指定的模塊名,。編譯結(jié)果如下:
target thumb C: helloworld <= development/hello/hello.c
target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)
target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld)
target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld)
Install: out/target/product/generic/system/bin/helloworld
|
5.如上面的編譯結(jié)果所示,,編譯后的可執(zhí)行文件存放在out/target/product/generic/system/bin/helloworld,通過”adb push”將它傳送到模擬器上,,再通過”adb shell”登錄到模擬器終端,,就可以執(zhí)行了
Android編譯環(huán)境(2) - 手工編譯C模塊
我們來試試如何直接運(yùn)用gcc命令行來編譯,從而了解Android編譯環(huán)境的細(xì)節(jié),。
Android編譯環(huán)境提供了”showcommands”選項(xiàng)來顯示編譯命令行,我們可以通過打開這個(gè)選項(xiàng)來查看一些編譯時(shí)的細(xì)節(jié),。當(dāng)然,,在這之前要把上一篇中的helloworld模塊clean:
上面的“make clean-$(LOCAL_MODULE)”是Android編譯環(huán)境提供的make clean的方式。
接下來使用showcommands選項(xiàng)重新編譯helloworld:
# make helloworld showcommands
build/core/product_config.mk:229: WARNING: adding test OTA key
target thumb C: helloworld <= development/hello/hello.c
prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -I system/core/include -I hardware/libhardware/include -I hardware/ril/include -I dalvik/libnativehelper/include -I frameworks/base/include -I external/skia/include -I out/target/product/generic/obj/include -I bionic/libc/arch-arm/include -I bionic/libc/include -I bionic/libstdc++/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -I bionic/libm/include -I bionic/libm/include/arch/arm -I bionic/libthread_db/include -I development/hello -I out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates -c -fno-exceptions -Wno-multichar -march=armv5te -mtune=xscale -msoft-float -fpic -mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -include system/core/include/arch/linux-arm/AndroidConfig.h -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -DSK_RELEASE -DNDEBUG -O2 -g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once -fgcse-after-reload -frerun-cse-after-loop -frename-registers -DNDEBUG -UDEBUG -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -MD -o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o development/hello/hello.c
target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)
prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-g++ -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lstdc++ -lm out/target/product/generic/obj/lib/crtbegin_dynamic.o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o -Wl,--no-undefined prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/../lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a out/target/product/generic/obj/lib/crtend_android.o
target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld)
out/host/linux-x86/bin/acp -fpt out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld out/target/product/generic/symbols/system/bin/helloworld
target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld)
out/host/linux-x86/bin/soslim --strip --shady --quiet out/target/product/generic/symbols/system/bin/helloworld --outfile out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld
Install: out/target/product/generic/system/bin/helloworld
out/host/linux-x86/bin/acp -fpt out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld out/target/product/generic/system/bin/helloworld
|
從上面的命令行可以看到,,Android編譯環(huán)境所用的交叉編譯工具鏈?zhǔn)?/span>prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc,,-I和-L參數(shù)指定了所用的C庫頭文件和動(dòng)態(tài)庫文件路徑分別是bionic/libc/include 和out/target/product/generic/obj/lib,其他還包括很多編譯選項(xiàng)以及-D所定義的預(yù)編譯宏,。
我們可以利用上面的編譯命令,,稍加簡化來手工編譯helloworld程序。先手工刪除上次編譯得到的helloworld程序:
# rm out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o
# rm out/target/product/generic/system/bin/helloworld
|
再用gcc編譯,,生成目標(biāo)文件:
# prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -I bionic/libc/arch-arm/include -I bionic/libc/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -c -fno-exceptions -Wno-multichar -march=armv5te -mtune=xscale -msoft-float -fpic -mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -include system/core/include/arch/linux-arm/AndroidConfig.h -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -DSK_RELEASE -DNDEBUG -O2 -g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once -fgcse-after-reload -frerun-cse-after-loop -frename-registers -DNDEBUG -UDEBUG -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -MD -o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o development/hello/hello.c
|
與Android.mk編譯參數(shù)比較,,上面主要減少了不必要的-I參數(shù)。
接下來生成可執(zhí)行文件:
# prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lm out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o out/target/product/generic/obj/lib/crtbegin_dynamic.o -Wl,--no-undefined ./prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/../lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a out/target/product/generic/obj/lib/crtend_android.o
|
這里值得留意的是參數(shù)“-Wl,-dynamic-linker,/system/bin/linker”,,它指定了Android專用的動(dòng)態(tài)鏈接器/system/bin/linker,,而不是通常所用的ld.so。
生成的可執(zhí)行程序可用file和readelf命令來查看一下:
# file out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld
out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
# readelf -d out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld |grep NEEDED
0x00000001 (NEEDED) Shared library: [libc.so]
0x00000001 (NEEDED) Shared library: [libm.so]
|
這是ARM格式的動(dòng)態(tài)鏈接可執(zhí)行文件,,運(yùn)行時(shí)需要libc.so和libm.so,。“not stripped”表示它還沒被STRIP。嵌入式系統(tǒng)中為節(jié)省空間通常將編譯完成的可執(zhí)行文件或動(dòng)態(tài)庫進(jìn)行STRIP,,即去掉其中多余的符號(hào)表信息,。在前面“make helloworld showcommands”命令的最后我們也可以看到,Android編譯環(huán)境中使用了out/host/linux-x86/bin/soslim工具進(jìn)行STRIP,。
有關(guān)Android Toolchain的其他一些內(nèi)容可參考:Android Toolchain與Bionic Libc
Android上JNI的調(diào)試
Android的SDK中沒有包括JNI的支持,,而且對(duì)如何支持JNI也沒有任何文檔說明。不過既然整個(gè)Android平臺(tái)是開源的,,我們可以通過Google發(fā)布的源代碼來找到一些線索(比如frameworks/base/media/jni/目錄),,依葫蘆畫瓢的實(shí)現(xiàn)上層JAVA程序通過JNI來調(diào)用Native C程序中的函數(shù)。
依照下面的步驟可以實(shí)現(xiàn)一個(gè)非常簡單的JNI的實(shí)例程序:
1. 首先編寫C模塊,,實(shí)現(xiàn)動(dòng)態(tài)庫,。(關(guān)于如何在Android中編譯C模塊的更多細(xì)節(jié),,請(qǐng)參考《Android編譯環(huán)境(1) - 編譯Native C的helloworld模塊》。)
在development目錄下添加新目錄hellolib,,并添加hellolib.c和Android.mk文件,。hellolib.c的內(nèi)容如下:
#include <jni.h>
#define LOG_TAG "TestLib"
#undef LOG
#include <utils/Log.h>
JNIEXPORT void JNICALL Java_com_test_TestHelloLib_printHello(JNIEnv * env, jobject jobj)
{
LOGD("Hello LIB!\n");
}
|
注意這里的函數(shù)名需要按照JNI的規(guī)范(因此也可以用javah -jni工具來生成頭文件,來保證函數(shù)名的正確性),,Java_com_test_TestHelloLib_printHello的命名對(duì)應(yīng)后面在java代碼中,,package名字是com.test,類名是TestHelloLib,,native函數(shù)名是printHello,。
另外,LOGD及#define LOG_TAG "TestLib"等打印log的方式是采用了Android所提供的LOG機(jī)制,,這樣才能通過Android的logcat工具看到log,。
用于編譯C模塊的Android.mk文件內(nèi)容如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
hellolib.c
LOCAL_C_INCLUDES := \
$(JNI_H_INCLUDE)
LOCAL_SHARED_LIBRARIES := \
libutils
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE := libhello
include $(BUILD_SHARED_LIBRARY)
|
該文件中的一些變量分別對(duì)應(yīng)的含義如下:
LOCAL_SRC_FILES - 編譯的源文件
LOCAL_C_INCLUDES - 需要包含的頭文件目錄
LOCAL_SHARED_LIBRARIES - 鏈接時(shí)需要的外部庫
LOCAL_PRELINK_MODULE - 是否需要prelink處理(參考prelink的詳細(xì)介紹:《動(dòng)態(tài)庫優(yōu)化——Prelink(預(yù)連接)技術(shù)》,Android的Toolchain, prelink工具:《Android Toolchain與Bionic Libc》)
LOCAL_MODULE - 編譯的目標(biāo)對(duì)象
BUILD_SHARED_LIBRARY - 指明要編譯成動(dòng)態(tài)庫,。
接下來回到Android頂層目錄,,并執(zhí)行make libhello來編譯:
# cd $(YOUR_ANDROID) && make libhello
target thumb C: libhello <= development/hellolib/hellolib.c
target SharedLib: libhello (out/target/product/generic/obj/SHARED_LIBRARIES/libhello_intermediates/LINKED/libhello.so)
target Non-prelinked: libhello (out/target/product/generic/symbols/system/lib/libhello.so)
target Strip: libhello (out/target/product/generic/obj/lib/libhello.so)
Install: out/target/product/generic/system/lib/libhello.so
|
編譯結(jié)果可得到位于out/target/product/generic/system/lib/目錄的動(dòng)態(tài)共享庫libhello.so
2.編寫Java模塊,來通過JNI方式調(diào)用C接口,。具體Eclipse環(huán)境的搭建請(qǐng)參考Android SDK文檔中的詳細(xì)說明,,及Hello Android程序的創(chuàng)建過程,這里僅給出我們需要修改的TestHelloLib.java文件:
package com.test;
import android.app.Activity;
import android.os.Bundle;
public class TestHelloLib extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
printHello();
}
static {
System.loadLibrary("hello");
}
private native void printHello();
}
|
注意上面代碼中粗體字部分:private native void printHello()用來聲明一個(gè)native接口,,static { System.loadLibrary("hello"); } 用來加載上面步驟中生成libhello.so(注意loadLibrary方法的參數(shù)不是”libhello.so”,,而是去掉前綴和后綴之后的”hello”),onCreate()方法中則調(diào)用了printHello()接口,。
通過這一步驟可生成Android開發(fā)者所熟悉的apk文件:TestHelloLib.apk,。
3.集成測(cè)試TestHelloLib.apk和libhello.so。先運(yùn)行emulator并將TestHelloLib.apk和libhello.so上傳至emulator中,。注意要將libhello.so上傳到emulator的/system/lib目錄,,由于該目錄是只讀的,上傳之前先要執(zhí)行adb remount:
# adb remount
# adb push out/target/product/generic/system/lib/libhello.so /system/lib
# adb install TestHelloLib.apk
|
接下來在模擬器菜單中可以看到已經(jīng)安裝的TestHelloLib程序,,運(yùn)行即可,。
由于JNI接口printHello()并沒有作界面上的改動(dòng),要驗(yàn)證其效果需要用Android的logcat工具來查看,。運(yùn)行”adb logcat”可以找到下面的log片斷:
I/ActivityManager( 48): Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.category.LAUNCHER} flags=0x10200000 comp={com.test/com.test.TestHelloLib} }
I/ActivityManager( 48): Start proc com.test for activity com.test/.TestHelloLib: pid=174 uid=10024 gids={}
D/dalvikvm( 174): Trying to load lib /system/lib/libhello.so 0x43481c58
D/dalvikvm( 174): Added shared lib /system/lib/libhello.so 0x43481c58
D/dalvikvm( 174): No JNI_OnLoad found in /system/lib/libhello.so 0x43481c58
D/dalvikvm( 174): +++ not scanning '/system/lib/libwebcore.so' for 'printHello' (wrong CL)
D/dalvikvm( 174): +++ not scanning '/system/lib/libmedia_jni.so' for 'printHello' (wrong CL)
D/TestLib ( 174): Hello LIB!
I/ActivityManager( 48): Displayed activity com.test/.TestHelloLib: 806 ms
|
這里包含了調(diào)用printHello()接口的log信息,,其中”D/TestLib ( 174): Hello LIB!”就是printHello()所打印的信息。至此成功完成Android JNI的實(shí)例驗(yàn)證,。