Android Studio 2.2以上支持了Cmake的配置JNI的相关参数,简化了通过Android.mk配置。并很好的继承了C++的编辑方式。以下是对应的引入第三方so和第三方.cpp文件的路径脚本编写。对应于:CMakeLists.txt

设置CMake插件的版本

# 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. #语法,参考:https://cmake.org/cmake/help/latest/command/add_library.html
add_library(<name> [STATIC | SHARED | MODULE]
[EXCLUDE_FROM_ALL]
source1 [source2 ...])
# 语法######################################### # 示例
add_library(
#编译生成的库的名称,注意最终生成时会在前面加`lib`.
# Specifies the name of the library.
native-lib # Sets the library as a shared library.
# 生成的库的类型,有SHARED,STATIC,MODULE
SHARED # Provides a relative path to your source file(s).
# 要编译的源代码文件
src/main/cpp/native-lib.cpp )

包含要编译的源码头文件

如果有源码文件有头文件,自然需要包含:

# 语法, 参考:https://cmake.org/cmake/help/latest/command/include_directories.html
include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])
# 语法######################################### # Specifies a path to native header files.
# 注意这里指定的是头文件的目录
include_directories(src/main/cpp/include/)

使用NDK中的Api

有时候我们发现需要使用到NDK中定义的Api,因为NDK中的API都已经是编译好的,而且CMake会自动去NDK目录中查找这些我们需要使用的库,所以这里只需要提供库的名称就可以了,一下举例加载NDK中的log库,因为我们在NDK开发中,总是少不了要打印日志的。 
- 首先查找要使用的库,使用变量保存它的路径。

# 语法,参考:https://cmake.org/cmake/help/latest/command/find_library.html
# A short-hand signature is:
# name1 保存库路径的变量
find_library (<VAR> name1 [path1 path2 ...])
# The general signature is: find_library (
<VAR>
name | NAMES name1 [name2 ...] [NAMES_PER_DIR]
[HINTS path1 [path2 ... ENV var]]
[PATHS path1 [path2 ... ENV var]]
[PATH_SUFFIXES suffix1 [suffix2 ...]]
[DOC "cache documentation string"]
[NO_DEFAULT_PATH]
[NO_CMAKE_ENVIRONMENT_PATH]
[NO_CMAKE_PATH]
[NO_SYSTEM_ENVIRONMENT_PATH]
[NO_CMAKE_SYSTEM_PATH]
[CMAKE_FIND_ROOT_PATH_BOTH |
ONLY_CMAKE_FIND_ROOT_PATH |
NO_CMAKE_FIND_ROOT_PATH]
)
# 语法######################################### # 示例
# 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 ) 为了编译时可以使用,需要将上面一步找到的库链接到我们要编译的库中。
# 语法,参考: https://cmake.org/cmake/help/latest/command/target_link_libraries.html
target_link_libraries(<target> ... <item>... ...)
# 语法 end ####################### # 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.
native-lib # Links the log library to the target library.
# 此处使用上面保存的变量
${log-lib} )

NDK中也有一些库是以源代码的方式存在的,这个时候如果我们要使用它,就需要把它编译到一个本地静态库中,再链接到我们要生成的库中。

# 这样就会在\app\.externalNativeBuild\cmake\debug\${ANDROID_ABI}\ 目录下生成libapp-glue.a/静态库
add_library( app-glue
# 以静态的方式添加
STATIC
# 该源码用于管理`NativeActivity`生命周期事件和触摸事件
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c ) # You need to link static libraries against your shared native library.
# 链接到库中
target_link_libraries( native-lib app-glue ${log-lib} )

添加第三方的库

使用add_library添加第三库

# 语法,因为已经编译好了,所以使用`IMPORTED`关键字
add_library(<name> <SHARED|STATIC|MODULE|UNKNOWN> IMPORTED [GLOBAL]) # 示例
add_library( imported-lib
SHARED
IMPORTED )

当第三方库有针对不同架构编译了不同的库版本时,有时候我们只需要引入我们想要的版本的库,当我们想要引入多个版本的库时,可以使用ANDROID_ABI变量,它表示默认的ABI架构和NDK支持的架构,如果我们在build.gradle中设置了过滤值,则表示过滤后的架构集合。

set_target_properties( # Specifies the target library.
imported-lib # Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION # Provides the path to the library you want to import.
# 注意下面的imported-lib是在项目的app目录下的
imported-lib/src/${ANDROID_ABI}/libimported-lib.so )

同时为了在编译时的使用,我们需要导入所依赖的库的头文件

include_directories( imported-lib/include/ )

添加链接,以便在编译的时候使用,注意如果要添加的第三方库在编译我们要产出的库时不需要直接用到(比如一个库是imported-lib所依赖的),则不需要执行以下步骤。

target_link_libraries( native-lib imported-lib app-glue ${log-lib} )

build.gradle中配置编译库的脚本

