Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using serviceloader on android

I am very new to java and android development and to learn I am trying to start with an application to gather statistics and information like munin does. I am trying to be able to load "plugins" in my application. These plugins are already in the application but I don't want to have to invoke them all separately, but be able to iterate over them. I was trying to use serviceloader but could never get the META-INF/services into my apk. So I am wondering if it is possible to use serviceloader on android Thanks
EDIT: I am asking about java.util.ServiceLoader, I think it should, but I can't figure out how to get my services folder into META-INF on the apk

like image 650
silverchris Avatar asked Apr 22 '11 22:04

silverchris


3 Answers

The META-INF folder is deliberately excluded from the APK by ApkBuilder; the only comment in ApkBuilder.java is "we need to exclude some other folder (like /META-INF)" but there is no other explanation.

Even after adding META-INF with ant, you will still get in trouble if you want to use Proguard, which refuses to replace the content of META-INF/services/* files or rename them (that's another story, the author wants to keep Proguard agnostic).

However, people using maven may want to check https://github.com/pa314159/maven-android-plugin (the branch named "modified"), that tries to solve both issues. It is a fork from the original "android-maven-plugin" I modified one month ago for my own Android projects.

It also provides a patch for Proguard-4.7

Hope this helps, any feedback is welcome.

like image 44
PA314159 Avatar answered Nov 08 '22 14:11

PA314159


There is an open bug report against this issue. See https://code.google.com/p/android/issues/detail?id=59658

like image 170
Gili Avatar answered Nov 08 '22 12:11

Gili


I've figured out a solution that may work for some situations. Instead of ServiceLoader, I'm using the org.openide.util.Lookup class / library that comes with NetBeans - it is a superset of ServiceLoader. It does not require NetBeans itself and seems to work ok with Eclipse. It is necessary to replace whatever ServiceLoader functionality you are using in your application with Lookup equivalents, and add the org-openide-util-lookup library. Then, you can just do something like this:

Lookup lookup = new ProxyLookup(Lookup.getDefault(),
                Lookups.metaInfServices(myClass.getClassLoader(), "services/"));

And move your ServiceLoader files from META-INF/services/ to services/.

Note that, because of the ProxyLookup, this will continue to work on standard Java environments unchanged (i.e., in those cases it will continue to look in META-INF/services).

Here is a link to the documentation for the library: http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/lookup/Lookups.html

UPDATE

After working with this for a couple of days, it seems to function well - I move between environments (standard Java and Android) and it works properly in each location. The primary downside is having to manually copy the files to the /services directory.

like image 3
djilk Avatar answered Nov 08 '22 13:11

djilk