Native getenv function return always null

440 views
Skip to first unread message

Jérôme Haxhiaj

unread,
Sep 28, 2017, 11:30:18 AM9/28/17
to android-ndk
Hi,

I'm tracking some bugs in an application and found some strange behavior with the function getenv().
It returns automatically null.
I tried with variables USER and TMPDIR, the result remains the same.
This issue occurs on Android 6 and 7, with clang and gcc.

My code is below :

JNIEXPORT jstring JNICALL
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
char *tmp = NULL;

/* Get temp dir */
tmp = getenv("TMPDIR");
__android_log_print(ANDROID_LOG_INFO, "ProcTest", "User is located in %s", tmp);

return (*env)->NewStringUTF(env, "getenv unit test .");
}

Jerome

Dan Albert

unread,
Sep 28, 2017, 12:44:38 PM9/28/17
to android-ndk
TMPDIR isn't set on Android. You'll want to pass your app's cache directory through from Java. https://stackoverflow.com/a/6485850/632035

I believe USER is also empty in an app context, as it isn't particularly meaningful.

If you just want to check that getenv exists, try PATH.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/ed631264-dda1-4e47-9440-f8b4f3d624e6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jérôme Haxhiaj

unread,
Oct 5, 2017, 9:24:32 AM10/5/17
to android-ndk
Thank you for your answer.
getenv("PATH") worked fine
Reply all
Reply to author
Forward
0 new messages