Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android JNI what is current working directory for C/C++ executed code?

In Android if an C/C++ shared library created using NDK is invoked and it loads a file what is its currently working directory? Thanks

like image 388
Androider Avatar asked Nov 25 '11 10:11

Androider


2 Answers

The current directory is "/", not the application directory:

#include <jni.h>
#include <android/log.h>

char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL)
    __android_log_print(ANDROID_LOG_INFO, "", cwd);

To get the application directory, you need to use JNI calls to Java code, which in turn gets android application directory from Context.

like image 97
Nodrev Avatar answered Oct 03 '22 19:10

Nodrev


Negative. Native code getcwd() will return '/', which is not the application directory. To let native know where it is, must pass application directory (obtained from Context object) to native deliberately via a native method. Or try to call Context's method with native codes, which is too complex.

like image 25
alexhilton Avatar answered Oct 03 '22 18:10

alexhilton