Android 源码中如何添加 C C++、Java 库
1.添加 C/C++ 程序库
1.1 源码方式添加
在 device/Jelly/Rice14/
目录下创建以下的目录和文件
libmymath
├── Android.bp
├── my_math.cpp
└── my_math.h
libmymath 是一个动态库。其 Android.bp
内容如下:
cc_library_shared {
name: "libmymath",
srcs: ["my_math.cpp"],
export_include_dirs: ["."],
product_specific: true,
}
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.cpp 内容如下:
#include "my_math.h"
int my_add(int a, int b)
{
return a + b;
}
int my_sub(int a, int b)
{
return a - b;
}
接着修改我们之前添加的 hello 项目:
修改 hello.cpp
#include <cstdio>
#include "my_math.h" //添加头文件
int main()
{
printf("Hello Android %d \n", my_add(1,2)); //添加函数调用
return 0;
}
修改 Android.bp:
cc_binary {
name: "hello",
srcs: ["hello.cpp"],
cflags: ["-Werror"],
product_specific: true,
shared_libs: ["libmymath"