一、源代码编译
1.1 so预编译
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libAppArea
LOCAL_SRC_FILES :=libAppArea.so
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MODULE_SUFFIX:=$(HOST_JNILIB_SUFFIX)
LOCAL_CERTIFICATE:=platform
LOCAL_PRELINK_MODULE := false
include $(BUILD_PREBUILT)
1.2 share lib编译
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
#LDFLAGS += -ldl
LOCAL_LDLIBS := -ldl -lm -llog -llibc
LOCAL_MODULE := libnandFlashReadPage_jni
LOCAL_SRC_FILES := nandFlashReadPage_jni.cpp\ LOCAL_SHARED_LIBRARIES := libdl\
liblog\
libpre_NandRecognition\
libpre_AppArea\
libc\
libstdc++ LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
升级到Android 4.0。 原来我们在3.2 版本号以下编译的程序有些不能编译通过。
特别是
复制apk,复制动态库的Android.mk 文件,经过研究。现解决方法例如以下。
错误信息:The following variables have been changed: PRODUCT_COPY_FILES. Stop.
解决的方法:以下几个样例共參考。各自是复制APK;SO;ZIP; db; jpg
假设有其它更好的办法。
请留言。
- LOCAL_PATH:= $(call my-dir)
- ######################################
- #COPY PayMent APK
- include $(CLEAR_VARS)
- LOCAL_MODULE := PayMent
- #输出PayMent.apk 文件的文件夹
- LOCAL_MODULE_PATH := $(TARGET_OUT)/app/
- LOCAL_SRC_FILES :=../3rd-apk/PayMent/PayMent.apk
- LOCAL_MODULE_TAGS := optional
- LOCAL_MODULE_CLASS := APPS
- #APPS,JAVA_LIBRARIES,SHARED_LIBRARIES,STATIC_LIBRARIES,EXECUTABLES
- LOCAL_MODULE_SUFFIX:=$(COMMON_ANDROID_PACKAGE_SUFFIX)
- LOCAL_CERTIFICATE:=platform
- LOCAL_SDK_VERSION := current
- include $(BUILD_PREBUILT)
- ######################################
- #COPY SpeechService APK WITH xx.so
- include $(CLEAR_VARS)
- #need modify here
- LOCAL_MODULE := SpeechService
- LOCAL_MODULE_PATH := $(TARGET_OUT)/app/
- LOCAL_SRC_FILES :=../3rd-apk/IFlyService/SpeechService.apk
- #need modify here
- #optional if you have .so;need add COPY so file
- LOCAL_JNI_SHARED_LIBRARIES:=libmsc
- LOCAL_MODULE_TAGS := optional
- LOCAL_MODULE_CLASS := APPS
- LOCAL_MODULE_SUFFIX:=$(COMMON_ANDROID_PACKAGE_SUFFIX)
- LOCAL_CERTIFICATE:=platform
- LOCAL_SDK_VERSION := current
- include $(BUILD_PREBUILT)
- ######################################
- #COPY SpeechService so file
- include $(CLEAR_VARS)
- #need modify here
- LOCAL_MODULE := libmsc
- LOCAL_SRC_FILES :=../3rd-apk/IFlyService/libmsc.so
- #need modify here
- LOCAL_MODULE_TAGS := optional
- LOCAL_MODULE_CLASS := SHARED_LIBRARIES
- LOCAL_MODULE_SUFFIX:=$(HOST_JNILIB_SUFFIX)
- LOCAL_CERTIFICATE:=platform
- LOCAL_PRELINK_MODULE := false
- include $(BUILD_PREBUILT)
- ############################################
- #copy many so files
- #===========================================
- include $(CLEAR_VARS)
- LOCAL_MODULE := BaiduInput_Pad
- LOCAL_MODULE_PATH := $(TARGET_OUT)/app/
- LOCAL_SRC_FILES :=../3rd-apk/BaiduIME/BaiduInput_Pad.apk
- LOCAL_MODULE_TAGS := optional
- LOCAL_MODULE_CLASS := APPS
- LOCAL_MODULE_SUFFIX:=$(COMMON_ANDROID_PACKAGE_SUFFIX)
- LOCAL_JNI_SHARED_LIBRARIES:= ../3rd-apk/BaiduIME/libkpenoem_api_so.so \
- ../3rd-apk/BaiduIME/libtmfe30.so \
- ../3rd-apk/BaiduIME/libinputcoreoem-2.so
- LOCAL_CERTIFICATE:=platform
- LOCAL_SDK_VERSION := current
- include $(BUILD_PREBUILT)
- #==========================================
- #make exe file
- include $(CLEAR_VARS)
- LOCAL_SRC_FILES:= xxx.c
- LOCAL_MODULE := TestBusy
- LOCAL_MODULE_TAGS := eng
- LOCAL_STATIC_LIBRARIES := libcutils libc
- #EXTRA_LDLIBS := -lpthread # -lrt
- #EXTRA_CFLAGS := -DRUN_IN_TARGET
- LOCAL_MODULE_CLASS :=EXECUTABLES
- include $(BUILD_EXECUTABLE)
- #==========================================
- #cp zip file; or other txt jpg db files
- LOCAL_PATH:= $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_MODULE := data-bak
- LOCAL_SRC_FILES := data-bak.zip
- LOCAL_MODULE_PATH := $(TARGET_OUT)/../recovery-bak
- LOCAL_MODULE_TAGS := optional
- LOCAL_MODULE_CLASS := SHARED_LIBRARIES
- LOCAL_MODULE_SUFFIX:= .zip
- #LOCAL_MODULE_SUFFIX:= .txt
- LOCAL_CERTIFICATE:= PRESIGNED
- LOCAL_PRELINK_MODULE := false
- include $(BUILD_PREBUILT)
- LOCAL_PATH:= $(call my-dir)
- #cp .jpg file
- #===========================================
- include $(CLEAR_VARS)
- LOCAL_MODULE := bg
- LOCAL_SRC_FILES := overlay/packages/apps/Settings/res/drawable/bg.jpg
- LOCAL_MODULE_PATH := $(TARGET_OUT)/../data/data/com.lenovo.nebula.settings/files
- LOCAL_MODULE_TAGS := optional
- LOCAL_MODULE_CLASS := SHARED_LIBRARIES
- LOCAL_MODULE_SUFFIX:= .jpg
- #platform shared media PRESIGNED
- LOCAL_CERTIFICATE:= PRESIGNED
- LOCAL_PRELINK_MODULE := false
- include $(BUILD_PREBUILT)
- #cp .mp4 file
- #===========================================
- include $(CLEAR_VARS)
- LOCAL_MODULE := oobeletvintroduce
- LOCAL_SRC_FILES := packages/apps/OOBE3D/res/drawable/oobeletvintroduce.mp4
- LOCAL_MODULE_PATH := $(TARGET_OUT)/app
- LOCAL_MODULE_TAGS := optional
- LOCAL_MODULE_CLASS := SHARED_LIBRARIES
- LOCAL_MODULE_SUFFIX:= .mp4
- #platform shared media PRESIGNED
- LOCAL_CERTIFICATE:= PRESIGNED
- LOCAL_PRELINK_MODULE := false
- include $(BUILD_PREBUILT)
注意:LOCAL_PATH:= $(call my-dir) 这个变量,在一个Android.MK文件里仅仅能有一个。
多个LOCAL_PATH会出现错误。
而include $(CLEAR_VARS)
和include $(BUILD_PREBUILT)
能够是多个。
在 tid=11" title="Android" style="color:rgb(179,43,213)">Android
假设有源代码的话,能够直接跟你自己的代码一去编译成动态库so。可是假设没有源代码的话,你就必须在自己的动态库so里面将别人生成好的静态库导入进来一起编译了。
我在编译的时候遇到了不少问题。我认为有必要进行总结一下。
以下我以一个简单的实际样例来解说怎样在动态库中导入静态库。
静态库中的源码有两个文件:static.h, static.c,有一个add方法
static.h
#include <stdio.h>
int add(int x, int y);
static.c
#include "static.h"
int add(int x, int y)
{
return x + y;
}
将它编译成静态库。Android.mk例如以下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := static_add
LOCAL_SRC_FILES := static.c
include $(BUILD_STATIC_LIBRARY)
注意编译静态库的时候,必须有一个Application.mk文件:
APP_MODULES:=static_add
APP_MODULES的值应该和Android.mk中的LOCAL_MODULE的值保持一样。
然后调用ndk-build进行编译生成libstatic_add.a静态库。
hejinlai_iMac:jni hejinlai$ ndk-build
Prebuilt : libstatic_add.a <= jni/
生成静态库后,然后编写动态库中的源码: share.h share.c
share.h
#include <stdio.h>
int test_add(int x, int y);
share.c
1234567 #include "share.h"
#include "static.h"
int test_add(int x, int y)
{
// 调用static里面的方法
return add(x, y);
}
编写导入静态库的Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := static_add
LOCAL_SRC_FILES := libstatic_add.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := share_add
LOCAL_STATIC_LIBRARIES := static_add
LOCAL_SRC_FILES := share.c
include $(BUILD_SHARED_LIBRARY)
注意上面生成的libstatic_add.a必须跟Android.mk放在同一文件夹下,否则须要填写对应的路径,然后进行编译:
hejinlai_iMac:jni hejinlai$ ndk-build
Compile thumb : share_add <= share.c
Prebuilt : libstatic_add.a <= jni/
SharedLibrary : libshare_add.so
Install : libshare_add.so => libs/armeabi/libshare_add.so
提示so编译成功。
须要注意的是我这边share.c和static.c放在同一文件夹下,假设放在不同的文件夹下,须要指定
LOCAL_C_INCLUDES链接到对应的路径。
一、Eclipse中集成第三方jar包及.so动态库
MyMapsproject下创建文件夹libs以及libs/armeabi,把baidumapapi.jar放在的libs/文件夹下,把libBMapApiEngine_v1_3_1.so放在libs/armeabi/下。
Eclipse中把第三方jar包baidumapapi.jar打包到MyMaps的步骤:
1. 右击project。选择Properties;
2. Java Build Path,选择Libraries。
3. Libraries页面点击右面button“Add Library…”;
4. 选择“User Library”,点击“Next”;
5. 点击“User Libraries”button;
6. 在弹出界面中,点击“New…”;
7. 输入“User library name”。点击“OK”确认;
8. 返回之后。选择刚刚创建的User library,右面点击“AddJARs”;
9. 选择MyMaps/libs/下的baidumapapi.jar。
10. 确认,返回。
这样。编译之后。该jar包就会被打进MyMaps.apk中,libBMapApiEngine_v1_3_1.so也被打包在lib/armeabi/中。
程序执行过程中,libBMapApiEngine_v1_3_1.so被放在/data/data/<yourAppPackage>/lib/下,载入动态库时系统会从程序的该lib/文件夹下查找.so库。
二、源代码中集成第三方集成jar包及.so动态库
Android源代码中MyMaps放在packages/apps下。MyMaps下创建文件夹libs以及libs/armeabi。并把baidumapapi.jar放在libs/,把libBMapApiEngine_v1_3_1.so放在libs/armeabi。
2.1 改动Android.mk文件
Android.mk文件例如以下:
plaincopy
- LOCAL_PATH:= $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_MODULE_TAGS := optional
- LOCAL_STATIC_JAVA_LIBRARIES := libbaidumapapi
- LOCAL_SRC_FILES := $(call all-subdir-java-files)
- LOCAL_PACKAGE_NAME := MyMaps
- include $(BUILD_PACKAGE)
- ##################################################
- include $(CLEAR_VARS)
- LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES :=libbaidumapapi:libs/baidumapapi.jar
- LOCAL_PREBUILT_LIBS :=libBMapApiEngine_v1_3_1:libs/armeabi/libBMapApiEngine_v1_3_1.so
- LOCAL_MODULE_TAGS := optional
- include $(BUILD_MULTI_PREBUILT)
- # Use the following include to make our testapk.
- include $(callall-makefiles-under,$(LOCAL_PATH))
1 集成jar包
LOCAL_STATIC_JAVA_LIBRARIES取jar库的别名。能够随意取值;
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES指定prebuiltjar库的规则,格式:别名:jar文件路径。
注意:别名一定要与LOCAL_STATIC_JAVA_LIBRARIES里所取的别名一致。且不含.jar;jar文件路径一定要是真实的存放第三方jar包的路径。
编译用BUILD_MULTI_PREBUILT。
2 集成.so动态库
LOCAL_PREBUILT_LIBS指定prebuilt so的规则,格式:别名:so文件路径。
注意:别名一般不可改变,特别是第三方jar包使用.so库的情况,且不含.so;so文件路径一定要是真实的存放第三方so文件的路径。
编译拷贝用BUILD_MULTI_PREBUILT。
2.2 增加到GRANDFATHERED_USER_MODULES
在文件user_tags.mk中,把libBMapApiEngine_v1_3_1增加到GRANDFATHERED_USER_MODULES中
plaincopy
- GRANDFATHERED_USER_MODULES += \
- … \
- libBMapApiEngine_v1_3_1
user_tags.mk能够是build/core下的。也能够是$(TARGET_DEVICE_DIR)下的。推荐改动$(TARGET_DEVICE_DIR)下的。
2.3 编译结果
MyMaps.apk编译生成在out/target/product/<YourProduct>/system/app/下;
libBMapApiEngine_v1_3_1.so放在out/target/product/<YourProduct>/system/lib/下。这也是系统载入动态库时搜索的路径。
2.4 源代码中预编译静态库
測试通过
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS)
LOCAL_MODULE := libKL15APIA
#LOCAL_SRC_FILES := libKL15APIA.a
LOCAL_PREBUILT_LIBS := libKL15APIA.a
LOCAL_MODULE_TAGS := optional
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
LOCAL_CERTIFICATE:=platform
include $(BUILD_MULTI_PREBUILT)
參考博文:http://blog.csdn.net/zhangchiytu/article/details/6424910