Bundle APK with dynamic application properties file? - java

Is it possible to bundle or generate an APK with some sort of dynamic file where you can set different application properties, such as background colors, etc?
Thanks!

This is a high level answer, but assuming they are downloading the apk file from a webserver, there's nothing stopping you from generating some sort of configuration file, dropping it in res and accessing it from your application. An apk file is simply a "zip" file with some extra trimmings. It would not be difficult to have your classes.dex file along with the other requisite files on the server, and create an apk file on the file using some sort of zip library. Now, I'm not familiar off the top of my head how you could access an arbitrary file (such as an xml file or .properties file) out of the res folder, but a little bit of googleing should give you that answer.

Related

Writing to text file created manually inside app

Is it possible in Android, to manually add a file inside a project and then, modify it? Example:
"I have a test.txt file in the following path: "app/src/data". I would like to make a method to write a given String in the test.txt file."
Is that possible? I been looking everywhere, but can't seen to do such an easy task.
If you mean modifying files inside the APK itself then it's not possible. Besides, the folder structure you see in the project is not the final structure on the APK (just unzip your APK, it's a .ZIP really): Fpr example, the source directory is all compiled into a classes.dex. The res/ directory is compiled and fully copied ...
Take a look at How to write files to assets folder or raw folder in android?
and https://developer.android.com/training/basics/data-storage/files.html
You can read raw files stored in /res/raw, or assets stored in assets/ , but you cannot modify stuff inside the APK itself.
What you can do is create and modify as many files as you wish from the different places Android gives to any app, such as:
CACHE directory (context.getCacheDir() -> /sdcard/Android/data/your.package/cache
External files (context.getExternalFilesDir() -> /sdcard/Android/data/your.package/files
Arbitrary directories in the SDCARD.

How to stop creating thumbs.db files in compression using java

I am trying to compress a folder to zip and extract the same folder using java. When i zipped or unzipped the folder the files which are in the folder compressed and extracted successfully. But a Thumbs.db file is creating with in the folder.
I want to delete/avoid the Thumbs.db files before/after compressing to zip or extracting from zip programmatically.
Is it possible in java ?
Thumbs.db is usually a file with the system and hidden attributes (see attrib.exe ) and therefore invisible in the Explorer (unless you set the Explorer to show all files including system files).
When you extract the file you should set it's attributes accordingly.
How to set a file attribute using Java is already described here: https://stackoverflow.com/a/36465283/150978

Location of ogg files with EasyOgg

After looking at a couple of audio libraries, I settled on EasyOgg. Most of the samples report that you play sounds like this:
new OggClip("mysfx.ogg").loop();
This crashes with Couldn't find: mysfx.ogg at runtime. I tried several things:
Plain filename
Relative path from my project root directory
Forward-slashes and backslashes
I can't figure out where exactly the file goes, and how to specify the name. It seems like they should be somehow embedded in my application JAR. (I just have them sitting on the file system.)
I fiddled around with it for a while and came up with a solution using InputStreams:
FileInputStream stream = new FileInputStream("file.ogg");
OggClip clip = new OggClip(stream);
This works without including the files in the jar.
I'm not familiar with EasyOgg, but the first thing I would do is pass in the complete location to the file as a sanity check.
new OggClip("/home/someuser/audio/mysfx.ogg").loop();
If you can't count on java being run from the same location every time, you can use an environment variable to point to the location that your files are sitting in.
new OggClip(System.getenv("MY_APP_HOME") + "/audio/mysfx.ogg").loop();
As far as getting to a resource from inside a jar file, have you tried getResource()?
See: Access file in jar file?
I discovered that the OGG files need to be in the JAR file. This is clear from the working zip samples I found on the interwebz.
To use Gradle to zip up every .ogg file in audio, I add this to my jar task:
from fileTree(dir: '.', include: 'audio/**/*.ogg')
This works, except when I debug from Eclipse. A better solution is to create a separate project (I called mine EmbeddedResources) which creates a JAR that only contains .ogg files. Then, I reference this project from my game project, and I'm done.

Text File + JNLP

I’m trying to figure out how to include a reference to a external data file (in text form) that I want distributed along with my application via Web Start (JNLP). Sifting through the documentation for the JNLP structure, I see that you can include references to JAR, nativelib, and extensions – however, I don’t see a means to include a text file resource. How can I accomplish this so that Web Start will download the text file from the server and store it locally along with my application?
I don't believe you can do that.
You can, however, put it on your classpath (in a jar) and reference it through getResourceAsStream().
i just checked that jnlp file is also available as jar file also and as exe file also, so can i open these in a java gui and load my xml file

Config files - where to put them in Java?

I have a java desktop app and the issue of config files is vexing me.
What I want is for my distributable application folder to look like this:
MyApp/Application.jar
MyApp/SpringConfig.xml
MyApp/OtherConfig.xml
MyApp/lib
But at the moment SpringConfig.xml is inside Application.jar and I can't even find OtherConfig.xml programmatically.
I don't care how I set up the various files in my compilation path, so long as they end up looking like the above.
So..
where do i put the files in my dev setup?
and how do i access them programmatically?
thanks
the spring config file is related to the code and wiring of your application, hence it'd better be inside the jar, and should be subject to change by the users
(new File(".")).getAbsolutePath(); returns the absolute path of your jar - then you can load the OtherConfig.xml by a simple FileInputStream
if the SpringConfig.xml contains configuration data like database credentials, put them in an external application.properties and use a custom PropertyPlaceholderConfigurer to load the external file.
Answering the question "where do I put the files in my dev setup" is not possible because we don't know your environment.
Actually, if you want to be able to edit the config yourself (and not necessarily end-users), you can open the jar with any zip software (WinRAR for instance) and edit the config file from within the jar.
Update: as it seems you can't make the config files to be places out of the jar. Well, for a start, you can do it manually - whenever the .jar is complete, just remove the config file from inside and place it outside.
I typically create a structure where I have a src/ directory and then other directories exist at the same level. Some of those directories include:
lib/ - External Libraries
config/ - Configuration Files
resources/ - Various resources I use (images, etc)
At that same level, I then create an Ant script to perform my build so that the appropriate config files, resources, lib, etc are copied into my JAR file upon build. It has worked great for me up to this point and is a fairly easy to understand organizational structure.
Update: Accessing my config files is done, typically, by knowing their location and opening them up and reading them in the code. Because I use Ant to build, I make sure that my config files are in a location that I expect. So, for example, in a recent application I created, when I compile, my JAR file is in the top level directory (relative to the application release structure). Then, there is a "main" config file at that same level. And there is a "theme" config file that is in a themes folder.
To read the various files, I just open them up as I would any other file and read them in and go from there. It's nothing particularly fancy but it works well and it makes it easy to manually change configurations if I need to do so.
In dev mode, put them in source dir and they will be copied to your classes folder, you can then access them using classloader.
Example:
URL url = ClassLoader.getSystemResource("test.properties");
Properties p = new Properties();
p.load(new FileInputStream(new File(url.getFile())));
In Prod mode, you can make them part of your jar.

Categories