Android系统编译常用配置及指令

2023-10-09  本文已影响0人  Amber_9

有时间来整理一下Android系统编译的内容,本文从三方面展开:编译过程、常用的编译配置和命令

一. 编译过程步骤

1.初始化编译环境

source ./build/envsetup.sh它主要作用:

2. 选择编译目标 lunch xxtarget

image.png
image.png

3. 编译 make -j8

Android根目录的Makefile include进来一个编译核心文件:build/make/core/main.mk
main.mk主要功能:

二. 常用配置

Android 7.0 之后逐渐用 Android.bp 替换 Android.mk

1.编译动态库

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
cJSON.c
LOCAL_MODULE:= libmycjson
include $(BUILD_SHARED_LIBRARY)
cc_library_shared { //编译成动态库,类似于 Android.mk 中的 BUILD_SHARED_LIBRARY
name: "libcamera_client",
aidl: {
export_aidl_headers: true, //是否导出 aidl 头文件路径
local_include_dirs: ["aidl"], //将[指定的目录列表]加入 aidl 搜索头文件路径。
// [“aidl”]表示当前目录下的 aidl 目录
include_dirs: [ //指定搜索外部 aidl 头文件的路径
"frameworks/native/aidl/gui",
],
},
srcs: [ //源文件,类似于 Android.mk 中的 LOCAL_SRC_FILES
// AIDL files for camera interfaces
// The headers for these interfaces will be available to any modules that
// include libcamera_client, at the path "aidl/package/path/BnFoo.h"
":libcamera_client_aidl", //引用了另外一个模块中定义的源文件。在当前 bp 文件的第 81 行
// Source for camera interface parcelables, and manually-written interfaces
"Camera.cpp",
"CameraMetadata.cpp",
"CameraParameters.cpp",
"CaptureResult.cpp",
"CameraParameters2.cpp",
"ICamera.cpp",
"ICameraClient.cpp",
"ICameraRecordingProxy.cpp",
"ICameraRecordingProxyListener.cpp",
"camera2/CaptureRequest.cpp",
"camera2/OutputConfiguration.cpp",
"camera2/SessionConfiguration.cpp",
"camera2/SubmitInfo.cpp",
"CameraBase.cpp",
"CameraUtils.cpp",
"VendorTagDescriptor.cpp",
],
shared_libs: [ //编译所依赖的动态库,类似于 Android.mk 中的 LOCAL_SHARED_LIBRARIES
"libcutils",
"libutils",
"liblog",
"libbinder",
"libgui",
"libcamera_metadata",
"libnativewindow",
],
include_dirs: [//用户指定的头文件查找路径,类似于 Android.mk 中的 LOCAL_C_INCLUDES
"system/media/private/camera/include",
"frameworks/native/include/media/openmax",
],
export_include_dirs: [//将当前的路径导出,如果有其他模块引用当前模块,就不需要指定头文件路径
"include",
"include/camera"
],
export_shared_lib_headers: ["libcamera_metadata"],//将 libcamera_metadata 模块对应的头文件路径列表导出
cflags: [//编译 flag,类似于 Android.mk 中的 LOCAL_CFLAGS
"-Werror",
"-Wall",
"-Wextra",
],
}

2. 编译可执行程序

LOCAL_PATH:= $(call my-dir)
curdir=$(LOCAL_PATH)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
main.c
LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/libcjson
LOCAL_SHARED_LIBRARIES += \
libmycjson \
LOCAL_CFLAGS += \
-Wno-error \
-Wno-unused-parameter
LOCAL_MODULE:= cjson_test
LOCAL_MODULE_TAGS := optional
LOCAL_MULTILIB := 64
include $(BUILD_EXECUTABLE)
include $(curdir)/libcjson/Android.mk
cc_binary {
name: "reboot",
srcs: ["reboot.c"],
shared_libs: ["libcutils"],
cflags: ["-Werror"],
}

