Android Studio - Include ResourceBundles in Module
Asked Answered
P

4

6

I currently switched from eclipse to android studio. In eclipse I had 2 projects, one android application project and one java project which I included in the android project as library. This java project uses ResourceBundles to create internationalized error messages for it's own errors. This has been my project structure:

/MyApp
   /src
   /res
   ...
/MyLibrary
   /src
   /res (added as source folder to build path)
      /loc
         Bundle_en.properties 

This worked when loading the RessourceBundles as following:

ResourceBundle.getBundle("loc.Bundle", Locale.ENGLISH);

Now I switched to android studio and my new project structure looks like this (added the java library as module):

/MyProject
   /MyApp
      ...
   /MyLibrary
      /src
         /main
            /java
               ...
            /res
               /loc
                  Bundle_en.properties   

But I'm not able to load the ResourceBundles anymore, it's just throwing a java.util.MissingResourceException. I tried a lot of different locations for the ResourceBundles and different paths but I'm going to get crazy because nothing seems to work. Could anybody explain where to put those bundles and how to load them?

Thank you!

Piapiacenza answered 20/2, 2014 at 14:53 Comment(5)
MyLibrary is using the java plugin in build.gradle?Freehand
Yes it uses apply plugin: 'java'Piapiacenza
src/main/java is an Android-Gradle directory convention; the Java plugin may not know to look for resources inside there.Freehand
Where do I have to put those ResourceBundles then? Tried MyLibrary/src/loc but that didn't work as well.Piapiacenza
Didn't work as well =/ Neither with "res.loc.Bundle" nor "loc.Bundle" nor "Bundle".Piapiacenza
L
11

Faced exactly the same problem. To make it work I finally had to create a resorces folder in my project module's main folder.

Android resources folder

here multiple files starting with the same name (as messages in this picture) gets bundled as a resource bundle.

Finally had to call it using ResourceBundle.getBundle("org.eclipse.paho.client.mqttv3.internal.nls.logcat") or ResourceBundle.getBundle("org.eclipse.paho.client.mqttv3.internal.nls.messages") to get the required resource.

Lerner answered 16/6, 2015 at 11:39 Comment(2)
Works! I found this hint helpful: "This resources folder can be created via the context menu (New > Folder > Java Resources Folder)." #29635962Bleareyed
This works but actually it is not necessary to create a new resource folder but the existing folder can be included as resource folder in build.gradle, see my answer.Adnopoz
A
1

If you include the second project as a library, you might not want to create a new resource folder as suggested in a previous answer (which does work). Instead, you can simply add the library's resource folder to your resource directories in your module's build.gradle: to the android section add

sourceSets {
    main.resources.srcDirs += 'path/to/your/libs/res'
}

If now the added res folder contains org/mypackage/Bundle.properties you can refer to it using

ResourceBundle.getBundle("org.mypackage.Bundle")

Actually adding a new resource folder does nothing more then adding it as a resource directory in build.gradle.

Adnopoz answered 31/8, 2017 at 15:27 Comment(0)
G
0

I never tried but Intellij comes with very good integration of Resource Bundles.

Refer this link

http://www.jetbrains.com/idea/webhelp/resource-bundle.html

From the link above

Resource bundle is a set of properties files that have same base name with different language-specific suffixes. A resource bundle contains at least two properties files with similar base name, for example file_en.properties and file_de.properties.

IntelliJ IDEA recognizes properties files, and if two or more properties files with the names that differ only in suffix, are encountered, joins them into a resource bundle. The new node Resource Bundle '(base name)' appears in the Project Tool Window:

resource Bundles

You can have these files inside your module or on root as well.

Glede answered 20/2, 2014 at 15:20 Comment(1)
Hey, thanks for the information. Android Studio is recognizing these files but I'm not able to load them in Java. Android just doesn't find them whatever I try.Piapiacenza
S
0

First please ensure your resource folder (where the property file is localted) is in the classpath and you can easily find that by calling the following.

 URLClassLoader ldr = (URLClassLoader)ClassLoader.getSystemClassLoader();
 URL[] urls = ldr.getURLs();
 for(URL url : urls)
   {
      System.out.println(url.getPath());
    }

Now if you find your resources folder in the classpath then you can simply call the bundle base name, in your case ResourceBundle.getBundle("Bundle"), no need for a fully qualified path. Assuming you are using English locale, it should find it. You can further add en_US, en_NZ, en_GB etc if needed.

If you do not find your property folder then make sure it is in the classpath and if you need to add it dynamically follow this thread.

How do you change the CLASSPATH within Java?

Remember the only addition for loading property files dynamically is that you MUST call findResource or findResources API on the class loader to load the property file. Hope this helps.

Spanner answered 14/4, 2015 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.