Android Studio - Include ResourceBundles in Module

2020-02-14 02:46发布

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!

4条回答
一夜七次
2楼-- · 2020-02-14 03:04

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.

查看更多
家丑人穷心不美
3楼-- · 2020-02-14 03:07

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.

查看更多
Root(大扎)
4楼-- · 2020-02-14 03:20

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.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-02-14 03:22

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.

查看更多
登录 后发表回答