Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> Android NDK使用第三方動態庫

Android NDK使用第三方動態庫

編輯:初級開發

每個人的android配置環境不一樣 碰到的問題也不太一樣
#環境Ubuntu10.10  android-froyo2.2.1,使用源代碼編譯的SDK、ADT, NDKR5 同樣的網上的例子:參考http://blogimg.chinaunix.Net/blog/upfile2/100730112819.pdf 
------------------------------------- hello-ndk-jni.c ----------------------------- #include <string.h> #include <jni.h> #include "add.h" 
JString Java_com_gcx_ndkjni_HelloNdkJni_stringFromNDKJNI( JNIEnv* env,jobject thiz ) {     return (*env)->NewStringUTF(env, "Hello from NDK JNI !"); } 
jint Java_com_gcx_ndkjni_HelloNdkJni_add( JNIEnv* env,jobject thiz, jint a, jint b) { jint num = c_add(a, b);     return num; } ---------------------------------------------------------------------------------- 
------------------------------------- add.c -------------------------------------- int c_add(int a, int b) {     return (a + b); } 
------------------------------------- add.h ------------------------------------- #ifndef __C_ADD_H__ #define __C_ADD_H__ 
int c_add(int a, int b); 
#endif ----------------------------------------------------------------------------------- 
用於編譯libadd.so的makefile ---------------------------------- makefile --------------------------------------- NDK_PATH = /home/xusz/ndkr5 TOOLCHAIN_PATH = $(NDK_PATH)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin CFLAGS = -I$(NDK_PATH)/platforms/android-8/arch-arm/usr/include LDFLAGS = -L$(NDK_PATH)/platforms/android-8/arch-arm/usr/lib CC = $(TOOLCHAIN_PATH)/arm-linux-androideabi-gcc LD = $(TOOLCHAIN_PATH)/arm-Linux-androideabi-ld 
all:  $(CC) -O2 -fPIC -c $(CFLAGS) $(LDFLAGS) add.c -o libadd.o  $(LD) -o libadd.so libadd.o -shared    clean:  rm -rf libadd.o  rm -rf libadd.so ------------------------------------------------------------------------------------ --------------------------------- android.mk -------------------------------------- LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_PRELINK_MODULE := false LOCAL_C_INCLUDES := $(LOCAL_PATH) LOCAL_SHARED_LIBRARIES := libadd LOCAL_LDLIBS += -ladd LOCAL_SRC_FILES := hello-ndk-jni.c LOCAL_MODULE := hello-ndk-jni include $(BUILD_SHARED_LIBRARY) -------------------------------------------------------------------------------------- 
1. 編譯 libadd.so xusz@ubuntu:~/workspace/HelloNDKJNI/jni$ make /home/xusz/ndkr5/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc -O2 -fPIC -c -I/home/xusz/ndkr5/platforms/android-8/arch-arm/usr/include -L/home/xusz/ndkr5/platforms/android-8/arch-arm/usr/lib add.c -o libadd.o  /home/xusz/ndkr5/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-Linux-androideabi-ld -o libadd.so libadd.o -shared    xusz@ubuntu:~/workspace/HelloNDKJNI/jni$  xusz@ubuntu:~/workspace/HelloNDKJNI/jni$  
2. 編譯jni的so xusz@ubuntu:~/workspace/HelloNDKJNI/jni$ ndk-build -B Compile thumb  : hello-ndk-jni <= hello-ndk-jni.c SharedLibrary  : libhello-ndk-jni.so Install        : libhello-ndk-jni.so => libs/armeabi/libhello-ndk-jni.so xusz@ubuntu:~/workspace/HelloNDKJNI/jni$  
從eclipse中啟動 emulator告訴我 sorry。 
3. 從終端中啟動模擬器,我的avd名是tst xusz@ubuntu:~/workspace/HelloNDKJNI/jni$ emulator -avd tst -partition-size 128 
4. 另啟動一個終端,push libadd.so 到/system/lib xusz@ubuntu:~/workspace/HelloNDKJNI/jni$ adb push libadd.so /system/lib failed to copy 'libadd.so' to '/system/lib/libadd.so': Read-only file system xusz@ubuntu:~/workspace/HelloNDKJNI/jni$  采用網上最簡單的方式,修改system的權限 xusz@ubuntu:~/workspace/HelloNDKJNI/jni$  xusz@ubuntu:~/workspace/HelloNDKJNI/jni$ adb remount remount succeeded xusz@ubuntu:~/workspace/HelloNDKJNI/jni$ adb shell # chmod 777 system # exit xusz@ubuntu:~/workspace/HelloNDKJNI/jni$ adb push libadd.so /system/lib 38 KB/s (1843 bytes in 0.046s) xusz@ubuntu:~/workspace/HelloNDKJNI/jni$  
5. 從模擬器中啟動編譯的應用就可以了。
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved