android studio如何编写jni项目实例

android studio自从采用了cmake的方式来开发jni相关的项目,方便了不少,网上有很多教程。我这里讲下如何创建实例。

1.创建项目

android studio如何编写jni项目实例_第1张图片


android studio如何编写jni项目实例_第2张图片

记得勾上support C++和 Exceptions support 和 runtime Type infomation support。此时会默认生成一个native的方法。这里讲下修改的地方。

2.创建一个独立的native方法的类

通常我们都比较喜欢独立创建一个native方法类,如我这边写一个md5生成的项目。

public class CipherNativeLib {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("cipher_native_lib");
    }

    public static native String cryptMd5By32(String source, boolean isUpper);

    public static native String cryptMd5By16(String source, boolean isUpper);
}

同时,相应的CMakeLists.txt的文件也要做修改,比如,我们生成的so名称叫cipher_native_lib。我这边还把相关的.cpp文件也引入进来。

3.修改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.

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.
             cipher_native_lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/cipher_native_lib.cpp
             src/main/cpp/md5.cpp
             src/main/cpp/my_utils.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.
                       cipher_native_lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
4.cipher_native_lib.cpp类的生成

android studio可以快速生成native相关的类,再native方法上使用自动构建功能。

android studio如何编写jni项目实例_第3张图片
即可在相关的cpp文件上生成对应的函数。

5.cpp文件的修改

#include 
#include "my_utils.h"


extern "C" {

/**
 * 生成32位md5
 * @param env
 * @param type
 * @param source_  需要加密的文字
 * @param isUpper  转换是否需要大写
 * @return
 */
JNIEXPORT jstring JNICALL
Java_com_kv_ciphersample_CipherNativeLib_cryptMd5By32(JNIEnv *env, jclass type, jstring source_, jboolean isUpper) {
    const char *source = env->GetStringUTFChars(source_, 0);

    char target[32];
    cryptMd5(source, target, 32, isUpper == JNI_TRUE ? 0 : 1);

    env->ReleaseStringUTFChars(source_, source);
    return env->NewStringUTF(target);
}

/**
 * 生成16位md5
 * @param env
 * @param type
 * @param source_ 需要加密的文字
 * @param isUpper 转换是否需要大写
 * @return
 */
JNIEXPORT jstring JNICALL
Java_com_kv_ciphersample_CipherNativeLib_cryptMd5By16(JNIEnv *env, jclass type, jstring source_, jboolean isUpper) {
    const char *source = env->GetStringUTFChars(source_, 0);

    char target[16];
    cryptMd5(source, target, 16, isUpper == JNI_TRUE ? 0 : 1);

    env->ReleaseStringUTFChars(source_, source);
    return env->NewStringUTF(target);
}

}
如我这边写的这两个函数和java类的native方法相对应。


相关c和c++的编写,目前我也正在熟悉中,C或者C++下面如何引用外部的函数,如何编写函数,数组如何传递给函数。可以参考我github上面写的一个项目。

CipherSample(github上面的一个android studio jni的实例)




你可能感兴趣的:(jni)