Read an article on:
http://blog.csdn.net/github_33304260/article/details/62891083
We should already be able to build the NDK project,
Let's take a look at Java call C code: Java invoke C code to perform addition operations
Java code:
/**
* through JNI simple plastic addition Operation
* @param a
* @param b
* @return/public
static native int addint (int A, T b);
. h Code:
* * Class: com_libin_factory_ndk_ndk
* method: addint
* Signature: (II) I
* * Jniexport jint jnicall java_com_libin_factory_ndk_ndk_addint
(jnienv *, Jclass, Jint, Jint);
C + + code:
Jniexport jint jnicall java_com_libin_factory_ndk_ndk_addint
(jnienv *env, Jclass type, jint A, Jint b) {
retur n A + b;
}
Call:
Toast.maketext (Jniactivity.this, "Add results:" + ndk.addint (2), Toast.length_short). Show ();
Show:
Description
Java_ Full Class Name _ Method name
Java_com_libin_factory_ndk_ndk_addint Java invoke C code to perform string concatenation operations
Java code:
/**
* Simple string concatenation via JNI
* @param s
* @return/public
static native string addstring (string s);
. h Code:
* * Class: com_libin_factory_ndk_ndk
* method: addstring
* Signature: (ljava/lang/string ;) ljava/lang/string;
* *
jniexport jstring jnicall java_com_libin_factory_ndk_ndk_addstring
(jnienv *, Jclass, jstring);
C + + code:
/**
* Simple string concatenation via JNI
* @param s
* @return/
jniexport jstring jnicall java_com_libin_factory_ Ndk_ndk_addstring
(jnienv *env, Jclass type, jstring s_) {
char *text = Jstringtochar (env, s_);
Char temp[20] = " / I am from C";
strcat (text, temp);
Return chartostring (env, text);
}
The following two are tool classes
/** * String converted to char * @param env * @param jstr * @return */char* Jstringtochar (jnienv* env, jstring jstr) {Char
* Rtn = NULL;
Jclass clsstring = Env->findclass ("java/lang/string");
Jstring Strencode = Env->newstringutf ("Utf-8");
Jmethodid mid = Env->getmethodid (clsstring, "GetBytes", "(ljava/lang/string;) [B");
Jbytearray barr= (Jbytearray) Env->callobjectmethod (Jstr, Mid, Strencode);
Jsize alen = Env->getarraylength (Barr);
jbyte* ba = env->getbytearrayelements (Barr, Jni_false);
if (Alen > 0) {rtn = (char*) malloc (alen + 1);
memcpy (RTN, BA, Alen);
Rtn[alen] = 0;
} env->releasebytearrayelements (Barr, BA, 0);
return RTN;
/** * Char is converted to String * @param env * @param chr * @return */jstring chartostring (jnienv *env, const char *CHR) {
Logi ("chartostring:%s\n", CHR);
Jclass strclass = Env->findclass ("java/lang/string"); Jmethodid strconstruct = Env->getmethodid (strClass, "<init>", "([bljava/lang/string;) V");
Jbytearray bytes = Env->newbytearray (strlen (CHR));
Env->setbytearrayregion (bytes, 0, strlen (CHR), (jbyte*) CHR);
jstring encoding = ENV->NEWSTRINGUTF ("Utf-8");
Return (jstring) env->newobject (strclass, strconstruct, Bytes, encoding); }
Call:
Toast.maketext (Jniactivity.this, "JNI results:" + ndk.addstring ("I am from Java"), Toast.length_short). Show ();
Show:
Java calls C code executes each element plus ten
Java code:
/**
* Let C code give you each element plus a
* @param intarray
* @return
/public static native int[] Increasearrayeles ( Int[] intarray);
. h Code:
* * Class: com_libin_factory_ndk_ndk
* method: increasearrayeles
* Signature: ([i) [I
*/
Jniexport Jintarray jnicall java_com_libin_factory_ndk_ndk_increasearrayeles
(jnienv *, Jclass, JintArray);
C + + code:
/**
* Let C code give you each element plus
* @param intarray
* @return * * *
jniexport jintarray jnicall
java_com_ Libin_factory_ndk_ndk_increasearrayeles (jnienv *env, Jclass type, Jintarray intarray_) {
//Get the length of the array
jsize size = Env->getarraylength (Intarray_);
Gets the array element
jint *array = env->getintarrayelements (Intarray_, jni_false);//jni_false the same part does not open up new space
/ Iterate over the array for each element by adding a for
(int i = 0; i < size; i++) {
* (array + i) + +;
LOGE ("Array%d =%d", I, * (array + i));
}
Env->releaseintarrayelements (Intarray_, array, 0);
Returns the result return
Intarray_;
}
Call:
int a[] = {1,2,3,4,5,6,7};
int[] INTs = Ndk.increasearrayeles (a);
for (int i = 0; i< ints.length;i++) {
logger.e (a[i]+ "");
}
Show:
Java calls C code execution detect password is correct
Java code:
/**
* Application: Detect password is correct, correct return 200, error return
* @param pwd
* @return
/public
static native int Checkpwd ( String pwd);
. h Code:
* * Class: com_libin_factory_ndk_ndk
* method: checkpwd
* Signature: (ljava/lang/string;) I
*/
Jniexport jint jnicall java_com_libin_factory_ndk_ndk_checkpwd
(jnienv *, Jclass, jstring);
C + + code:
/**
* Application: Detection password is correct, correct return 200, error return to
* @param pwd * @return * * * *
jniexport jint jnicall
Java_com_libin_factory_ndk_ndk_checkpwd (jnienv *env, Jclass type, jstring pwd_) {
//Assume server password 123456
char * Otigin = "123456";
Char *fromuser = Jstringtochar (env, pwd_);
Function comparison string is
the same int code = strcmp (Otigin, fromuser);
if (code = = 0) {return
;
}
Call:
Toast.maketext (jniactivity.this, "password checksum" + ndk.checkpwd ("123456"), Toast.length_short). Show ();
Show:
Below we can look at the C call Java:
http://blog.csdn.net/github_33304260/article/details/71213921