详解Android Studio+Gradle3.0编写JNI

今天学习下如何编写第一个JNI代码,实现JNI需要先下载NDK,由于现在Android Studio使用CMake编译jni的C/C++文件,所以还需要下载CMake和LLDB,如下图通过Android Studio来下载:
这里写图片描述

1.新建一个工程NDKSimple,如下图:

这里写图片描述
(图中cpp文件的生成会在下面讲到)

2.检查下NDK环境配置好了没有。步骤如下:

检查SDK Location里面的NDK路径,右键点击app->open module Settings,查看SDK location的NDK路径:
这里写图片描述
检查local.properties文件里面有没有NDK路径:
这里写图片描述

3.编写JNI接口

如下图编写一个简单的获取字符串的JNI接口
这里写图片描述
这里我已经编译好了,所以getResult()不会变红,这里通过System.loadLibrary(“liuxin”);来加载库,这里库的名字需要跟你的build.gradle ndk节点下面的名字一样,这个后面还会讲到。

4.编写C/C++程序

然后build编译工程,检查NdkInterface.java编译后有没有生成class文件,文件位置如下图:
这里写图片描述
然后使用javah 生成头文件,打开Terminal,输入命令进入到debug目录下,命令如下:
CD C:\Users\Administrator\Desktop\NDKSimple\app\build\intermediates\classes\debug
然后使用javah+包名+文件路径来生成头文件,命令如下:
javah com.liu.xin.ndksimple.ndk.NdkInterface
这是我们会发现在debug目录下生成了一个.h文件,如下图:
这里写图片描述
有了头文件的声明还不行,我们需要定义实现这个方法
我们在main下新建一个jni文件夹,如图
这里写图片描述
然后把刚生成的头文件复制到这个目录并且编写.c/.cpp程序

//NDKInterface.c文件
#include "com_liuxin_ndksimple_ndk_NdkInterface.h"
JNIEXPORT jstring JNICALL Java_com_liuxin_ndksimple_ndk_NdkInterface_getResult
  (JNIEnv * jniEnv, jobject obj){

   return (*jniEnv)->NewStringUTF(jniEnv,"liuxin");
  }
5.使用CMake编译生成.so文件

在app的builde.grade文件中添加如下代码:在defaultConfig中

 ndk {    
           //这里的名称要和上面加载库中库名称一样
           moduleName "liuxin"
        }
       // 使用Cmake工具
        externalNativeBuild {
            cmake {
                cppFlags ""
                //生成多个版本的so文件
                abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64'
            }
        }

在android{}中配置CMakeLists.txt路径

externalNativeBuild {
      cmake {
          path "CMakeLists.txt"  // 设置所要编写的c源码位置,以及编译后so文件的名字
      }
  }

在在app的builde.grade文件新建CMakeLists.txt文件,添加如下配置信息:

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

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 it for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.AndroidStudio开始支持Cmake了,ndk感觉挺费劲的,这个是不是好玩点,,这里是要生成的库的文件名 libtest.so
             #这里是liuxin
             liuxin
             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             # Associated headers in the same location as their source
             # file are automatically included.对应的C文件的目录位置
             src/main/jni/NdkInterface.c)

# 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 )

# 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.指定依赖库
                      #这里是liuxin
                       liuxin

                       # Links the target library to the log library
                       # included in the NDK.关联日志记录库文件,在ndk目录中
                       ${log-lib} )

如上文件中,主要注意add_library中配置你要生成so文件的名称,以及C/C++文件的路径;而target_link_libraries中指定依赖库的文件名

调用JNI,编译程序

如下图在MainActivity中调用JNI:
这里写图片描述
最后rebuild 工程,运行程序,如图:
这里写图片描述

到此就结束了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值