android配置jni cmake,jni开发------安装及配置 NDK 和 CMake

输入“CMakeLists.txt”作为文件名,然后点击 OK。

55fd2b2273b5a8b4531f72773c469d6e.gif

jni.png (96.67 KB, 下载次数: 1)

建立的各个文件路径

2020-6-27 17:37 上传

现在,您可以通过添加 CMake 命令来配置您的构建脚本。要指示 CMake 根据原生源代码创建原生库,请向您的构建脚本添加 cmake_minimum_required() 和 add_library() 命令:

# Sets the minimum version of CMake required to build your native library.

# This ensures that a certain set of CMake features is available to

# your build.

cmake_minimum_required(VERSION 3.4.1)

# Specifies a library name, specifies whether the library is STATIC or

# SHARED, and provides relative paths to the source code. You can

# define multiple libraries by adding multiple add_library() commands,

# and CMake builds them for you. When you build your app, Gradle

# automatically packages shared libraries with your APK.

add_library( # Specifies 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 )

在使用 add_library() 向 CMake 构建脚本添加源代码文件或库时,Android Studio 还会在您同步项目后在 Project 视图中显示相关的头文件。不过,为了让 CMake 能够在编译时找到头文件,您需要向 CMake 构建脚本添加 include_directories() 命令,并指定头文件的路径:

add_library(...)

# Specifies a path to native header files.

include_directories(src/main/cpp/include/)

向 CMake 构建脚本添加 find_library()命令以找到 NDK 库并将其路径存储为一个变量。您可以使用此变量在构建脚本的其他部分引用 NDK 库。并会将其路径存储在 log-lib 中:

find_library( # Defines the name of the path variable that stores the

# location of the NDK library.

log-lib

# Specifies the name of the NDK library that

# CMake needs to locate.

log )

为了让您的原生库能够调用 log 库中的函数,您需要使用 CMake 构建脚本中的 target_link_libraries() 命令来关联这些库:

find_library(...)

# Links your native library against one or more other native libraries.

target_link_libraries( # Specifies the target library.

native-lib

# Links the log library to the target library.

${log-lib} )

关联Gradle

从 IDE 左侧打开 Project 窗格,然后选择 Android 视图。

右键点击您想要关联到原生库的模块(例如 app 模块),然后从菜单中选择 Link C++ Project with Gradle。您会看到一个类似于图 4 所示的对话框。

从下拉菜单中,选择CMake或ndk-build。

a、如果您选择 CMake,请使用 Project Path 旁的字段为您的外部 CMake 项目指定 CMakeLists.txt 脚本文件。

b、如果您选择 ndk-build,请使用 Project Path 旁的字段为您的外部 ndk-build 项目指定 Android.mk脚本文件。如果 Application.mk 文件与您的 Android.mk 文件位于同一目录下,Android Studio 也会包含此文件。

使用 Android Studio 对话框关联外部 C++ 项目。

4.点击 OK。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值