Android -- JNI开发(静态注册)

3 篇文章 0 订阅
1 篇文章 0 订阅

近来研究了一下Android Studio JNI开发,写过几个JAVA调用C层的例子,网上百度了很多,发现好多例子现在都不适用,因此在这儿做一个简单的总结,分享给大家。

特别说明:由于AS 和 Gradle更新过快,导致网上以前一些JNI开发的教程不适用。在这儿,此教程采用的是AS3.1,Gradle4.4。理论上,Gradle3.0以上的版本采用此教程即可。

下面的步骤仅供参考,详细代码请见:链接:https://pan.baidu.com/s/1fABPPC-uAf0fFAJrsmBkcA 密码:1l8b
可以参考着代码来看这篇文章。
AS正确的配置/安装了sdk,并且安装了CMake、LLDB、NDK,如下图所示。
这里写图片描述

新建一个工程,MainActivty.java写好相应的TextView显示代码。如下图:
这里写图片描述

在MainActivty.java同级目录下建立JNIUtils.java的文件,此文件为JNI接口,并填充相应方法,JNI接口需要用native关键字修饰,,如下图:
这里写图片描述
注意 “System.loadLibrary(“hellojni”);”名字注意,需要和你的build.gradle ndk节点下的名字一样。

这个时候再Build -> Rebuild Project一下,生成相应的class文件。不报错继续往下走,报错请自行查找原因。

用AS自带的终端进入你的工程java文件夹下,我这里进入的是app\src\main\java,然后使用javah+包名+文件路径来生成头文件。我这儿运行的命令是:javah com.jnidemo.JNIUtils
这里写图片描述

操作正确的话会在这个同级目录下生成一个.h文件,我这里生成的是com_jnidemo_JNIUtils.h 如下图:
这里写图片描述

然后再main目录下创建jni文件夹,并将刚才生成的文件拷贝进去,并且新建一个cpp文件,将.h里面的代码拷贝近cpp文件里,并稍加修改,如下图:
这里写图片描述

在build.gradle的defaultConfig节点下加入:

        // 使用Cmake工具
        externalNativeBuild {
            cmake {
                cppFlags ""
                //生成多个版本的so文件
                abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64'
            }
        }

在build.gradle的android节点下加入:

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

在app目录下添加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文件名称.  
           hellojni
     
           # Sets the library as a shared library.  
           SHARED  
           # 设置这个so文件为共享.  
     
           # Provides a relative path to your source file(s).  
           # 设置JNI源文件路径.
           src/main/jni/main.cpp)
     
    # 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.  
                # 制定目标库.  
                hellojni
     
                # Links the target library to the log library  
                # included in the NDK.  
                ${log-lib} )  

最后我们去完善MainActivity.java文件,如下图:
这里写图片描述

最后连上你的手机,运行一下:
这里写图片描述

至此,实现了JAVA到C层的调用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值