Ndk learns 18:jni's c&c++ call Java

Source: Internet
Author: User


I. Java reflection mechanism understand the principle of the Java reflection mechanism first

Example online a lot, reflection is very flexible

two. Tune Java at the JNI layer use:Java layer reverse is relatively easy, increase the reverse threshold, the call is written to the JNI layer, so that the Java layer has no call relationship Note:C and C + + are different, use C + + as an example here
1. First provide a showlog function in the code private void showmsg () { Log. D ("_bing_", "showmsg");}

2. Add code to the last add example jniexport jint jnicall java_org_bing_testjni_mainactivity_add (jnienv *env, jobject obj, jint a, jint b){While (true) { //Reflection call Java code jclass cls = env-findclass("Org/bing/testjni/mainactivi Ty "); If (cls = = NULL) {Break ;        } jmethodid showmsg = env-getmethodid(CLS, "ShowMsg", "() V") ); if (showmsg = = NULL) {Break ;        } //Call construct initialization //Jmethodid init = Env->getmethodid (CLS, "<init>", "() V");//Jobject obj1 = Env->newobject (CLS, init); env-callvoidmethod(obj, showmsg);Break ;    } return a + b;}
3. How to get the function signature information aboveEnter the command in the Project Bin/classes directory: javap-p-S org.bing.testjni.MainActivity
Javap-p-S package name. Class Name
It is also possible to make scripts for generating headers and queries easy to use
3. Output Results






From for notes (Wiz)

Ndk learns 18:jni's c&c++ call Java

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.