Over a million developers have joined DZone.

Using org.openide.util.NbBundle on Android

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

Yesterday I solved the final issue with the localisation of my Android application. Some of the code, reused from existing NetBeans Platform applications, depends on the class org.openide.util.NbBundle which is an enhancement of the plain ResourceBundle, offering support for branding (i.e., the capabilty of changing a resource in existing compiled code by only adding new configuration files) and for the dinamicity of a platform-based application, where modules (including branding modules) can come and go at runtime. The latter feature is probably useless on Android, while the former could be of some utility; in any case, my primary point was to reuse some code that already existed, was tested and worked eliminating the need for a fork or a cut & paste.

I found no problems with NbBundle per se; but it is available in the org-openide-util module, which is a sort of kitchen-sink of static utility methods, many involving Swing. And, of course, Swing is not on Android. The Dalvik class loader, when verifies the bytecode, complains when there are unresolved references, even though you will never use them, and break the application. The viable solution that I've found so far is simple, but tedious: to provide, in form of empty stubs, all the Swing / AWT required classes at the minimum level neeed to satisfy the bytecode verifier (that is, not necessarily with all the fields and methods that they have in their original versions). This turned out to be about 45 classes. Then, since it's illegal to bundle java.* stuff in an Android application, the Maven shade plugin made the trick of changing the offending package references to it.tidalwave.java.* etc.

In the end, everything worked.

This, of course, is a workaround to an existing NetBeans Platform problem, that is org-openide-util has gotten too large. Already with NetBeans 6.9 we are going to have an improvement (Lookup, which is one of the most useful and reusable parts of the NetBeans API, has been spinned off org-openide-util into its own module); perhaps in future we should go further with splitting, at least making sure that all the stuff that is not related to Swing doesn't require it as an indirect and useless dependency.

You can get the code by cloning the Mercurial repo from https://kenai.com/hg/bluebill-mobile~android-src (tag dzone20100514), looking at the modules/LookupForAndroid directory (the shade plugin configuration is in the modules/Main). After some clean up, this module will be moved to my TheseFoolishThings project and be available as a reusable Maven artifact.

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}