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 


二:配置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文件的名字 
  } 


三:配置CMakeLists.txt


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


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

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


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


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


七:创建实现头文件的.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 .”的返回:


八:加载SO库

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


九:生成SO文件

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


十:执行本地调用


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


参考博客: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} )


          

     

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值