NDK环境搭建起来挺费劲,既然Android Studio作为一个IDE集成了NDK,可以编译出so供Java层调用native代码,那能不能使用它来编译可执行的二进制呢?答案是当然可以了,而且挺方便的。
具体步骤:
- 创建一个C++工程

- 在cpp目录下新建一个文件

- 在新创建的native-hello.cpp文件里面编写要编译的二进制对应的代码
cpp
#include <stdio.h>
int main()
{
printf("hello native\n");
return 0;
}
- 修改cpp文件同级的CMakeLists.txt文件,在里面加上下面这一段
bash
add_executable(native-hello
native-hello.cpp)
- 进行编译构建,构建成功后,会在(Project Files){项目名称}/app/build/intermediates/cxx/Debug/xxxxxxxx/obj/{CPU架构}/目录下生成对应的二进制文件native-hello

- 将该二进制推送到Android设备上,添加权限后,便可以执行。