利用Android.bp来编译so库
在SimpleHook模块中,我们使用了以下构建脚本来建议Android.bp
cc_library {
name:"libsecAI",
host_supported: true,
shared_libs:[
"libnativehelper",
"libart",
"liblog",
],
local_include_dirs:[
"include",
],
arch:{
arm: {
srcs: [
"AIHook.cpp",
"HookPage.cpp",
],
},
arm64:{
srcs: [
"AIHook.cpp",
"HookPage.cpp",
],
},
},
cppflags:[
"-DART_STACK_OVERFLOW_GAP_arm=8912",
"-DART_STACK_OVERFLOW_GAP_arm64=8912",
"-DART_STACK_OVERFLOW_GAP_mips=16384",
"-DART_STACK_OVERFLOW_GAP_mips64=16384",
"-DART_STACK_OVERFLOW_GAP_x86=16384",
"-DART_STACK_OVERFLOW_GAP_x86_64=20480",
"-DIMT_SIZE=43",
],
cpp_std:"c++17",
}
可以看出,Android.bp文件的语法与json文件的形式很类似。这里cc_library是用来生成native库的(so包),常用的属性:
- name: 指定了生成的lib包的名字
- host_supported:当该模块依赖的某个模块中存在该属性,则依赖它的模块都需要配置该属性
- local_include_dirs:项目内的头文件目录依赖,这里是相对路径
- cppflag:编译的flag选项,示例中定义了一些工程依赖的宏的定义
- cpp_std: 指定了编译器依赖的语言编译标准
- arch: 用来指定需要的目标编译平台,诸如arm, arm64,x86等,在srcs中指定需要依赖的源码文件(以相对文件的形式给出)
- srcs:常见的还可以直接在最外层用
srcs
属性配置工程源码文件 - include_dirs: 指定依赖的头文件,这里的头文件可以为模块外的头文件,以相对整个Android工程路径给出。
- export_include_dirs: 是否将头文件导出;这使得依赖该模块的模块无需再引用依赖模块的头文件目录。
利用Android.bp来编译Java库
来看下framework模块的bp文件
java_library {
name: "framework",
srcs: [
// From build/make/core/pathmap.mk FRAMEWORK_BASE_SUBDIRS
"core/java/**/*.java",
"graphics/java/**/*.java",
"location/java/**/*.java",
"lowpan/java/**/*.java",
.....
":framework-javastream-protos",
"core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl",
"core/java/android/accessibilityservice/IAccessibilityServiceClient.aidl",
"media/java/android/media/session/ISessionControllerCallback.aidl",
......
":framework-statslog-gen",
],
aidl: {
export_include_dirs: [
// From build/make/core/pathmap.mk FRAMEWORK_BASE_SUBDIRS
"core/java",
"graphics/java",
"location/java",
"lowpan/java",
"media/java",
"media/mca/effect/java",
"media/mca/filterfw/java",
"media/mca/filterpacks/java",
"drm/java",
"opengl/java",
"sax/java",
"telecomm/java",
"telephony/java",
"wifi/java",
"keystore/java",
"rs/java",
],
include_dirs: [
"system/update_engine/binder_bindings",
"system/bt/binder",
"system/security/keystore/binder",
],
},
exclude_srcs: [
// See comment on framework-atb-backward-compatibility module below
"core/java/android/content/pm/AndroidTestBaseUpdater.java",
// See comment on framework-oahl-backward-compatibility module below
"core/java/android/content/pm/OrgApacheHttpLegacyUpdater.java",
],
no_framework_libs: true,
libs: [
"conscrypt",
"okhttp",
"bouncycastle",
"ext",
],
static_libs: [
"framework-protos",
"android.hidl.base-V1.0-java",
"android.hardware.cas-V1.0-java",
],
// Loaded with System.loadLibrary by android.view.textclassifier
required: [
"libtextclassifier",
"libmedia2_jni",
],
javac_shard_size: 150,
dxflags: [
"--core-library",
"--multi-dex",
],
}
首先java_library
说明了编译的是java库,另外还可能有java_library_static
,file_group
等属性,这里面有好多属性,我们重点关注几个选项。
- name:指定了模块名
- static_libs:指定了依赖的静态库(将被打入到jar中)
- libs: 共享库
- srcs:指定模块中含有的java代码,这里也可以指定aidl文件。这里通过
"core/java/**/*.java"
,这种写法来覆盖某个路径下的所有java文件 - aidl: 一般我们会在这里面指定aidl文件相对路径