重定向标准输出中的Android NDK到logcat的 [英] Redirect stdout to logcat in Android NDK
问题描述
我不能让我的Nexus S(运行Android 4.0)重定向本地标准输出消息logcat的。我读过,我需要做到这一点:
I can't get my Nexus S (running Android 4.0) to redirect native stdout message to logcat. I've read that I need to do this:
$ adb shell stop
$ adb shell setprop log.redirect-stdio true
$ adb shell start
不过,这似乎并没有工作。 (它确实打破的JUnit不过,正如<一个href="http://stackoverflow.com/questions/3462850/trying-to-run-trivial-android-junit-tests-getting-test-run-failed-no-test-re">here,所以它不是没有效果。)
However, this doesn't seem to work. (It does break JUnit though, as mentioned here, so it's not without effect.)
有关参考,这里是我的code:
For reference, here's my code:
package com.mayastudios;
import android.util.Log;
public class JniTester {
public static void test() {
Log.e("---------", "Start of test");
System.err.println("This message comes from Java.");
void printCMessage();
Log.e("---------", "End of test");
}
private static native int printCMessage();
static {
System.loadLibrary("jni_test");
}
}
和JNI的.c文件:
JNIEXPORT void JNICALL
Java_com_mayastudios_JniTester_printCMessage(JNIEnv *env, jclass cls) {
setvbuf(stdout, NULL, _IONBF, 0);
printf("This message comes from C (JNI).\n");
fflush(stdout);
//setvbuf(stderr, NULL, _IONBF, 0);
//fprintf(stderr, "This message comes from C (JNI).\n");
//fflush(stderr);
}
而Android.mk:
And the Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := jni_test
LOCAL_SRC_FILES := test_jni.c
include $(BUILD_SHARED_LIBRARY)
我只是通过调用 NDK建造
编译这一点。本机方法被正确调用,但我没有得到任何日志输出它(即使在详细)。我得到从Java输出日志,但(此消息来自Java的。)。
I'm compiling this just by calling ndk-build
. The native method is called correctly but I don't get any log output from it (even on verbose). I do get the log output from Java though ("This message comes from Java.").
什么,我可能是做错了任何提示?
Any hints of what I might be doing wrong?
PS:我已经设置了一个小Mercurial库演示该问题:<一href="https://bitbucket.org/skrysmanski/android-ndk-log-output/">https://bitbucket.org/skrysmanski/android-ndk-log-output/
PS: I've set up a small Mercurial repository that demonstrates the problem: https://bitbucket.org/skrysmanski/android-ndk-log-output/
推荐答案
这不是明显,我从previous答案。即使在root权限的设备,你需要运行:
This was not obvious to me from the previous answers. Even on rooted devices you need to run:
adb root
adb shell stop
adb shell setprop log.redirect-stdio true
adb shell start
这篇关于重定向标准输出中的Android NDK到logcat的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!