在eclipse上新建jni工程可以参考:http://www.cnblogs.com/ashitaka/p/5953708.html

要在java层打印c的log必须引入这个头文件的宏定义:

#ifndef __LOG
#define __LOG
#ifdef __cplusplus
extern "C" {
#endif
#include <android/log.h>
// 宏定义类似java 层的定义,不同级别的Log LOGI, LOGD, LOGW, LOGE, LOGF。 对就Java中的 Log.i log.d
#define LOG_TAG "HelloJni" // 这个是自定义的LOG的标识
//#undef LOG // 取消默认的LOG
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG, __VA_ARGS__)
#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,LOG_TAG, __VA_ARGS__) #ifdef __cplusplus
}
#endif
#endif

分析一下:这里调用了系统的log文件 #include <android/log.h>

#ifndef _ANDROID_LOG_H
#define _ANDROID_LOG_H #include <stdarg.h> #ifdef __cplusplus
extern "C" {
#endif /*
* Android log priority values, in ascending priority order.
*/
typedef enum android_LogPriority {
ANDROID_LOG_UNKNOWN = ,
ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */
ANDROID_LOG_VERBOSE,
ANDROID_LOG_DEBUG,
ANDROID_LOG_INFO,
ANDROID_LOG_WARN,
ANDROID_LOG_ERROR,
ANDROID_LOG_FATAL,
ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */
} android_LogPriority; /*
* Send a simple string to the log.
*/
int __android_log_write(int prio, const char *tag, const char *text); /*
* Send a formatted string to the log, used like printf(fmt,...)
*/
int __android_log_print(int prio, const char *tag, const char *fmt, ...)
#if defined(__GNUC__)
#ifdef __USE_MINGW_ANSI_STDIO
#if __USE_MINGW_ANSI_STDIO
__attribute__ ((format(gnu_printf, , )))
#else
__attribute__ ((format(printf, , )))
#endif
#else
__attribute__ ((format(printf, , )))
#endif
#endif
; /*
* A variant of __android_log_print() that takes a va_list to list
* additional parameters.
*/
int __android_log_vprint(int prio, const char *tag,
const char *fmt, va_list ap); /*
* Log an assertion failure and abort the process to have a chance
* to inspect it if a debugger is attached. This uses the FATAL priority.
*/
void __android_log_assert(const char *cond, const char *tag,
const char *fmt, ...)
#if defined(__GNUC__)
__attribute__ ((noreturn))
#ifdef __USE_MINGW_ANSI_STDIO
#if __USE_MINGW_ANSI_STDIO
__attribute__ ((format(gnu_printf, , )))
#else
__attribute__ ((format(printf, , )))
#endif
#else
__attribute__ ((format(printf, , )))
#endif
#endif
; #ifdef __cplusplus
}
#endif #endif /* _ANDROID_LOG_H */

这里面定义了log的优先级,并且log最终调用的都是__android_log_print(...)函数:

int __android_log_print(int prio, const char *tag, const char *fmt, ...)
{
va_list ap;
char buf[LOG_BUF_SIZE]; va_start(ap, fmt);
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
va_end(ap); return __android_log_write(prio, tag, buf);
}

所以自己定义一个头文件,并且定义宏指令指向__android_log_print(...)。就可以调用了。

另外在 system/core/include/cutils/log.h 也有定义,但是有些没有,而且在#include <cutils/log.h>提示找不到。估计是版本变更了。但是原理是一样的。

下面是测试函数:

JNIEXPORT jint JNICALL Java_com_example_hellojni_MainActivity_test
(JNIEnv *env, jclass clazz){
LOGD("log.d Java_Log_test()");
LOGI("Log.i Java_Log_test()");
return 0;
}

安卓测试程序:

public class MainActivity extends Activity {

    static{
System.loadLibrary("HelloJava");
}
private Button btn_getString;
private TextView tv_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
setEvent();
} private void setEvent() {
// TODO Auto-generated method stub
btn_getString.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv_content.setText(getString());
test();
}
});
} private void initUI() {
// TODO Auto-generated method stub
btn_getString = (Button) findViewById(R.id.btn_getString);
tv_content = (TextView) findViewById(R.id.tv_content);
} public static native String getString();
public static native int test();
}

测试结果:

参考博客:http://blog.csdn.net/thl789/article/details/6638494

     http://blog.csdn.net/luoshengyang/article/details/6581828

