Gradle3.0 以上创建 .so 文件并调用

之前没有使用过 JNI 或 NDK 开发,最近了解到把项目中的秘钥放到 .so 文件中能起到较好的安全性作用,因此花了个早上入坑

首先配置 CMake 环境

Gradle3.0 以上创建 .so 文件并调用_第1张图片
image

在 module 工程文件的 build.gradle 文件中做以下配置

defaultConfig 的配置

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

android 的配置

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"  // C 文件编辑的信息
        }
    }

关于 CMakeLists.txt 文件,跟 build.gradle 同层级

# 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文件名称.
       nativejni

       # Sets the library as a shared library.
       SHARED
       # 设置这个so文件为共享.

       # Provides a relative path to your source file(s).
       # 设置这个so文件为共享.指向创建的 c 文件
       src/main/jni/nativejni.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.
            # 制定目标库.
            nativejni

            # Links the target library to the log library
            # included in the NDK.
            ${log-lib} )

把上面的代码复制到 CMakeLists.txt 文件中即可,上面的 nativejni.c 文件后面提到,先执行上诉步骤会报错,CMakeLists.txt 文件中的代码执行错误,暂时别管,因为需要结合下面步骤一起,等 .so 文件创建完就没事了

编写 .so 文件

  1. 首先创建一个 NativeUtils 文件
public class NativeUtils {
    static {
        //这是加载 .so 文件的方法,这里的 nativejni 就是 so 文件名
        System.loadLibrary("nativejni");
    }

    public static native String getStringFromNative();
}
  1. 然后 ReBuild 下项目


    Gradle3.0 以上创建 .so 文件并调用_第2张图片
    image
  2. 生成头文件
    Terminal 中调用 javah -d jni -classpath 上一步生成的 build 的 NativeUtils 路径

javah -d jni -classpath /Users/mac/Documents/workspace/JNIDemo/app/build/intermediates/classes/debug com.feng.jnidemo.NativeUtils

jni 包中就会生成 com_feng_jnidemo_NativeUtils.h 头文件

Gradle3.0 以上创建 .so 文件并调用_第3张图片
image

  1. 在 jni 包中创建 nativejni.c c 文件
//引用头文件
#include "com_feng_jnidemo_NativeUtils.h"

//方法名:Java+头文件名+方法名
JNIEXPORT jstring JNICALL Java_com_feng_jnidemo_NativeUtils_getStringFromNative
            (JNIEnv *env, jobject obj) {
        return (*env)->NewStringUTF(env, "this is secret key");
}

最后 Rebuild 一下发现生成 .so 文件


Gradle3.0 以上创建 .so 文件并调用_第4张图片
image

调用方法

String str=NativeUtils.getStringFromNative();
Log.e(TAG, "onCreate: str="+str );

你可能感兴趣的:(Gradle3.0 以上创建 .so 文件并调用)