android {
...... defaultConfig {
......
//在默认配置中配置 cmake 的一些参数
externalNativeBuild {
cmake {
arguments "-DANDROID_TOOLCHAIN=clang"
cFlags ""
cppFlags ""
}
} //ndk 配置
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a',
'arm64-v8a'
}
} //配置库编译的脚本
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
} //定义不同的产品类型(多渠道打包),通过复写externalNativeBuild,使用target字段实现打包不同的so库。
productFlavors {
...
demo {
...
externalNativeBuild {
cmake {
...
// Specifies which native libraries to build and package for this
// product flavor. If you don't configure this property, Gradle
// builds and packages all shared object libraries that you define
// in your CMake or ndk-build project.
targets "native-lib-demo"
}
}
} paid {
...
externalNativeBuild {
cmake {
...
targets "native-lib-paid"
}
}
}
} }

Android Studio 2.2以上支持了Cmake的配置JNI的相关参数的更多相关文章

  1. Android Studio的安装及第一次启动时的配置

    Android Studio的安装及第一次启动时的配置 一.下载Android Studio 百度搜索“Android Studio" 点击中文社区进入,选择最新版本下载. 下载后双击安装包 ...

  2. Android Studio Gradle 添加.so 支持文件

    近期发展Android Wear 关注商品.官员Demo所有gradle 工程. 当然,我也用eclipse配置一个可行的环境. 问题来了,eclipse,android studio 开发 andr ...

  3. 在Android Studio上进行OpenCV 3.1开发环境配置

    开发环境: Windows 7 x 64 家庭版 Android Studio 1.5.1(Gradle版本2.8) JDK 1.8.0 Android 6.0(API 23) OpenCV 3.1. ...

  4. 如何在Android Studio中添加RecyclerView-v7支持包

    1.打开SDK Manager,在Extras树下找到Android Support Library,下载好支持包.RecyclerView在v7-21版本就出来了.我这里不用更新了,说明是最新的,怎 ...

  5. Android Studio的HAXM不支持虚拟机

    因为我的计算机是服务器,不支持虚拟机,所以报这个错了. 解决方式是直接连接物理手机,打开USB调试,安装驱动,运行项目即可.

  6. Android Studio的project中两个build.gradle配置的区别

    一般创建一个android项目后回出现两个gradle: build.gradle(Project):用来配置整个工程的 build.gradle(app):一个是用来配置app的 对compile和 ...

  7. Android studio 在一个项目上添加另一个项目,引用其内部参数

    Setting.gradle 里面 添加 include ':app',‘imagePicker’ 其中 imagePicker 为要引入的项目名 build.gradle(Module: app)  ...

  8. android studio使用CMake和NDK,实现应用自身被卸载时打开某一网址

    实现应用自身被卸载时打开某一网址的c代码 MyActivity: public class MyActivity extends Activity { /** * Called when the ac ...

  9. android studio NDK配置

    向您的项目添加 C 和 C++ 代码 本文内容 下载 NDK 和构建工具 创建支持 C/C++ 的新项目 构建和运行示例应用 向现有项目添加 C/C++ 代码 创建新的原生源文件 创建 CMake 构 ...

随机推荐

  1. 数据结构 happiness

    问题描述 这一天是小 V 的生日,他收到了朋友们送给他的礼物.现在,小 V 有 n 件礼物,他将这 n 件礼物排成一排,依次编号为 1 到 n,每件礼物都有一个满意值 w[i].现在小 V 要从中选取 ...

  2. MySQL安装与管理

    数据库服务器.数据库和表的关系 –所谓安装数据库服务器,只是在机器上装了一个数据库管理程序,这个管理程序可以管理多个数据库,一般开发人员会针对每一个应用创建一个数据库. –为保存应用中实体的数据,一般 ...

  3. 自动化打包资源混淆集成python实践----打包

    1.自动化打包方案 1)友盟多渠道多渠道打包 2)gradle productFlavors系统的条件编译 3)美团打包 4)APK文件注释写入渠道号 2.各打包方案简介 1)友盟多渠道多渠道打包(w ...

  4. linq 事务处理

    首先引用使名空间: using System.Transactions; 然后写入代码: using (TransactionScope ts = new TransactionScope()) { ...

  5. Django之博客系统:自定义认证

    前面我们在登录的时候,是通过输入用户名和密码来进行认证 user=authenticate(username=cd['username'],password=cd['password']) 这个是通过 ...

  6. 算法训练 数字三角形(DP)

    问题描述 (图3.1-1)示出了一个数字三角形. 请编一个程序计算从顶至底的某处的一条路 径,使该路径所经过的数字的总和最大. ●每一步可沿左斜线向下或右斜线向下走: ●1<三角形行数≤100: ...

  7. 读《JavaScript权威指南》笔记(五)

    1.getComputedStyle()方法的返回值是一个CSSStyleDeclaration对象,它代表了应用在指定元素(或伪对象)上的所有样式. 2.clip style="clip: ...

  8. Vue2.0学习--Vue数据通信详解

    一.前言 组件是 vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用.组件间如何传递数据就显得至关重要.本文尽可能罗列出一些常见的数据传递方式,如p ...

  9. mysql查询不区分大小写

    摘自:http://www.jb51.net/article/70884.htm 当我们输入不管大小写都能查询到数据,例如:输入 aaa 或者aaA ,AAA都能查询同样的结果,说明查询条件对大小写不 ...

  10. ConcurrentHashMap get方法保持同步的解释

    原文链接:https://blog.csdn.net/artaganan8/article/details/80867158 ConcurrentHashMap是Java5中新增加的一个线程安全的Ma ...