Fabrizio Giudici is a Senior Java Architect with a long Java experience in the industrial field. He runs Tidalwave, his own consultancy company, and has contributed to Java success stories in a number of fields, including Formula One. Fabrizio often appears as a speaker at international Java conferences such as JavaOne and Devoxx and is member of JUG Milano and the NetBeans Dream Team. Fabrizio is a DZone MVB and is not an employee of DZone and has posted 67 posts at DZone. You can read more from them at their website. View Full User Profile

Using org.openide.util.Lookup on Android

04.30.2010
| 12218 views |
  • submit to reddit

The standard Lookup class has got a number of ways for the programmer to provide his own implementation behind the scenes of Lookup.getDefault(): a class name in a system property, or a Lookup or Lookup.Provider class registered in META-INF/services. Unfortunately, as I said before, I wasn't able to make any of those work because of issues with the classloader. I think that these issues can be solved (after all, OSGi can run on Android and it's heavily classloader-based), but need to learn more stuff. So, I resorted to an old trick of NetBeans developers that relies on reflection:

package it.tidalwave.bluebill.mobile.android;

import java.lang.reflect.Field;
import org.openide.util.Lookup;
import android.app.Application;

public class BlueBillApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();

try
{
final Field defaultLookup = Lookup.class.getDeclaredField("defaultLookup");
defaultLookup.setAccessible(true);
final BlueBillLookup blueBillLookup = new BlueBillLookup();
blueBillLookup.setContext(this);
defaultLookup.set(null, blueBillLookup);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}

Properly configured in the Android manifest, BlueBillApplication gets notified of the fundamental life-cycle events of the application and is able to initialize the global Lookup before any other part of the application kicks in.

Conclusion

What's ahead? I need to better understand the part about how Android does (not) manage resources embedded in jar files. If it's an unworkable limitation, one alternative could be to use the Maven Shade plugin that is able to transform and relocate files in a jar; it could copy the content of the META-INF/services/* directory in a properly Android-style resource under 'res' or 'assets'. In the same way, I could replace the code inside org-openide-util.jar that scans META-INF/services with a proper alternate implementation.

Also, I've also run into some incompatibilities between Lookup and the Android runtime. For instance, an internal implementation class had to be patched, as it raised an exception that doesn't occur with the Sun JDK; and there's an internal dependency on a simple event-related class of Swing, that is not available in Android. The Maven Shade plugin, that is able to replace single pieces in JAR contents and even renaming all the references to a given class, proved to be ok to deal with this kind of problems, as it allowed me to provide a patched for the broken class and the missing class without having to fork the original sources. I'm not going into details about this stuff, since in the end these fixes aren't needed in my current trunk - maybe I'll be back with this topic in a new post.

Published at DZone with permission of Fabrizio Giudici, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Jay Spring replied on Fri, 2010/04/30 - 5:07pm

I really want to learn Android. So I attempted to read this article. Though the sentences are so verbose I stopped reading after the second paragraph.

 

"Today I ran a fundamental round of refactorings trying to apply the best solution for the problem described by a specific question on the Android FAQ: "How do I pass data between Activities/Services within a single application?". For the most generic case (share any kind of Java object) there is no specific infrastructure in Android."

 

In English please?

Fabrizio Giudici replied on Fri, 2010/04/30 - 6:38pm

If it's verbose, it should suffice to drop the redundant parts :-) So:

"How do I pass data between Activities/Services within a single application?" There's no specific infrastructure in Android (for this purpose).

Then the rest of the article explains how you can do on your own.

Now, if you are asking what an Activity/Service is, you won't find an answer here, because I'm not writing Android tutorials for beginners - I think that there are a lot already available. 

Eugenios Ampelius replied on Wed, 2013/09/25 - 2:48pm

 Jay Spring, I also have the same problem. I "feel" the article is good, well written, but aside mass texting, I don't know how to do something on Android. Perhaps we should find some online courses and start from there. We are still too young for this website, apparently :).

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.