美文网首页
java 调用C/C++

java 调用C/C++

作者: lixinxin | 来源:发表于2017-12-01 17:26 被阅读13次

    1.CMakeList.txt 配置

    
    add_library( # Sets the name of the library.
                 native-lib
    
                 # Sets the library as a shared library.
                 SHARED
    
                 # Provides a relative path to your source file(s).
                 src/main/cpp/native-lib.cpp
                 )
    
    add_library( # Sets the name of the library.
                 Hello
    
                 # Sets the library as a shared library.
                 SHARED
    
                 # Provides a relative path to your source file(s).
                 src/main/cpp/Hello.c
                 )
    
    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 )
    
    
    target_link_libraries( # Specifies the target library.
                           native-lib
    
                           # Links the target library to the log library
                           # included in the NDK.
                           ${log-lib} )
    
    target_link_libraries( # Specifies the target library.
                           Hello
    
                           # Links the target library to the log library
                           # included in the NDK.
                           ${log-lib} )
    
    

    2.gradle 添加依赖

    android {
      defaultConfig{
            externalNativeBuild {
                cmake {
                    cppFlags "-frtti -fexceptions"
                }
                 ndk {
                    abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a',"x86" //添加cpu平台
                }
    
            }
        }
      
        externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
            }
        }
    }
    
    

    3.新建一个类 JNI.java 添加本地方法

    public class JNI {
    
        static {
            System.loadLibrary("Hello");
        }
    
        public native String getNameFromJni();
    
        public native int add(int x, int y);
    
        public native String connect(String str);
    
        public native String md5FromJni(String str);
    
    }
    

    4.在cpp 下创建c/c++文件 Hello.c

    #include <jni.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "MD5Utils.h"
    #include "MD5Utils.c"
    #include <android/log.h>
    
    
    #define  LOG_TAG    "Hello"
    #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
    #define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
    #define LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
    
    JNIEXPORT jstring JNICALL
    Java_com_lixinxin_jnidemo1_jni_JNI_getNameFromJni(JNIEnv *env, jobject instance) {
        char *returnValue = "Hello";
        return (*env)->NewStringUTF(env, returnValue);
    }
    
    JNIEXPORT jint JNICALL
    Java_com_lixinxin_jnidemo1_jni_JNI_add(JNIEnv *env, jobject instance, jint x, jint y) {
        int value = x + y;
        return value;
    }
    
    JNIEXPORT jstring JNICALL
    Java_com_lixinxin_jnidemo1_jni_JNI_connect(JNIEnv *env, jobject instance, jstring str_) {
        const char *str = (*env)->GetStringUTFChars(env, str_, 0);
        char *value = strcat(str, " jni string");
        return (*env)->NewStringUTF(env, value);
    }
    
    JNIEXPORT jstring JNICALL
    Java_com_lixinxin_jnidemo1_jni_JNI_md5FromJni(JNIEnv *env, jobject instance, jstring str_) {
        const char *str = (*env)->GetStringUTFChars(env, str_, 0);
        MD5_CTX md5;
        MD5Init(&md5);
    
        unsigned char decrypt[16];
        MD5Update(&md5, str, strlen(str));
        MD5Final(&md5, decrypt);
        unsigned char hexstr[32];
        for (int i = 0; i < 16; i++) {
            sprintf(&hexstr[i * 2], "%02x", decrypt[i]);
        }
        LOGE("name=%s", (char *) hexstr);
        return (*env)->NewStringUTF(env, (char *) hexstr);
    }
    

    5.调用

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView tv = (TextView) findViewById(R.id.sample_text);
            JNI jni = new JNI();
            tv.setText(jni.md5FromJni("111111"));
        }
    

    相关文章

      网友评论

          本文标题:java 调用C/C++

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