Android files manipulation - android

Through the housekeeping stuff my android application has to do is to read and write some files and sometimes to delete others and create new ones. The files in cause have all "sdcard/" as root. Is it safe if I perform all this task using Java style functions ?
To me it is very handy to do it this way but I've read that not always the java functions can be applied when it comes to files in Android.

Yes you can access /sdcard with regular java.io classes in Android. But don't hardcode this root; use Environment.getExternalStorageDirectory() as the root.

Related

What's the correct way to use JSON files in a library?

I am currently creating an android library that uses json commands for communication with another library. I would like to extract these commands from code and save them in separate files for better readability.
Where do I save those files when there is no asset directory and how do I read them?
Edit: I have found an answer to my question:
how to access resources in a android library project
Have a look at Internal Storage. The files saved here are only available to your app. It uses the Java File API to read an write using the FileInputStream and FileOutputStream
UPDATE:
As per the discussion in the comments, OP was looking for a method to ship a JSON file with the library. In light of that:
I'm not sure if library modules support raw resources. If they do, you might want to use that but it will increase the size significantly. You could also fetch the file from a server the first time you're the library is used, keeping track of that using a SharedPreference entry.

Android Studio - What is the Java Resources Folder?

I know there are two well-known folders where one can put resources.
The first is the /assets folder, the documentation says:
Contains file that should be compiled into an .apk file as-is. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
The second is the /res folder, the documentation says:
Contains application resources, such as drawable files, layout files, and UI string. See Application Resources for more information.
Now i was wondering, there is a third option to create a resource folder "New > Folder > Java Resources Folder"
To me i looks like some part of the Android Plugin in IntelliJ because it has a little Android symbol in front of it .
Any ideas what the use of it could be? I couldn't find any documentation about it.
My first guess would be to use it in situations where you want to supply resources to a JVM Test.
In standard java world
Resources can be embedded directly in "your source tree" and used with Class's method getResource (see java documentation for a more precise description).
In android world
This practice is not recommanded (do not work at all, because such resources are removed from generated APK). You can still declare Java resources folders (see build.gradle :
sourceSets {
main {
resources.srcDirs = ['src/main/java/yourresourcesfolder']
}
}
And the result in the apk :
Conclusion
As resources's folder tree is removed,using java's resource folder become from my point of view, useless (in an android projet). Using android's asset folder is a better choice (also avoid resource's name conflict, but it's an another story :D )
This post is a bit old, but I want to bring an answer that explain one use case for the java resources directory on Android. If this folder exists in the options of a project it's because something can be done with it...
Enters the Service Loader, that helps to connect other services (aka libraries) into your main app, it can be used as a Gateway for your Android library to extend the functionality of a feature, exposing only the interfaces or abstract classes, with the Service Loader providing the implementation instead of your project having direct access to them.
Here is an example of how it's implemented. So, in summary the folder (META-INF/services) and files you have to create in order to use the Service Loader on an Android App, NEED to be inside this Java Resources Folder, otherwise your provider won't be able to see any implementation.
You can see it working in my sample here if you want to check it out: https://github.com/difereto-globant/test-library-feature/tree/1.0.9.

How safe is to save properties files in assests or raw folders?

For the application I'm working on I was thinking to define few property files, something like: debug.properties, beta.properties, release.properties. These files mostly will contain urls to their corresponding environments. Depending of some flag I will set, the application will load the corresponding property file and read the values from there.
I was thinking to put these files in assets or raw resource folder, but after changing the extension from .apk to .zip of my application, I noticed that these file are accessible and readable.
If you had prior experience, what are the best practices for configuring different build environments in Android?
Is there a better place where I could put the properties files so they cannot be accessed?
I had the same need in my app and found the same problem you found: It's to easy to open the apk and edit the properties files.
The solution I chose was to put those environment-specific properties in some Java source files.
We have one Android project per environment. Each project is almost empty and refers to an Android Library that actually contains the app's code. Each Android project contains a Java file declaring its environment properties.
This seems counter-intuitive to use Java instead of properties file, but when fighting against piracy, all ways are good. The Java files can be obfuscated if you use Proguard. Even better, you can use Dexguard that can encrypt the strings in the Java source so that it will extremely hard to tamper the source and make the properties point elsewhere.

Replace string in a function at runtime Android NDK

I have an external compiled static C++ library that I'm using in my android application. This library is reading a file. I want to know if there is a way I can "redirect" the function that's reading the file so that it reads another file.
So if it does:
fopen("myfile.txt", "rb");
I want to intercept it and to do this instead:
fopen("myotherfile.txt", "rb");
In Objective-C I use MethodSwizzling. Is there something similar I can do in C++ or the android NDK?
Short of editing the binary (with uncertain results), your best option is to use a symlink... if you're just doing it for development purposes, you could use adb shell into your test device to create the symlink.
http://developer.android.com/guide/developing/tools/adb.html#issuingcommands
How about contact the author of the library and ask them to introduce a parameter? Having hard-coded file paths is a lousy design anyway, the library will be better off.

GetFilesDir() from NDK?

Is there a way to get the application's directory to save private data to a file for my application directly from the NDK? In other words, I need an equivalent of the Java function mContext.getFilesDir().
I have noted that other posts such as this one:
Android NDK Write File
mention what is the 'usual' place for this directory to be. But for it to be correct in all versions and all devices, an equivalent system call is surely necessary.
I am currently using a NativeActivity and no Java at all.
Thank you for your time.
I have custom helper class for accessing various paths, for the data directory I do this:
Read /proc/self/cmdline or /proc/**MyPID**/cmdline
Append the results from #1 to /data/data/
Example results:
/data/data/com.yourcompany.yourproduct/
If you are using NativeActivity then you have access from native code to an ANativeActivity instance (see <ndk_root>/platforms/android-9/arch-arm/usr/include/android/native_activity.h) which has internalDataPath and externalDataPath members.

Categories

Resources