1, add in the Android.mk file
Local_ldlibs: =-l$ (sysroot)/usr/lib-llog
Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_module: =ndkdemo
Local_src_files: =COM_APP_TEST_NATIVEMETHOD.C
Local_ldlibs: =-l$ (sysroot)/usr/lib-llog
Include $ (build_shared_library)
2, introduce the header file in the. c file to be implemented
#include <string.h>
#include <jni.h>
#include <android/log.h>
#include <stdio.h>
3, where the debug information needs to be output
__android_log_write (Android_log_debug, "tt", "Come in jin!!");
Note that the front is two underscores
4, directly below the code
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <jni.h>
#include <android/log.h>
Add the log output tag to the Logcat native-activity
#define Logi (...) (void) __android_log_print (android_log_info, "native-activity", __va_args__))
#define LOGW (...) (void) __android_log_print (Android_log_warn, "native-activity", __va_args__))
/********** Transfer integer *************
*/
Jniexport void Jnicall Java_com_nan_callback_mycallbackactivity_calljniint (jnienv* env, jobject obj, jint i)
{
Find a class in Java
Jclass cls = (*env)->findclass (env, "com/nan/callback/mycallbackactivity");
Find the methods in the class again
Jmethodid mid = (*env)->getmethodid (env, CLS, "Callbackint", "(I) V");
if (mid = = NULL)
{
Logi ("int error");
Return
}
Callback Methods in Java
int k=0;
for (k;k<10;k++) {
(*env)->callvoidmethod (env, obj, Mid, k);
}
}
/******** Transfer String *************
*/
Jniexport void Jnicall java_com_nan_callback_mycallbackactivity_calljnistring (jnienv* env, Jobject obj, jstring s)
{
Find a class in Java
Jclass cls = (*env)->findclass (env, "com/nan/callback/mycallbackactivity");
Find the methods in the class again
Jmethodid mid = (*env)->getmethodid (env, CLS, "Callbackstring", "(ljava/lang/string;) V");
if (mid = = NULL)
{
Logi ("string error");
Return
}
const char *ch;
Gets the string passed by Java.
ch = (*env)->getstringutfchars (env, S, NULL);
Print
Logi ("From Java string:%s", ch);
(*env)->releasestringutfchars (env, S, ch);
Callback Methods in Java
(*env)->callvoidmethod (env, obj, Mid, (*env)->newstringutf (env, "hello haha"));
}
/******** transfer Array (byte[]) *************
*/
Jniexport void Jnicall Java_com_nan_callback_mycallbackactivity_calljnibyte (jnienv* env, jobject obj, JbyteArray b)
{
Find a class in Java
Jclass cls = (*env)->findclass (env, "com/nan/callback/mycallbackactivity");
Find the methods in the class again
Jmethodid mid = (*env)->getmethodid (env, CLS, "Callbackbyte", "([B) V");
if (mid = = NULL)
{
Logi ("byte[] error");
Return
}
Get array length
Jsize length = (*env)->getarraylength (env,b);
Logi ("Length:%d", length);
Get the Received data
int i;
jbyte* p = (*env)->getbytearrayelements (env,b,null);
Print
for (i=0;i<length;i++)
{
Logi ("%d", p[i]);
}
Char c[5];
C[0] = 1;c[1] = 2;c[2] = 3;c[3] = 4;c[4] = 5;
Constructing arrays
Jbytearray Carr = (*env)->newbytearray (env,length);
(*env)->setbytearrayregion (ENV,CARR,0,LENGTH,C);
Callback Methods in Java
(*env)->callvoidmethod (env, obj, Mid, Carr);
}
How to print debug information in the C file in NDK programming