问题描述
所以,我正在写一个Android应用程序,它使用了大量C ++库。我有一切工作,以使Java应用可以调用C ++代表团方法,但我发现自己希望我可以从C ++消息记录到Android日志。这是从Java容易,但我无所适从,如何调用从C ++ Java方法。我搜索发现方法从C ++,这根本不是我想要做的打开一个JVM。理想情况下,我想传递一个日志方法指针到C ++,它可以被用来每当我想要的。当然,Java不支持方法指针。我的Java方法看起来是这样的:
私人无效日志(String s)将{
Log.i(标签,S); // Android的日志
}
我只是不知道如何让C ++访问此方法。
C ++调用 COUT
和的printf
不会显示出来的在LogCat中输出。有两个解决方案。
-
使用的NDK,让你将消息记录到LogCat中提供的日志记录宏。当你有一个完整的图书馆现有调试语句这有利于新的code和包装code你写的,但不是那么好。我定义的宏如下:
#定义LOG_INFO(信息)__android_log_write(ANDROID_LOG_INFO,JNI,资讯) #定义LOG_ERROR(错误)__android_log_write(ANDROID_LOG_ERROR,JNI,错误)
,然后在源$ C $ C我可以叫
LOG_INFO(图书馆被称为!);
-
捕获标准输出的程序/标准错误和漏斗它变成LogCat中。从的Android调试桥页:
So I'm writing an Android app which uses a large c++ library. I have everything working so that the java app can call the c++ delegation methods, but I'm finding myself wishing I could log messages from c++ to the Android log. This is easy from java, but I'm at a loss as to how to call a java method from c++. My searches found methods for opening a jvm from c++, which is not at all what I want to do. Ideally, I'd like to pass a log method pointer to c++, which could then be used whenever I wanted. Of course, java doesn't support method pointers. My java method would look something like:
private void log(String s){
Log.i(Tag, s); // Android log
}
I just don't know how to allow c++ to access this method.
C++ calls to cout
and printf
will not show up in the LogCat output. There are two solutions.
Use the Logging macros provided by the NDK that allow you to log messages to LogCat. This is good for new code and wrapper code you are writing, but not so good when you have a library full of existing debugging statements. I define macros as followed:
#define LOG_INFO(info) __android_log_write(ANDROID_LOG_INFO,"JNI",info) #define LOG_ERROR(error) __android_log_write(ANDROID_LOG_ERROR,"JNI",error)
and then within the sourcecode I can call
LOG_INFO("Library is called!");
Capture the standard out/ standard error of the program and funnel it into LogCat. From the Android Debug Bridge page:
这篇关于如何调用从C ++中的JNI Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!