Android ndk c code calls java code

Source: Internet
Author: User
Tags call back

Here, I will briefly talk about the method for c calling java in ndk. As for the establishment of the ndk environment, I will not talk about it here. If I have time, I will post it for java to call c. I will post it later, let's talk about c to call back the java code. I won't talk about creating a project first. Let's start with a typical example helloworld. Hahaha, step 1. Create a class in the android project and put a method in it, syso is a helloworld. At the same time, we recommend that you create an activity so that you can put a button and click it to see the effect. Here we will expand a little bit. 1: Write a java method public void helloFromJava () {System. out. println ("helloFromJava");} 2: Write the corresponding native METHOD static {System. loadLibrary ("Hello");} public native void callback1 (); 3: implement the call and put it in the button for calling. 4: here we need to use javah to generate the C header file. Of course we can not do this. In addition, I will not expand the second part to create a C file in ndk. # include <jni. h> # include "com_itheima15_ndk4_DataProvider.h" // This header file is generated by javah in jdk. I believe you don't need to introduce how to use javah. // The Log Module, habitually put a log Module for debugging # include <android/log. h> # define LOG_TAG "System. out "# define LOGD (...) _ android_log_print (ANDROID_LOG_DEBUG, LOG_TAG, _ VA_ARGS _) # define LOGI (...) _ android_log_print (ANDROID_LOG_INFO, LOG_TAG, _ VA_ARGS _) JNIEXPORT void JNICALL encode (JNIEnv * env, jobject obj) {// C call the java empty method/** 1. Find the class in which the method exists (find the class that has the method) * 2. Find this method in this class * 3. Execute this method */char * classname = "com/xuchulong/ndk4/DataProvider "; // This is my class name. Put my helloworld in it according to your own situation. // The first step is to find this class // jclass (* FindClass) (JNIEnv *, const char *); jclass clazz = (* env)-> FindClass (env, classname); // find this method in step 1 of this class // jmethodID (* GetMethodID) (JNIEnv *, jclass, const char *, const char *); // jclass find the method name in the class // const char * Find/const char * method signature jdk javap-s get method signature usage: javap-s package name. class Name jmethodID method = (* env)-> GetMethodID (env, clazz, "helloFromJava", "() V"); // execute this method // void (* CallVoidMethod) (JNIEnv *, jobject, jmethodID ,...); (* env)-> CallVoidMethod (env, obj, method);} C code has completed ndk-build.

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.