如何调用从C ++中的JNI Java方法 [英] How to call Java methods from C++ in JNI

查看:157
本文介绍了如何调用从C ++中的JNI Java方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在写一个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中输出。有两个解决方案。

  1. 使用的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(图书馆被称为!);

  2. 捕获标准输出的程序/标准错误和漏斗它变成LogCat中。从的Andr​​oid调试桥页:

      

    查看输出和错误

         

    在默认情况下,Android系统将输出和错误(的System.out和System.err)输出到/ dev / null的。在运行在Dalvik虚拟机的过程中,可以让系​​统写入副本输出到日志文件。在这种情况下,系统使用日志标签输出和错误写入的消息到日志,都与优先级I

         

    要途径以这种方式输出,停止正在运行的模拟器/设备实例,然后使用shell命令setprop,使输出的重定向。这里是你如何做到这一点:

      $ ADB挡弹
      $ ADB壳setprop log.redirect,标准输入输出真
      $ ADB shell启动
     

         

    系统保留,直到终止模拟器/设备实例此设置。要使用设置作为模拟器/设备实例在默认的,你可以添加一个条目,以/data/local.prop设备上。

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.

  1. 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!");

  2. Capture the standard out/ standard error of the program and funnel it into LogCat. From the Android Debug Bridge page:

    Viewing stdout and stderr

    By default, the Android system sends stdout and stderr (System.out and System.err) output to /dev/null. In processes that run the Dalvik VM, you can have the system write a copy of the output to the log file. In this case, the system writes the messages to the log using the log tags stdout and stderr, both with priority I.

    To route the output in this way, you stop a running emulator/device instance and then use the shell command setprop to enable the redirection of output. Here's how you do it:

      $ adb shell stop
      $ adb shell setprop log.redirect-stdio true
      $ adb shell start
    

    The system retains this setting until you terminate the emulator/device instance. To use the setting as a default on the emulator/device instance, you can add an entry to /data/local.prop on the device.

这篇关于如何调用从C ++中的JNI Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