NDK 编译的三种方式

简介: 通过 Android Studio 默认的方式创建带有 native 方法的类,build 项目。

作者:字节流动

来源:https://blog.csdn.net/Kennethdroid/article/details/86418725


通过 Android Studio 默认的方式

创建带有 native 方法的类,build 项目。

image.png

image.png

image.png

image.png

生成与类名相关的 .h 文件。

进入 app -> build -> intermediates -> classes -> debug 目录下

执行: javah com.haohao.hellojni.MyJNI (先配置好 JDK 的环境变量),生成 com_haohao_hellojni_MyJNI.h 文件

image.png

创建 cpp 文件。

在 main 文件夹下,新建 jni 目录,剪切 .h 文件到 jni 目录下,创建 hello.cpp 文件

image.png

hello.cpp

image.png

配置 build.gradle 文件。

修改 app/build.gradle 文件, muduleName 为引入的 .so name , 直接运行项目,安装 apk ,运行就 OK 了

image.png

生成的 .so 文件位置。

image.png

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 文件。

image.png

配置项目工程。

在 main 目录下新建 jniLibs 目录,并拷贝 armeabi arm64-v8a armeabi-v7a 文件夹,运行 proj 。

image.png

image.png

通过 CMake 工具。

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

通过 IDE 自动构建

创建项目时,勾选 Include C++ support

image.png

选择默认的 Toolchain Default

image.png

AS 自动生成 CMakeLists.txt 文件(CMake 构建脚本)


image.png

image.png

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 。配置完成之后,同步,运行。

image.png

手动构建

新建一个工程,创建 native 类,快捷键 Alt + Enter ,自动创建 jni 目录和相应的 .cpp 文件。

image.png

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

image.png

选择脚本文件的路径。

image.png

app/build.gradle 会自动同步。同步完成后,运行项目。


image.png


NDK 开发系列文章:


「视频云技术」你最值得关注的音视频技术公众号,每周推送来自阿里云一线的实践技术文章,在这里与音视频领域一流工程师交流切磋。

阿里云社区.png

相关文章
|
Android开发
【Android 安装包优化】动态库打包配置 ( “armeabi-v7a“, “arm64-v8a“, “x86“, “x86_64“ APK 打包 CPU 指令集配置 | NDK 完整配置参考 )
【Android 安装包优化】动态库打包配置 ( “armeabi-v7a“, “arm64-v8a“, “x86“, “x86_64“ APK 打包 CPU 指令集配置 | NDK 完整配置参考 )
1016 0
【Android 安装包优化】动态库打包配置 ( “armeabi-v7a“, “arm64-v8a“, “x86“, “x86_64“ APK 打包 CPU 指令集配置 | NDK 完整配置参考 )
|
1月前
|
Java Linux 开发工具
NDK与JNI开发(1)ndk_build方式开发
NDK与JNI开发(1)ndk_build方式开发
NDK与JNI开发(1)ndk_build方式开发
|
Java Android开发 C++
【Android NDK 开发】Android Studio 使用 CMake 导入动态库 ( 构建脚本路径配置 | 指定动态库查找路径 | 链接动态库 )(二)
【Android NDK 开发】Android Studio 使用 CMake 导入动态库 ( 构建脚本路径配置 | 指定动态库查找路径 | 链接动态库 )(二)
476 0
【Android NDK 开发】Android Studio 使用 CMake 导入动态库 ( 构建脚本路径配置 | 指定动态库查找路径 | 链接动态库 )(二)
|
9月前
|
搜索推荐 Java Linux
记一次编译Android源码
记一次编译Android源码
|
Android开发
【Android NDK 开发】Android Studio 使用 CMake 导入静态库 ( CMake 简介 | 构建脚本路径配置 | 引入静态库 | 指定静态库路径 | 链接动态库 )(二)
【Android NDK 开发】Android Studio 使用 CMake 导入静态库 ( CMake 简介 | 构建脚本路径配置 | 引入静态库 | 指定静态库路径 | 链接动态库 )(二)
345 0
|
Java Android开发
编译安卓项目时报错
编译安卓项目时报错
327 0
编译安卓项目时报错
|
XML Android开发 数据格式
|
Java 编译器 Linux
【CMake】CMake 引入 ( Android Studio 创建 Native C++ 工程 | C/C++ 源码编译过程 | Makefile 工具 | CMake 引入 )(二)
【CMake】CMake 引入 ( Android Studio 创建 Native C++ 工程 | C/C++ 源码编译过程 | Makefile 工具 | CMake 引入 )(二)
270 0
【CMake】CMake 引入 ( Android Studio 创建 Native C++ 工程 | C/C++ 源码编译过程 | Makefile 工具 | CMake 引入 )(二)
|
Java Android开发 C++
【CMake】CMake 引入 ( Android Studio 创建 Native C++ 工程 | C/C++ 源码编译过程 | Makefile 工具 | CMake 引入 )(一)
【CMake】CMake 引入 ( Android Studio 创建 Native C++ 工程 | C/C++ 源码编译过程 | Makefile 工具 | CMake 引入 )(一)
271 0
【CMake】CMake 引入 ( Android Studio 创建 Native C++ 工程 | C/C++ 源码编译过程 | Makefile 工具 | CMake 引入 )(一)
|
编译器 Android开发 C++
【Android NDK 开发】Android Studio 使用 CMake 导入静态库 ( CMake 简介 | 构建脚本路径配置 | 引入静态库 | 指定静态库路径 | 链接动态库 )(一)
【Android NDK 开发】Android Studio 使用 CMake 导入静态库 ( CMake 简介 | 构建脚本路径配置 | 引入静态库 | 指定静态库路径 | 链接动态库 )(一)
543 0