Basic programming knowledge-Java JNI Development Process (2)

Source: Internet
Author: User
Tags md5 digest

Use jni in android to call the local C ++ Library

The local library file suffix. so on the android platform. Similar to dll files on windows.


To use jni on android, you must first download android ndk.


Follow these steps to create an android project, right-click the android project, select properties, and choose Android Tools> Add Native Support. You can add local library support for the android project.

After the supported android project is added, the jni directory will be added, and the C ++ code will be written in this directory.


Create a new class and use native to modify a function. This function is implemented by the local database. In this example, the local library function is used to convert strings with built-in algorithms. The server can identify the legitimacy of the client based on this conversion. The purpose of using jni is to increase the complexity of code decompilation and cracking.


package com.show.shownative.lib;public class ShowNativeLib {public native String showConvertCode(String param);}

Use javah to generate the header file: ShowNativeLib. h

/* DO NOT EDIT THIS FILE - it is machine generated */#include 
 
  /* Header for class com_show_shownative_lib_ShowNativeLib */#ifndef _Included_com_show_shownative_lib_ShowNativeLib#define _Included_com_show_shownative_lib_ShowNativeLib#ifdef __cplusplusextern "C" {#endif/* * Class:     com_show_shownative_lib_ShowNativeLib * Method:    showConvertCode * Signature: (Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_show_shownative_lib_ShowNativeLib_showConvertCode  (JNIEnv *, jobject, jstring);#ifdef __cplusplus}#endif#endif
 

In native Project Settings of android, mk file classes are required for compilation and configuration. In this example, three MK s are required. The system will automatically create one for you and create two new ones based on the path.

1. The content of Application. mk is as follows:

APP_STL := stlport_staticAPP_ABI := armeabi armeabi-v7a mips x86

The configuration of the above two lines: the first is to add C ++ STL template support. The second is to set the number of CPU architectures for compiling so files.


2. The content of Android. mk is as follows:

LOCAL_PATH := $(call my-dir)include $(LOCAL_PATH)/ShowNativeLib.mk

The first line of the above configuration is dead. The second line is a multi-module introduction method. If you have other local library modules, you can divide them into modules. Here, you can include.


3. The content of ShowNativeLib. mk is as follows:

include $(CLEAR_VARS)LOCAL_MODULE    := ShowNativeLibLOCAL_SRC_FILES := ShowMD5.cpp ShowNativeLib.cppinclude $(BUILD_SHARED_LIBRARY)

This file is the configuration of this module tested in our example. ShowNativeLib. cpp is our implementation file. ShowMD5.cpp is a third-party open-source MD5 Digest algorithm library. (Here we have to talk about it. MD5 and SHA1 are called DIGEST algorithms (DIGEST), because many people have not understood it and call it an encryption algorithm .)




ShowNativeLib. cpp is implemented as follows.


# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Define CONVERT_KEY ("sobey_showfun_convert_key_47da-441331441") JNIEXPORT jstring JNICALL partition (JNIEnv * pEnv, jobject obj, jstring param) {if (! Param) return NULL;/** methods of operating java objects in JNI are all based on the java reflection mechanism. * If you have never touched java reflection, you should first check the java reflection information. * The following is only a basic explanation and will not be discussed in depth. * /// Get java. lang. string classjclass clzString = pEnv-> FindClass ("java/lang/String"); // obtain String. jmethodID getMethod = pEnv-> GetMethodID (clzString, "getBytes", "() [B"); // run this Method to obtain the String. the byte array returned by getBytes. Jobject byteObj = pEnv-> CallObjectMethod (param, getMethod); jbyteArray paramBytes = (jbyteArray) byteObj; // convert java byte [] to C ++'s unsigned char *. To carry out subsequent conversion algorithms. Unsigned char * pBufIn = (unsigned char *) pEnv-> GetByteArrayElements (paramBytes, NULL); jsize paramByteLen = pEnv-> GetArrayLength (paramBytes); // call the MD5 library, to process the summary. Std: string paramMd5 = GetMD5Code (pBufIn, (int) paramByteLen); int lengthMd5 = paramMd5.size (); // convert the built-in conversion key for MD5 processing std :: string paramKey = GetMD5Code (unsigned char *) CONVERT_KEY, strlen (CONVERT_KEY); // convert the algorithm. For (int idx = 0; idx
      
        NewStringUTF (resultMD5.c _ str (); return result ;}
      
     
    
   
  
 

The above is an example of using JNI in android.

Jni is still very helpful. After all, android java has a bottleneck in efficiency (an interpreted language Virtual Machine with an additional interpretation layer), and many require efficient data processing, it is inevitable to call the local database for processing.

Compared with android and ios, Alibaba Cloud's objc language and ios platform are technically more efficient than android's java language. Therefore, the smoothness of Apple's running on the same hardware configuration is not comparable to that on android, but the actual situation is that the hardware configuration on the android machine is too high.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.