在 aosp/device/mycompamy/product/
目录下创建以下的目录和文件:
.
├── AndroidProducts.mk
├── libmath
│ ├── Android.mk
│ ├── my_math.c
│ └── my_math.h
├── libmath2
│ ├── Android.mk
│ ├── my_math2.c
│ └── my_math2.h
├── main
│ ├── Android.mk
│ └── main.c
└── myaosp.mk
其中 AndroidProducts.mk myaosp.mk 是 配置product 中添加的 product 配置文件。
libmath 是一个 C 语言动态库。其 Android.mk 内容如下:
# 固定内容
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
# 源码位置
LOCAL_SRC_FILES:= \
my_math.c
# 模块名
LOCAL_MODULE:= libmymathmk
# 编译为 64 为库
LOCAL_MULTILIB := 64
LOCAL_MODULE_TAGS := optional
# 编译到vender
LOCAL_VENDOR_MODULE := true
# 表示这是一个动态库
include $(BUILD_SHARED_LIBRARY)
my_math.h 内容如下:
#ifndef __MY_MATH_H__
#define __MY_MATH_H__
int my_add(int a, int b);
int my_sub(int a, int b);
#endif
my_math.c 内容如下:
#include "my_math.h"
int my_add(int a, int b)
{
return a + b;
}
int my_sub(int a, int b)
{
return a - b;
}
接下来我们就可以在 libmath 目录下执行 mm 命令,编译当前模块:
mm
......
device/mycompamy/product/myaosp.mk was modified, regenerating...
out/soong/Android-myaosp.mk was modified, regenerating...
[100% 15/15] build out/target/product/my_generic_x86_64/obj/SHARED_LIBRARIES/libmymathmk_intermediates/lib
#### build completed successfully (01:16 (mm:ss)) ####
libmath2 目录下:
Android.mk:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
my_math2.c \
LOCAL_MODULE:= libmymath2mk
LOCAL_MULTILIB := 64
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_MODULE_TAGS := optional
LOCAL_VENDOR_MODULE := true
# 静态库
include $(BUILD_STATIC_LIBRARY)
my_math2.c:
#include "my_math2.h"
int my_multi(int a, int b)
{
return a * b;
}
int my_div(int a, int b)
{
return a / b;
}
my_math2.h:
#ifndef __MY_MATH2_H__
#define __MY_MATH2_H__
int my_multi(int a, int b);
int my_div(int a, int b);
#endif
同样,通过 mm 命令编译。
main 目录下:
main.c
#include <stdio.h>
#include "my_math.h"
#include "my_math2.h"
int main(int argc, char *argv[])
{
printf("a + b = %d\n", my_add(30, 40));
printf("a * b = %d\n", my_multi(30, 40));
return 0;
}
Android.bp:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
main.c
# 指定头文件
LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/../libmath \
$(LOCAL_PATH)/../libmath2
LOCAL_SHARED_LIBRARIES += \
libmymathmk
LOCAL_STATIC_LIBRARIES += \
libmymath2mk
LOCAL_VENDOR_MODULE := true
LOCAL_CFLAGS += \
-Wno-error \
-Wno-unused-parameter
LOCAL_MODULE:= mymathtestmk
LOCAL_MODULE_TAGS := optional
LOCAL_MULTILIB := 64
include $(BUILD_EXECUTABLE)
在 aosp/device/mycompamy/product/myaosp.mk
中添加:
PRODUCT_PACKAGES += mymathtestmk
接下来编译系统:
source build/envsetup.sh
lunch myaosp-eng
make -j16
编译完成启动虚拟机后,就可以通过 adb shell 运行我们的 hello 程序了
adb shell mymathtestmk
执行结果如下图所示:
将上文的 libmath/Android.mk 修改为 Android.bp:
cc_library_shared {
name: "libmymathbp",
srcs: ["my_math.c"],
export_include_dirs: ["."],
vendor: true,
compile_multilib: "64",
}
libmath2/Android.mk 修改为 Android.bp:
cc_library_static {
name: "libmymath2bp",
srcs: ["my_math2.c"],
export_include_dirs: ["."],
compile_multilib: "64",
}
将 main/Android.mk 修改为 Android.bp
cc_binary {
name: "mymathtestbp",
srcs: ["main.c"],
shared_libs: ["libmymath"],
static_libs: ["libmycjson"],
cflags: ["-Wno-error"] + ["-Wno-unused-parameter"],
}
其余操作和 Android.mk 方式均相同,需要注意模块名的最后两个字符我改成了 bp。