一、源码编译
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)
源码中编译还可以采用
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_PREBUILT_LIBS := libauthsignapkcommonjni.so
include $(BUILD_MULTI_PREBUILT)
apk中预编译so
include $(CLEAR_VARS)
LOCAL_MODULE := libxcb
LOCAL_SRC_FILES :=libxcb.so
include $(PREBUILT_SHARED_LIBRARY)
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)
或者:
###############################################
include $(CLEAR_VARS)LOCAL_MODULE := NeptuneService.apk
LOCAL_MODULE_PATH := $(TARGET_OUT)/preinstall/common
LOCAL_SRC_FILES := NeptuneService/NeptuneService_V2.02.00_20171220_SIG.apk
LOCAL_MODULE_CLASS := FAKEinclude $(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)
可以是多个。
在Android开发的时候,经常会使用到用c或c++编写的第三方的静态库。如果有源码的话,可以直接跟你自己的代码一去编译成动态库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动态库
MyMaps工程下创建目录libs以及libs/armeabi,把baidumapapi.jar放在的libs/目录下,把libBMapApiEngine_v1_3_1.so放在libs/armeabi/下。
Eclipse中把第三方jar包baidumapapi.jar打包到MyMaps的步骤:
1. 右击工程,选择Properties;
2. Java Build Path,选择Libraries;
3. Libraries页面点击右面按钮“Add Library…”;
4. 选择“User Library”,点击“Next”;
5. 点击“User Libraries”按钮;
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文件如下:
[plain] view 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中
[plain] view 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
android源码中将第三方so编译进apk:
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := tests //android10加上这句才能编译出来
LOCAL_MODULE_INCLUDE_LIBRARY := true //低版本加入这句即可
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_SRC_FILES += \
src/com/darker/fingerservice/aidl/IDarkerFingerService.aidl \
src/com/darker/fingerservice/aidl/IFingerListener.aidl
LOCAL_PACKAGE_NAME := ChinaFingerService
LOCAL_PROGUARD_ENABLED := disabled
LOCAL_CERTIFICATE := platform
LOCAL_DEX_PREOPT := false
ifeq ($(TARGET_CPU_ABI), arm64-v8a)
LOCAL_MULTILIB := 64
LOCAL_TARGET_CPU_ABI := arm64-v8a
else
LOCAL_MULTILIB := 32
LOCAL_TARGET_CPU_ABI := armeabi-v7a
endif
LOCAL_PRIVATE_PLATFORM_APIS := true
LOCAL_STATIC_JAVA_LIBRARIES := libAraTrustFinger \
libtcFingerSDK \
libNBFinger_android_SDK \
libNBFinger_jna_SDK \
libIbScan \
libIbScanJni \
libIbScanUsb
LOCAL_PREBUILT_JNI_LIBS := libs/$(LOCAL_TARGET_CPU_ABI)/libTrustFinger.so \
libs/$(LOCAL_TARGET_CPU_ABI)/libnfiq.so
LOCAL_PREBUILT_JNI_LIBS += \
libs/$(LOCAL_TARGET_CPU_ABI)/libibscanultimatejni.so
include $(BUILD_PACKAGE)