Android开发主流工具已变成AndroidStudio,新版AS已经支持CMake编译工具,可以用于编译C/C++文件,增强了Android调用jni代码的便捷性。相比于之前繁杂的ndk配置方式,cmake较为简单方便。
1、添加CMake支持
首先使用的AndroidStudio必须是2.2以上版本,在settings–Appearance&Behavior–System Settings–AndroidSDK中,确保下载安装了CMake、LLDB、NDK等必要组件。
使用CMake编译C/C++文件,有两种添加模式:
- 新建项目,在创建项目名称,配置包名时,勾选包含C/C++代码,如图
.
- 在已有的项目中添加C/C++代码文件,只需要在app.gradle文件中配置如下代码
defaultConfig{
//配置Cmake运行参数,在defaultconfig节点下配置
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
}
}
}
//关联CMake的程序清单文件,path 指向cmake文件路径,此处为项目工程根目录下。
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
CMakeLists.txt文件如下
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.AndroidStudio开始支持Cmake了,ndk感觉挺费劲的,这个是不是好玩点,,这里是要生成的库的文件名 libtest.so
test
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.对应的C文件的目录位置
src/main/cpp/Test.c)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.指定依赖库
test
# Links the target library to the log library
# included in the NDK.关联日志记录库文件,在ndk目录中
${log-lib} )
如上文件中,主要注意add_library中配置你要生成so文件的名称,以及C/C++文件的路径;而target_link_libraries中指定依赖库的文件名。
如此clean project 然后make,就可以生成so文件,被Android上层java调用。类似如下:
private native static void open(String path);
public native void close();
//加载lib库的so文件,要看请lib下面生成的so文件的名字,libtest.so
static {
System.loadLibrary("test");
}
在 Android 中如何调用 C 语言
当我们的 Java 需要调用 C 语言的时候可以通过 JNI 的方式,Java Native Interface。Android 提供了对 JNI 的支
持,因此我们在 Android 中可以使用 JNI 调用 C 语言。在 Android 开发目录的 libs 目录下添加 xxx.so 文件,不过 xxx.so
文件需要放在对应的 CPU 架构名目录下,比如 armeabi,x86 等。
在 Java 代码需要通过 System.loadLibrary(libName);加载 so 文件。同时 C 语言中的方法在 java 中必须
以 native 关键字来声明。普通 Java 方法调用这个 native 方法接口,虚拟机内部自动调用 so 文件中对应的方法。