NDK 编译的三种方式

NDK 编译的三种方式

该文章首发于微信公众号“字节流动”

通过 Android Studio 默认的方式

创建带有 native 方法的类,build 项目。
NDK 编译的三种方式
NDK 编译的三种方式
NDK 编译的三种方式
NDK 编译的三种方式

生成与类名相关的 .h 文件。
进入 app -> build -> intermediates -> classes -> debug 目录下
执行: javah com.haohao.hellojni.MyJNI (先配置好 JDK 的环境变量),生成 com_haohao_hellojni_MyJNI.h 文件
NDK 编译的三种方式
创建 cpp 文件。
在 main 文件夹下,新建 jni 目录,剪切 .h 文件到 jni 目录下,创建 hello.cpp 文件
NDK 编译的三种方式
hello.cpp
NDK 编译的三种方式
配置 build.gradle 文件。
修改 app/build.gradle 文件, muduleName 为引入的 .so name , 直接运行项目,安装 apk ,运行就 OK 了
NDK 编译的三种方式
生成的 .so 文件位置。
NDK 编译的三种方式
PS: 未指定 CPU 框架时,AS 会生成支持所有 CPU 框架的 .so 文件。

通过 ndk-build

创建 Android.mkApplication.mk 文件。
新建一个项目,在 app 目录下(任目录下都可以)新建 jni 文件,添加 Android.mkApplication.mk 文件,以及 com_haohao_hellojni_MyJNI.h 文件(运用上一小节的方法生成)。
Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

# 要生成的.so库名称。java代码System.loadLibrary("hello");加载的就是它
LOCAL_MODULE := hello

# C++文件
LOCAL_SRC_FILES := hello.cpp

include $(BUILD_SHARED_LIBRARY)

Application.mk

# 不写 APP_ABI 会生成全部支持的平台,目前支持:armeabi arm64-v8a armeabi-v7a
# APP_ABI := armeabi arm64-v8a armeabi-v7a mips mips64 x86 x86_64
APP_ABI := armeabi arm64-v8a armeabi-v7a

生成 .so 文件。
在 jni 目录下(配置好NDK环境变量)直接执行 ndk-build , 生成 .so 文件。
NDK 编译的三种方式
配置项目工程。
在 main 目录下新建 jniLibs 目录,并拷贝 armeabi arm64-v8a armeabi-v7a 文件夹,运行 proj 。
NDK 编译的三种方式
NDK 编译的三种方式

通过 CMake 工具。

从 Android Studio 2.2 开始,就默认使用 CMake 工具构建 NDK 项目,请确保你的 AS 版本大于 2.2 。

通过 IDE 自动构建

创建项目时,勾选 Include C++ support
NDK 编译的三种方式
选择默认的 Toolchain Default
NDK 编译的三种方式
AS 自动生成 CMakeLists.txt 文件(CMake 构建脚本)
NDK 编译的三种方式
NDK 编译的三种方式
CMakeLists.txt

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library. 
# 指定CMake的最小版本
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 them for you.
# Gradle automatically packages shared libraries with your APK.
# 设置模块名为 native-lib,SHARED 可分享的,以及配置源文件的路径
add_library( # Sets the name of the library. 设置模块名
             native-lib

             # Sets the library as a shared library. 
             SHARED 

             # Provides a relative path to your source file(s). 文件路径
             src/main/cpp/native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries 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.
# 找到 log 本地模块
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 this
# build script, prebuilt third-party libraries, or system libraries.
# 关联 native-lib 模块和 log 模块
target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

在配置 app/build.gradle ,针对特殊平台 abiFilters 。配置完成之后,同步,运行。
NDK 编译的三种方式

手动构建

新建一个工程,创建 native 类,快捷键 Alt + Enter ,自动创建 jni 目录和相应的 .cpp 文件。
NDK 编译的三种方式
native-lib.cpp

#include <jni.h>
#include <string>

extern "C"

JNIEXPORT jstring JNICALL
Java_com_haohao_ndk_1cpp_MyJNI_stringFromJNI(JNIEnv *env, jobject instance) {

    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

在工程根目录下创建 CMakeLists.txt 文件。

# 指定CMake的最小版本
cmake_minimum_required(VERSION 3.4.1)

add_library( # Sets the name of the library. 设置模块名
             native-lib

             # Sets the library as a shared library. 
             SHARED 

             # Provides a relative path to your source file(s). 文件路径
             src/main/cpp/native-lib.cpp )

选择 app modulde ,右击选择Link C++ Project with Gradle
NDK 编译的三种方式
选择脚本文件的路径。
NDK 编译的三种方式
app/build.gradle 会自动同步。同步完成后,运行项目。

NDK 编译的三种方式
NDK 编译的三种方式

联系与交流

微信公众号
NDK 编译的三种方式
个人微信
NDK 编译的三种方式