Home > Back-end >  Android Studio: how and where to provide ResourceBundles?
Android Studio: how and where to provide ResourceBundles?

Time:01-02

I am in the process of migrating an Android application that I had written years ago (back then using Eclipse) to AndroidStudio. I got the stuff building and executing by now but now I am at the next hurdle: My code uses java.util.ResouceBundles to provide language specific messages and texts (e.g. button labels, column headers, menu texts).

After I moved the stuff to AS I have tried to move that stuff around but didn't succeed. All I ever get is:

java.lang.ExceptionInInitializerError
        at mmo.myapplication.Messages.getString(Messages.java:21)
 ...
     Caused by: java.util.MissingResourceException: Can't find bundle for base name mmo.myapplication.messages, locale en_US
        at java.util.ResourceBundle.throwMissingResourceException

ResourceBundles used to be a really simple and easy mechanism. How does Android (and/or AS) handle ResourceBundles (or their equivalent) these days? Where do these files have to be placed? Do I need to adjust anything in the gradle setup to get these deployed with the application?

CodePudding user response:

As I just found out: the "normal" .../src/main/resources folder is the location to put them. That "res" folder that Android uses for its resources, confused me...

CodePudding user response:

In Android, you can use Resources and AssetManager to load language-specific strings from strings.xml files in your project's res directory.

For example, you can define a default string in res/values/strings.xml like this: You can also use AssetManager to load language-specific strings from a file in the assets directory. To do this, you can define a default string in a file assets/strings.xml like this: This will also automatically select the appropriate string based on the device's language setting.

I hope this helps! Let me know if you have any questions.

  • Related