Reputation: 69
I have changed the HelloJni sample of the android ndk, I want to write something to the stdout. Here is the Jni code :
#include <stdlib.h>
#include <jni.h>
#include <stdio.h>
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
printf("Hello from C !\n");
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
And this is my log :
09-28 13:07:02.906: I/ActivityManager(1650): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.hellojni/.HelloJni u=0} from pid 1790
09-28 13:07:03.007: D/dalvikvm(1650): GC_FOR_ALLOC freed 687K, 10% free 9300K/10247K, paused 62ms, total 62ms
09-28 13:07:03.070: D/dalvikvm(3562): Not late-enabling CheckJNI (already on)
09-28 13:07:03.085: I/ActivityManager(1650): Start proc com.example.hellojni for activity com.example.hellojni/.HelloJni: pid=3562 uid=10052 gids={1015, 1028}
09-28 13:07:03.101: I/dalvikvm(3562): Turning on JNI app bug workarounds for target SDK version 3...
09-28 13:07:03.132: D/dalvikvm(3562): Debugger has detached; object registry had 1 entries
09-28 13:07:03.171: D/dalvikvm(3562): Trying to load lib /data/data/com.example.hellojni/lib/libhello-jni.so 0x414af318
09-28 13:07:03.179: D/dalvikvm(3562): Added shared lib /data/data/com.example.hellojni/lib/libhello-jni.so 0x414af318
09-28 13:07:03.179: D/dalvikvm(3562): No JNI_OnLoad found in /data/data/com.example.hellojni/lib/libhello-jni.so 0x414af318, skipping init
09-28 13:07:03.265: V/TabletStatusBar(1715): setLightsOn(true)
09-28 13:07:03.328: I/ActivityManager(1650): Displayed com.example.hellojni/.HelloJni: +279ms
09-28 13:07:07.078: E/ThrottleService(1650): problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)
I have set the log.redirect-stdio to true, so that I could see the Hello line in my LogCat.
Does someone have an idea about how to get this work ? Thanks.
To be more clear, what I want to do is to see my output in stdout. There is no problem with , I just want to know whether I can do output to stdout. Thanks for who has given the solution __android_log_print.
Upvotes: 6
Views: 15827
Reputation: 12372
Android Studio
If you use Android Studio and gradle, it ignores Android.mk. Add this to your build.gradle file:
android {
defaultConfig {
ndk {
moduleName "your_module_name"
ldLibs "log"
}
}
}
Eclipse
you need to add LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
to your Android.mk file..
you just need to define your own helper macros, like this...
#include <android/log.h>
#define LOGV(TAG,...) __android_log_print(ANDROID_LOG_VERBOSE, TAG,__VA_ARGS__)
#define LOGD(TAG,...) __android_log_print(ANDROID_LOG_DEBUG , TAG,__VA_ARGS__)
#define LOGI(TAG,...) __android_log_print(ANDROID_LOG_INFO , TAG,__VA_ARGS__)
#define LOGW(TAG,...) __android_log_print(ANDROID_LOG_WARN , TAG,__VA_ARGS__)
#define LOGE(TAG,...) __android_log_print(ANDROID_LOG_ERROR , TAG,__VA_ARGS__)
Works very well.
Upvotes: 16
Reputation: 58467
Use the Android logging functions:
#include <android/log.h>
....
__android_log_print(ANDROID_LOG_VERBOSE, "MyApp", "The value of n is %d", n);
Note to those reading this after the question was edited: the question originally didn't specify that the OP didn't want to use the functions in <android/log.h>`.
Upvotes: 15
Reputation: 3283
To print to the Logcat via native code you'll have to use Android's native Logger.
Its something like that:
#include <jni.h>
#include <android/log.h>
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,DEBUG_TAG,__VA_ARGS__)
And you use it inside your function:
LOGD("hello");
In order for this to work you'll have to add The native LOG to you compilation: (Android.mk)
LOCAL_LDLIBS := -llog
Upvotes: 4