Public classMainactivityextendsappcompatactivity {PrivateJNI JNI; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); JNI=NewJNI (); } Public voidtest1 (View v) {jni.callvoid (); } Public voidtest2 (View v) {jni.callint (); } Public voidtest3 (View v) {jni.callstring (); } Public voidtest4 (View v) {//jni.callshowtoast ();Callshowtoast (); } Public voidShowtoast (String s) {Toast.maketext ( This, S, Toast.length_short). Show (); } Public native voidcallshowtoast ();}
Public classJNI {Static{system.loadlibrary ("Ccalljava"); } //C calling Java Null method Public voidHellofromjava () {LOG.E ("Calljava","Hello from Java"); } //C Calling a method with two int parameters in Java Public intAddintXinty) {returnx+y; } //C Calling a method in Java with parameter string Public voidprintstring (String s) {LOG.E ("Calljava", s); } PublicNativevoidcallvoid (); PublicNativevoidCallint (); PublicNativevoidcallstring (); PublicNativevoidcallshowtoast ();}
The picture above is the title of the following code
/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Com_itheima_ccalljava_jni*/#ifndef _included_com_itheima_ccalljava_jni#define_included_com_itheima_ccalljava_jni#ifdef __cplusplusextern "C" {#endif/** Class:com_itheima_ccalljava_jni * method:callvoid * Signature: () V*/Jniexportvoidjnicall java_com_itheima_ccalljava_jni_callvoid (jnienv*, jobject);/** Class:com_itheima_ccalljava_jni * method:callint * Signature: () V*/Jniexportvoidjnicall java_com_itheima_ccalljava_jni_callint (jnienv*, jobject);/** Class:com_itheima_ccalljava_jni * method:callstring * Signature: () V*/Jniexportvoidjnicall java_com_itheima_ccalljava_jni_callstring (jnienv*, jobject); #ifdef __cplusplus}#endif#endif
ANDROID.MK file: Below
# Copyright (C) thethe Android Open Source project## Licensed under the Apache License, Version2.0(The"License"); ThisFile exceptinchcompliance with the license.# obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0# # unless required by applicable law or agreed toinchwriting, software# distributed under the License isDistributed on an" as is"basis,# without warranties or CONDITIONS of any KIND, either express or implied.# see the License forThe specific language governing permissions and# limitations under the license.# #LOCAL_PATH get the current path Local_path:= $ (Call my-dir) # Clearing the compilation environment variables obtained from the last compilation will save Local_pathinclude $ (clear_vars) #指定编译生成的模块叫什么名字 the generated. So name is added with the Lib prefix and the. So suffix local_module: =ccalljava# Specifies the name of the. C source file to be compiled Local_src_files:=Ccalljava.clocal_ldlibs+= -llog# generate a dynamic link library. So file include $ (build_shared_library)
#include <jni.h>//Created by FullCircle on 2017/2/19.//#include <android/log.h>#defineLog_tag "System.out"#defineLOGE (...) __android_log_print (Android_log_error, Log_tag, __va_args__)Jniexportvoidjnicall java_com_zz_ccalljava_jni_callvoid (jnienv*env, Jobject thiz) { //① found byte-code object Class Jclass (*findclass) (jnienv*, const char*); //The second parameter to find the full class name of the bytecodeJclass clazz = (*env)->findclass (env,"Com/zz/ccalljava/jni"); //② Find the method to invoke in class//Jmethodid (*getmethodid) (jnienv*, Jclass, const char*, const char*); //Getmethodid second parameter Clazz bytecode object The name of the third parameter method the fourth parameter method signature can be obtained by Javap-s//in studio to the Build/intermediates/classes/debug of the module execution javap-s the full path of the class to generate the method signatureJmethodid Methodid = (*env)->getmethodid (Env,clazz,"Hellofromjava","() V"); //③ Gets the object through class (sometimes it can be omitted when the native method and the Java method to be called back can use the passed Jobject call method in the same class)//④ by Object call method Void (*callvoidmethod) (jnienv*, Jobject, Jmethodid, ...);(*env)Callvoidmethod (Env,thiz,methodid);}/** Class:com_zz_ccalljava_jni * method:callint * Signature: () V*/Jniexportvoidjnicall java_com_zz_ccalljava_jni_callint (jnienv*env, Jobject thiz) { //① Find ClassJclass clazz = (*env)->findclass (env,"Com/zz/ccalljava/jni"); //② Find a wayJmethodid Methodid = (*env)->getmethodid (Env,clazz,"Add","(II) I"); //③ Calling Method intresult = (*env)->callintmethod (Env,thiz,methodid,3,4); LOGE ("result=%d", result);}/** Class:com_zz_ccalljava_jni * method:callstring * Signature: () V*/Jniexportvoidjnicall java_com_zz_ccalljava_jni_callstring (jnienv*env, Jobject thiz) {Jclass Clazz= (*env)->findclass (env,"Com/zz/ccalljava/jni"); Jmethodid Methodid= (*env)->getmethodid (Env,clazz,"printstring","(ljava/lang/string;) V"); Jstring Jstr= (*env)->newstringutf (env,"Hello from C"); (*ENV)Callvoidmethod (ENV,THIZ,METHODID,JSTR);} Jniexportvoidjnicall java_com_zz_ccalljava_jni_callshowtoast (jnienv*env, Jobject thiz) {Jclass Clazz= (*env)->findclass (env,"com/zz/ccalljava/mainactivity"); Jmethodid Methodid= (*env)->getmethodid (Env,clazz,"Showtoast","(ljava/lang/string;) V"); //because callshowtoast this native method is in the Jni class, the Java method to invoke is in mainactivity, so the incoming jobject cannot be used//need to create an object Jobject (*allocobject) (jnienv*, jclass);Jobject jobj = (*env)Allocobject (Env,clazz); Jstring Jstr= (*env)->newstringutf (env,"Hello from C"); (*ENV)Callvoidmethod (ENV,JOBJ,METHODID,JSTR);} Jniexportvoidjnicall java_com_zz_ccalljava_mainactivity_callshowtoast (jnienv*env, Jobject thiz) {Jclass Clazz= (*env)->findclass (env,"com/zz/ccalljava/mainactivity"); Jmethodid Methodid= (*env)->getmethodid (Env,clazz,"Showtoast","(ljava/lang/string;) V"); //because callshowtoast this native method is in the Jni class, the Java method to invoke is in mainactivity, so the incoming jobject cannot be used//need to create an object Jobject (*allocobject) (jnienv*, Jclass); //jobject jobj = (*env)->allocobject (env,clazz);Jstring jstr = (*env)->newstringutf (env,"Hello from C"); (*ENV)Callvoidmethod (ENV,THIZ,METHODID,JSTR);}
C Call Java