Android系統預製APK、so和bin的方法
一、Android系統中預製APK
1. 預製有原始碼的APK的Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional // optional:指該模組在所有版本下都編譯
LOCAL_SRC_FILES := $(call all-java-files-under, src)
// 獲取所有子目錄的java檔案
LOCAL_PACKAGE_NAME := TestDemo // 包名
LOCAL_CERTIFICATE := platform //簽名
LOCAL_PROGUARD_ENABLED := disabled
include $(BUILD_PACKAGE)
// 編譯 APK
2. 預製沒有原始碼的APK的Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := SogouInput
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_CERTIFICATE:=PRESIGNED /
#LOCAL_CERTIFICATE:=platform // 簽名,注意,platform是對apk重新進行簽名
LOCAL_SRC_FILES := $(LOCAL_MODULE)$(COMMON_ANDROID_PACKAGE_SUFFIX)
include $(BUILD_PREBUILT)
注:device.mk中需要新增PRODUCT_PACKAGES += LOCAL_MODULE ,
這樣在Android程式碼全Build時,APK才會預製到system.img裡面。
二、Android系統中預製so
1. 預製有原始碼的so的Android.mk
LOCAL_PATH := $(call my-dir)
sources := ***
cincludes := ***
LOCAL_MODULE := lib
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(sources)
LOCAL_C_INCLUDES := $(cincludes)LOCAL_32_BIT_ONLY := true
LOCAL_CFLAGS := -DMSOS_TYPE_LINUX -DENABLE_LOGGER
LOCAL_SHARED_LIBRARIES := *** ***
LOCAL_MODULE_TARGET_ARCH:= arm64
//如果64換成32就是32bit
LOCAL_MULTILIB := 64 //如果64換成32就是32bit
include $(BUILD_SHARED_LIBRARY)
2. 預製沒有原始碼的so的Android.mk
// 如果同時預製32位和64位的so
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_SUFFIX := .so
LOCAL_MODULE := libabc
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_SRC_FILES_arm :=lib/abc.so
LOCAL_SRC_FILES_arm64 :=lib64/libabc.so
LOCAL_MODULE_TARGET_ARCHS:= arm arm64
LOCAL_MULTILIB := both
include $(BUILD_PREBUILT)
//如果只預製32 bit的so
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_SUFFIX := .so
LOCAL_MODULE := libabc
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_SRC_FILES_arm :=lib/libabc.so
LOCAL_MODULE_TARGET_ARCHS:= arm
LOCAL_MULTILIB := 32
include $(BUILD_PREBUILT)
//如果只想預製64 bit的so
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_SUFFIX := .so
LOCAL_MODULE := libabc
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_SRC_FILES_arm64 :=lib64/libabc.so
LOCAL_MODULE_TARGET_ARCHS:= arm64
LOCAL_MULTILIB := 64
include $(BUILD_PREBUILT)
注:so檔案涉及到ABI的相容性問題,詳細可參考如下部落格的文章:
三、Android系統中預製bin
1. 預製有原始碼的bin的Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= netcfg.c
LOCAL_MODULE:= netcfg
LOCAL_SHARED_LIBRARIES := libc libnetutils
include $(BUILD_EXECUTABLE)
2. 預製沒有原始碼的bin的Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := pppd
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := EXECUTABLES
LOCAL_SRC_FILES := $(LOCAL_MODULE)
include $(BUILD_PREBUILT)
3.apk的Android.mk中預製bin
LOCAL_POST_PROCESS_COMMAND := $(shell cp -rf $(LOCAL_PATH)/libs/armeabi/traceroute $(PRODUCT_OUT)/system/bin/opt/bin/)
// 此命令預製的bin apk有讀寫執行的許可權