Gradle3.0+ NDK开发入门

对于Ndk开发个人的简单理解:通过约定(Jni),使两种不同编程语言(java和c/c++)可以互相调用对应语言封装的方法。由于c/c++编程更加高效安全,把app中核心模块和消耗性能的功能使用c/c++编写,可以变相提高app的安全性和高效性。

Gradle3.0以后,as推荐使用CMake和ndk-build进行ndk开发,下面就一步一步展示如何使用CMake进行Ndk开发。

一:通过SDKManager下载:CMake和LLDB 

Gradle3.0+ NDK开发入门_第1张图片

二:配置CMake

在build.gradle的defaultConfig节点下加入: 
// 使用Cmake工具 
externalNativeBuild { 
cmake { 
cppFlags “” 
//生成多个版本的so文件 
abiFilters ‘arm64-v8a’, ‘armeabi-v7a’ 
  } 

在build.gradle的android节点下加入: 
// 配置CMakeLists.txt路径 
externalNativeBuild { 
cmake { 
path “CMakeLists.txt” //编译后so文件的名字 
  } 

Gradle3.0+ NDK开发入门_第2张图片

三:配置CMakeLists.txt

Gradle3.0+ NDK开发入门_第3张图片

四:创建一个Java2CJNI类,并在类里创建一个java2C的本地方法:

Gradle3.0+ NDK开发入门_第4张图片

五:通过javah命令获取到本地头文件

在项目根目录下,进入main->java目录,摁住shift键右击选择在此处打开命令窗口,进入docs命令,在命令中执行javah com.example.cmakendkdemo.Java2CJni

Gradle3.0+ NDK开发入门_第5张图片

执行完javah命令后,会在当前目录下创建一个.h的头文件

Gradle3.0+ NDK开发入门_第6张图片

六:在Main目录下创建jni文件夹,把.h头文件拖进jni文件

Gradle3.0+ NDK开发入门_第7张图片

七:创建实现头文件的.c源文件

在jni目录下创建一个Java2C.c的源文件,通过#include引入头文件com_example_cmakendkdemo_Java2CJni.h,并把在头文件下的声明方法JNIEXPORT jstring JNICALL Java_com_sanhui_ndkdemo_Java2CJNI_java2C(JNIEnv *, jobject);复制到Java2C.c中,补全方法参数,并实现一个C字符串“I am From Native C .”的返回:

Gradle3.0+ NDK开发入门_第8张图片

八:加载SO库

在我们创建的Java2CJNI类中加载so库,主要是为了在我们调用本地方法之前先编译本地源码。

Gradle3.0+ NDK开发入门_第9张图片

九:生成SO文件

在项目的工具类中选择Build->Rebuild Project,进行重新编译工程,然后AS会为我们生成so文件,so文件所在目录为:CMakeNdkDemo\build\intermediates\cmake\debug\obj

Gradle3.0+ NDK开发入门_第10张图片

十:执行本地调用

Gradle3.0+ NDK开发入门_第11张图片

OK,到这里已经完成了一个DEMO级别的NDK应用开发了,那么来看看我们的执行结果:

Gradle3.0+ NDK开发入门_第12张图片

参考博客:https://www.cnblogs.com/guanmanman/p/6769240.html

           https://blog.csdn.net/xiaozhu0922/article/details/78835144

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.
#CMakeLists.txt
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.
add_library( # Sets the name of the library.
# 设置so文件名称.
Java2C
# Sets the library as a shared library.
SHARED
# 设置这个so文件为共享.
# Provides a relative path to your source file(s).
# 设置这个so文件为共享.
src/main/jni/Java2C.c)
#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.
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.
target_link_libraries( # Specifies the target library.
# 制定目标库.
Java2C
# Links the target library to the log library
# included in the NDK.
${log-lib} )


          

     

你可能感兴趣的:(安卓开发)