3.编译Java动态库

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_JAVA_LIBRARIES := libmytest
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_MODULE := TestDemo
include $(BUILD_JAVA_LIBRARY)
java_library { //编译成 java 库
name: "services",//编译出的模块的名称
dex_preopt: {
app_image: true, //是否为当前模块生成 app image(.art)文件
profile: "art-profile",//指定与当前 Android.bp 相关的配置文件路径
},
srcs: [ //源文件
"java/**/*.java",
],
// The convention is to name each service module 'services.$(module_name)'
static_libs: [
//编译所依赖的静态库
"services.core",
"services.accessibility",
"services.appwidget",
"services.autofill",
"services.backup",
"services.companion",
"services.coverage",
"services.devicepolicy",
"services.midi",
"services.net",
"services.print",
"services.restrictions",
"services.usage",
"services.usb",
"services.voiceinteraction",
"android.hidl.base-V1.0-java",
],
libs: [//链接的动态库
"android.hidl.manager-V1.0-java",
],
// Uncomment to enable output of certain warnings (deprecated, unchecked)
//javacflags: ["-Xlint"],
}

4. apk(含源码,不含源码的可参考预编译)

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, app/src/main/java)
LOCAL_PACKAGE_NAME := StorageTest
LOCAL_SDK_VERSION := current
#LOCAL_PROGUARD_FLAG_FILES := proguard.flags
LOCAL_CERTIFICATE := platform
LOCAL_USE_AAPT2 := true
# 指定 Manifest 文件
LOCAL_MANIFEST_FILE := app/src/main/AndroidManifest.xml
LOCAL_RESOURCE_DIR := \
$(addprefix $(LOCAL_PATH)/, app/src/main/res)
# 需要的 jar,否则无法找到系统相关资源
LOCAL_STATIC_JAVA_LIBRARIES := \
androidx.appcompat_appcompat \
include $(BUILD_PACKAGE)
include $(call all-makefiles-under,$(LOCAL_PATH))
android_app {
name: "Music",
srcs: [
"src/**/*.java",
"src/com/android/music/IMediaPlaybackService.aidl",
],
sdk_version: "current",
product_specific: true, //安装到 product 分区
optimize: {
proguard_flags_files: ["proguard.flags"],
},
certificate: "platform",
}

5.预编译

#预编译配置文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := Vendor_5135_Product_0005.idc
LOCAL_SRC_FILES := Vendor_5135_Product_0005.idc
LOCAL_MODULE_CLASS := ETC
LOCAL_MODULE_PATH := $(TARGET_OUT)/usr/idc
include $(BUILD_PREBUILT)
# 预编译动态库
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES :=libglib-2.0.so
LOCAL_MODULE_SUFFIX := .so
LOCAL_MODULE := libglib-2.0
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MULTILIB := 32
include $(BUILD_PREBUILT)
# 预编译可执行程序
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := busybox_aarch64
LOCAL_MODULE := busybox_aarch64
LOCAL_MODULE_CLASS := EXECUTABLES
LOCAL_MODULE_TAGS := optional
include $(BUILD_PREBUILT)
#预编译APK
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Hello
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
#定义编译完成之后的类型:除了APPS,还有ETC,EXECUTABLES(.bin)SHARED_LIBRARIES(.so) 等选择值 
LOCAL_MODULE_CLASS := APPS

#定义编译完成之后模块的后缀
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)

#定义使用原app签名可用选择项platform,shared,media
LOCAL_CERTIFICATE := PRESIGNED

#将apk编进“/system/priv-app/目录”,如果为false,或者不加这句话,就会编进“/system/app” 目录, 二者区别在于前者的权限要高于后者,即不可卸载
LOCAL_PRIVILEGED_MODULE := true

#不进行odex化
LOCAL_DEX_PREOPT := false

# 定义编译出来的目标文件,如动态库,静态库,jar包以及apk
include $(BUILD_PREBUILT)
//预编译etc配置文件
prebuilt_etc {
name: "Vendor_my_idc",
src: "Vendor_5135_Product_0005.idc",
filename_from_src: true,
sub_dir:"usr",
}
//预编译可执行文件
cc_prebuilt_binary {
name: "logpersist.start",
srcs: ["logpersist"],
init_rc: ["logcatd.rc"],
required: ["logcatd"],
symlinks: [
"logpersist.stop",
"logpersist.cat",
],
strip: {
none: true,
},
}
//预编译动态库
cc_prebuilt_library_shared {
name: "libmycjson-pre.so",
srcs: ["x86_64/libmycjson-pre.so"],
compile_multilib : “64”,
}

三. 常用命令

1. 编译命令

1.1 m命令

1.2make命令

2. 编译配置和搜索命令

3. 导航命令

上一篇下一篇

猜你喜欢

热点阅读