In my androidTest/resources directory I have a few hundreds of images. I want to read all of them in a loop and test against specific function. I don't want to use their names, as often I don't know them. Is there any possibility to load by regex or iterate through the directory?
I found a solution. Change the name of directory from resources to assets and then use InstrumentationRegistry.getInstrumentation().context.resources.assets.list("")
Related
According to this thread, in order to make Hebrew available on all devices, I need to maintain two similar files of each resource type, one in values-iw and another in values-he. Having a ready-to-use strings.xml, I was wondering if there's a way to "link" the files in one of these folders to the files in the second folder instead of making an alias for each string (exampled here).
Thanks!
In my app I want to update the strings.xml file at runtime. So the idea is to download the content from an external API and then update the strings.xml file. Does anybody had any past working experience on this or any idea how to achieve this goal will be a great help!!
You cannot modify resources at runtime. You are welcome to ship a new APK that contains the new resources, though.
You are also welcome to download files from the Internet, parse them, and use their contents. However, you have no means of using them literally as string resources. They will just be strings (or whatever else you parse from the file).
You can't do that. If you ever happen to add values to the strings.xml at runtime, their ids won't be generated in the R.class, you can alternatively save those strings to another file on the phone (database file, json file, xml, etc.).
I have a number of different string resource files that are built with my Android application using our build system. These string files can be added incrementally at any time and our build system will pick them up from their separate directory. I want to enumerate all of the string files or be able to obtain a single string in them without having to know the name or id of the string resource in them. I also don't want the person adding these to have to edit a main string file in my package that includes an array listing the different files. Is there any way to accomplish this?
Example:
SoccerStrings.xml
id="SoccerDetails" value="soccer"
CricketStrings.xml
id="CricketDetails" value="cricket"
Without knowing these files exist how can I provide a list view with two items: Cricket and Soccer in addition to automatically supporting any additional files that might appear.
I was thinking the best possible approach would be to have the build system pull the individual files under assets folder and then use the getAssets().list("") functionality along with the XMLResourceParser class to access the string values. Would this work and allow me to have id conflicts (ex: id="name")? Is there an easier way?
I would look at aapt. I know you can get the resources out of the .apk with the following command --
aapt d --values resources app.apk
It might be possible to use aapt to get the resources earlier in the build process. It has the following option which looks prosmising, but aapt documentation is a bit thin.
--output-text-symbols
Generates a text file containing the resource symbols of the R class in the
specified folder.
As another alternative, you could modify your build process and parse the string class directly out of the gen/<package>/R.java file after it has been generated. You could store that in a loadable file, or generate your own source file to add to the build.
I am not going to tell you how to do it. I am answering the question to advise against it.
What you are planning to do does not go well with the resource files. These files are basically code. What you want to do is about data. You should have your data in assets directory. Then these files won't be precompiled in your build like the resource file. You can process these files any way you want. There will be then quite a bit coding involved to convert all that into business logic, but that would be price to pay to write good, maintainable code.
Playing with resource files the way you are suggesting is akin to Java reflection. You want to use it only because you have functionality that is about such feature, not because it's the easiest way out.
Having said that, you might be exactly in a situation where you have to handle the resource files the way you stated. In that case, please accept my apologies.
I'd like the ability to "overwrite" the Android resources packaged within my apk by having the app periodically download a zipped file containing overrides that follow the same naming convention as the source does. For example, the zip might consist of the following paths:
res/values/strings.json
res/values-land/strings.json
My code would parse those files and produce a Map> that would map the string resource id to a folder->value pair (or something along these lines). At this point I'm really only concerned with strings and images (maybe arrays), but not layouts, etc.
To the point: Is there any method available, that, given a list of folder names, would tell me which one the Android resolver would choose based on current state? I'm assuming this is difficult because the compiler breaks everything down to ids, but figured it was worth a shot. Any ideas?
Is there any method available, that, given a list of folder names, would tell me which one the Android resolver would choose based on current state?
No. You are welcome to roll this yourself based on Configuration, DeviceMetrics, and kin. You will also need to create your own parsers for your own files, as Android's resource system only works with resources, not arbitrary files retrieved from arbitrary locations.
The expectation in Android is that if you want to update resources, you update the app, probably because there were also code changes as well. Admittedly, this approach has its limitations.
I have some sql queries that I would like to put in a .txt file and import to my application on start up. 2 questions:
1) Where is the best place to put these files?
2) I thought maybe I can put them in /res/raw/sql/ but the files don't come up with AutoComplete. (They come up if I put them in /res/raw/). Any ideas?
You cannot make sub folders of the resources folder Android provides per default. The same is true for the various layout folders etc. So you have to put them directly in /res/raw. But anyway, I think there it is the best place to save them.