美文网首页
如何在Android项目中注入JNI

如何在Android项目中注入JNI

作者: 职场过客 | 来源:发表于2023-12-23 15:30 被阅读0次

1.首先在Android SDK中选择SDK Tools,然后安装CMake版本
2.在main文件下下新建一个cpp文件夹,然后点击右键选择Add C++ to Module,在新的弹框中直接点击OK!
图一


c++ module.png

图二


ok.png

3.会自动生成文件夹


image.png

解释一下:CMakeLists.txt中的代码

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.22.1)

# Declares and names the project.

project("funwalletjni")

# 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.
        ${CMAKE_PROJECT_NAME}
        # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             funwallet.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.
        ${CMAKE_PROJECT_NAME}
        # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

cmake_minimum_required(VERSION 3.22.1)当前CMake版本
project("funwalletjni") C++的项目名称
声明项目名称。 项目名称可以通过{PROJECT_NAME}访问, 由于这是顶级 CMakeLists.txt,因此项目名称也是可访问的 与{CMAKE_PROJECT_NAME} (两个 CMake 变量在顶层同步构建脚本范围)。

add_library( # Sets the name of the library.
        ${CMAKE_PROJECT_NAME}
        # Sets the library as a shared library.
             SHARED
             # Provides a relative path to your source file(s).
             funwallet.cpp )

列出 C/C++ 源文件以及此 CMakeLists.txt 的相对路径。
创建并命名一个库,将其设置为 STATIC 或 SHARED,并提供其源代码的相对路径。您可以定义多个库,CMake 会为您构建它们。 Gradle 会自动将共享库与您的 APK 打包。

funwallet.cpp解析
#include <jni.h>
#include <string>
extern "C"
JNIEXPORT jstring JNICALL
Java_com_base_funwallet_MainActivity_startEncrypt(JNIEnv *env, jobject thiz, jstring content,jstring key) {
//    JNIEnv * env:这个env可以看做是Jni接口本身的一个对象,jni.h头文件中存在着大量被封装好的函数,这些函数也是Jni编程中经常被使用到的,
//    要想调用这些函数就需要使用JNIEnv这个对象。例如:env->GetObjectClass()
//    std::string result;
//    jstring key = env->NewStringUTF(result.c_str());

    // 获取 AES256Encryption 类
    jclass clazz  = env->FindClass("com/base/funwallet/util/AES256Encryption");
    if (clazz == nullptr) return nullptr;  // 类没有找到

    // 获取 encrypt 方法的 ID
    //(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; 意思是传入两个String,之后返回一个String
    //Ljava/lang/String,意思是 L 表示这是一个对象类型(Reference Type)。
    //java/lang/String 表示类的路径,即 java.lang.String 类的路径。
    jmethodID mid = env->GetStaticMethodID(clazz, "encrypt", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
    if (mid == nullptr) return nullptr;  // 方法没有找到
    // 调用 encrypt 方法
    jstring encrypted = (jstring) env->CallStaticObjectMethod(clazz, mid, content, key);
    return encrypted;
}

在Android 代码中使用
这个是c++代码

   external fun startEncrypt(content: String, key: String): String
    
    companion object {
        init {
            //相当于注入so库,对应的是 project("funwalletjni")
            System.loadLibrary("funwalletjni")
        }
    }

这是是Android调用

  var key = "43090ab7031d425aa7f09f94b4668608"
  var oldStr = "0x9e63708e220a842a7bf8ee558df138fa19bca1a2+" + 1695021542
 var gk =  startEncrypt("$oldStr", key)

相关文章

网友评论

      本文标题:如何在Android项目中注入JNI

      本文链接:https://www.haomeiwen.com/subject/oczqndtx.html