0

I'm developing a native library for Android and I need to write/read some temporary files. As it's library I don't know in what context it will be used later.

Searching on SO I've found out that data/data/<Your_package_name_usually com.android.appName>/files/ is a good place to put write/read files, but I don't know how to get package name using NDK and it seems this is not valid in all conditions(Android NDK Write File)

Other solution would be to use internalDataPath from ANativeActivity, but I don't know how to obtain a valid ANativeActivity object from the library(GetFilesDir() from NDK?)

Is there any safe place put temporary files on Android using C++?

Please forgive me if I write some dumb things here, I'm new to Android development. Thank you!

EDIT: As a design requirement I'm not allowed to use Java/JNI calls.

Community
  • 1
  • 1
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
  • 2
    That certainly makes things more difficult. Can you not even pass in the cache folder path into your initial NDK startup code? – benjymous Dec 05 '13 at 13:52
  • Your no-JNI requirement seems ignorant. No Android app functions without JNI calls, as even if you write none yourself, the platform-provided code is chock full of them. However, you have options such as unix domain sockets, or even opening a file from java and discovering it in /proc/pid##/fd with your native code. You have numerous ways of discovering the package name, for example looking in your /proc/pid##/maps file to find your apk. – Chris Stratton Dec 05 '13 at 15:34
  • @ChrisStratton I'm not the library designer... Similar approach works on 14 different platforms except Android. I think google's approach to make only Java APIs is ignorant, not mine:) – Mircea Ispas Dec 05 '13 at 15:40
  • Google's decision is frustrating certainly, but your thinking that you can avoid JNI is what ignorant, because even if you don't knowingly use any in code you write yourself, your program is still full of such usage. I've provided you several alternatives to obvious usage if that makes your employers happier, but as a technical matter **JNI will be used** regardless if you have "permission" to use it or not. – Chris Stratton Dec 05 '13 at 15:41
  • @ChrisStratton I don't say no because I'me new to Android and I might be wrong:) JNI calls are presenst in the library only if POSIX/C functions go through Java, but I doubt... – Mircea Ispas Dec 05 '13 at 15:46
  • No, JNI calls are unavoidably mixed throughout all of the Android source code, both on the system side and on the side that ends up part of your application at runtime. Regardless if you write only Java code, or only native code, **your app will heavily use JNI** when it runs on Android. – Chris Stratton Dec 05 '13 at 15:48
  • However, I personally think that benjymous's solution is best - provide the path when you startup the library. It's pretty hard to see how your library can do much useful without *some* form of interaction with the rest of the application program, so whatever form you are content with, you can use to pass this information. – Chris Stratton Dec 05 '13 at 15:51
  • @ChrisStratton Probably this is what it will be done in the end. Thanks for help! – Mircea Ispas Dec 05 '13 at 15:52
  • Incidentally, you should know that you can't just safely build some generic cross-platform code and blindly link it into an Android app. For reliable behavior, you are going to have to put some care into handling the more "surprising" implications of the Android `Activity Lifecycle` such as how an activity and a process do not have a 1:1 mapping **in either direction** – Chris Stratton Dec 05 '13 at 15:54
  • Why don't you just use the paths stored in your native activity object? – eozgonul Dec 06 '13 at 06:29
  • @user2359247 because I don't have that object and I don't know how to get it. – Mircea Ispas Dec 06 '13 at 14:23
  • If you are writing an application that does not communicate with Java side, in the end you will have to have an android_app object which contains a ANativeActivity object that has internal and external data paths stored. – eozgonul Dec 06 '13 at 15:03
  • @user2359247 I'm developing a library. I don't know from what kind of application it will be used – Mircea Ispas Dec 06 '13 at 16:40

1 Answers1

0

I've been able to do such things using /data/local/tmp/, but I'm not sure it works in every situations.

mbrenon
  • 4,851
  • 23
  • 25