Androider
Androider

Reputation: 21335

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

Upvotes: 12

Views: 5138

Answers (3)

alexhilton
alexhilton

Reputation: 576

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.

Upvotes: 3

Nodrev
Nodrev

Reputation: 101

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.

Upvotes: 10

airun
airun

Reputation: 315

Maybe NDK sample at "android-ndk-rxx/samples/two-libs" will give you some useful message.

Upvotes: -3

Related Questions