3

I'm building c++ code for Android using NDK. My code will be used as an external SDK for app developers.

Is it possible to access the application's data path from the native code?

One option is to use JNI to call into the JVM and look up this information, however since i am writing library code, i am not sure my SDK won't be used in an all native applicaiton (NativeActivity).

What is the best option for achieving this?

lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
  • 1
    A Native Activity still has a VM on which it depends heavily - it's only "native" in the sense that the 3rd party developer doesn't write any Java code, but plenty is still utilized. – Chris Stratton Feb 10 '14 at 16:56
  • @ChrisStratton is right, you should not worry, standard Java SDK will still be available even with NativeActivity. But there is a clever workaround that does not need Java: http://stackoverflow.com/a/6284443/192373! – Alex Cohn Feb 10 '14 at 19:49

1 Answers1

5

It is possible to access application's data path from the full native code. External and internal storage paths are stored inside android_app->activity, which is of type ANativeActivity and android_app struct is defined at the entry point of the native activity, so you don't have to worry about anything.

/**
* Path to this application's internal data directory.
*/
const char* internalDataPath;

/**
 * Path to this application's external (removable/mountable) data directory.
 */
const char* externalDataPath;

In case there is no native activity, storage paths can be queried through JNI as it is stated in the comments.

eozgonul
  • 810
  • 6
  • 12
  • As I told, the entrance point of the native activity takes android_app as input, like, void android_main(struct android_app* state) as you could see in http://developer.android.com/reference/android/app/NativeActivity.html . Then, state->activity->externalDataPath gives the external data storage of the program, etc. – eozgonul Feb 11 '14 at 10:06