java打印Jni层log的更多相关文章

  1. Java层与Jni层的数组传递(转)

    源:Java层与Jni层的数组传递 Android开发中,经常会在Java代码与Jni层之间传递数组(byte[]),一个典型的应用是Java层把需要发送给客户端的数据流传递到Jni层,由Jni层的S ...

  2. Android开发实践:Java层与Jni层的数组传递

    转载:http://www.linuxidc.com/Linux/2014-03/97561.htm Android开发中,经常会在Java代码与Jni层之间传递数组(byte[]),一个典型的应用是 ...

  3. OpenCV向JNI层的参数转换

    九层之台,起于累土:千里之堤毁于蚁穴:成者半于九十.最近工程项目完全可以调试,却最后在 OpenCV向JNI层的参数转换 这个节点上遇到麻烦,看来得好好的思考一番,仔细寻找其中的纰漏. 一.实例 根据 ...

  4. Jni层回调java代码【转】

    本文转载自:http://www.linuxidc.com/Linux/2014-03/97562.htm JNI是Java Native Interface的缩写,是Java平台的重要特性,使得Ja ...

  5. Android中关于JNI 的学习(三)在JNI层訪问Java端对象

    前面两篇文章简介了JNI层跟Java层的一些相应关系,包含方法名,数据类型和方法名称等,相信在理论层面.可以非常好地帮助我们去了解JNI在Native本地开发中的作用,对JNI的一些概念也有了一个初步 ...

  6. 基于Eclipse的Android JNI层測试应用开发过程记录

    前言 本文记录一个Java层与JNI层參数与数据交互的应用程序开发过程.为实现一个功能完整的带Java与JNI的应用程序打下基础. 本文如果读者已搭建好Android的Eclipse与NDK开发环境, ...

  7. Android 从上层到底层-----jni层

    CPU:RK3288 系统:Android 5.1 功能:上层 app 控制 led 亮灭 开发板:Firefly RK3288 led_jni.h path:hardware/rockchip/fi ...

  8. Android的NDK开发(5)————Android JNI层实现文件的read、write与seek操作

    1. 在Android的Java层实现文件的读写操作是非常简单的,可以参看之前写的博文:http://blog.csdn.net/conowen/article/details/7296121 在JN ...

  9. Java 打印金字塔 or 打印带数字的金字塔 (Java 学习中的小记录)

    Java 打印金字塔 or 打印带数字的金字塔 (Java 学习中的小记录) 作者:王可利(Star·星星) 效果图: 代码如下: class Star8 { public static void m ...

随机推荐

  1. getPhysicalNumberOfCells 与 getLastCellNum的区别

    用org.apache.poi的包做excel导入,无意间发明若是excel文件中有空列,空列后面的数据全部读不到. 查来查去本来是HSSFRow供给两个办法:getPhysicalNumberOfC ...

  2. Centos下设置VNC为3389端口

    1.安装vnc yum install vnc vnc-server yum install vnc vnc-server 2.修改vnc端口,修改/usr/bin/vncserver ,把5900变 ...

  3. java.sql.SQLException: Incorrect key file for table 'C:\Windows\TEMP\#sql578_6e2_68d.MYI'; try to repair it

    java.sql.SQLException: Incorrect key file for table 'C:\Windows\TEMP\#sql578_6e2_68d.MYI'; try to re ...

  4. Rectangle Area

    class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { l ...

  5. 基于bootstrap + php +ajax datatable 插件的使用

    Datatables是一款jquery表格插件.它是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能. 下面是我学习datatables写的一个服务器端(php)分页例子,该功能包含的功能 ...

  6. android屏幕适配原则

    1.尽量使用线性布局和相对布局 2.尽量使用dip和sp,不要使用px 3.为不同的分辨率提供不同的布局文件和图片 4.在AndroidMainfest.xml中设置多分辨率支持 5.层级嵌套,合理布 ...

  7. LeetCode:461. Hamming Distance

    package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...

  8. mybatis foreach的使用

    foreach标签在批量插入数据库时非常方便,但是很容易出错,我没有注意括号的位置让我折腾了一个半小时找问题,醉醉哒,所以在这里记录一下foreach的使用. 首先,这是insert批量插入正确的代码 ...

  9. 关于sass的安装

    关于sass的安装真是费了九牛二虎之力,这么说一点都不夸张,好了我就不多浪费口水了,直接进入正题 1.首先要安装ruby,这个大家可以去度娘上查询,很好安装的,相信大家的智慧与实力都是可以安装成功的 ...

  10. Iptables工作原理使用详解

    Iptables防火墙简介 Iptables名词和术语 Iptables工作流程 基本语法 Filter 参数说明 NAT表: Icmp协议 TCP FLAG 标记 什么是状态检测 iptables的 ...