13

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

silverchris
  • 139
  • 1
  • 4
  • It would help we had any idea what "serviceloader" is. – CommonsWare Apr 23 '11 at 00:06
  • 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 – silverchris Apr 23 '11 at 01:10
  • I'm trying this myself, and I found [this person](http://groups.google.com/group/android-developers/browse_thread/thread/ba98351e50897499/2c96aecee4ece3d0) who has managed to do it, unfortunately, he's kind of light on the details. I've asked for some more info and if he gets back to me I'll be sure to let you know here :) – SubmittedDenied Jun 11 '11 at 15:28
  • There are more details in the info indicated by @SubmittedDenied in [Android issue 17450](https://code.google.com/p/android/issues/detail?id=17450&can=1&q=ServiceLoader&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars), but I have not been able to reproduce it for my project - modification to the apk happens after signing and thus cannot be installed – Ivin Mar 09 '12 at 11:15
  • Possible duplicate of [Keep 'META-INF/services'-files in apk](http://stackoverflow.com/questions/16898409/keep-meta-inf-services-files-in-apk) Since the author of this question hasn't been online since 2011 I didn't post my answer here. – AlexS Oct 16 '15 at 09:35

5 Answers5

5

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

Gili
  • 86,244
  • 97
  • 390
  • 689
4

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.

PA314159
  • 106
  • 1
  • 6
3

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.

djilk
  • 387
  • 3
  • 4
  • Lookup is relevant for Java, not for Android.`this will continue to work on standard Java environments unchanged`, not Android. – Infinite Recursion Dec 17 '13 at 04:03
  • No, Lookup works on Android, you just have to include the library jar (org-openide-util-lookup.jar) in the app. My point was that the code I included works properly in both Android and standard Java environments because of the Proxy aspect. – djilk Dec 19 '13 at 14:54
2

It is possible. You may want to check http://developer.android.com/reference/java/util/ServiceLoader.html

Vicente Plata
  • 3,370
  • 1
  • 19
  • 26
  • 3
    Do you know how to get the META-INF/services into the apk? I have been trying for hours and it always seems to be excluded. I am using eclipse to write and build the program – silverchris Apr 23 '11 at 01:48
  • 1
    AFAIK your only choice is to use an Ant script after the apk creation, and insert the META-INF folder using the jar task. – Vicente Plata Apr 23 '11 at 03:11
-5

ServiceLoader is stuff from the Java language that is not really relevant on Android. I recommend not using it. If you just want to find a list of classes within your .apk to load, there are all kinds of ways to do this -- put in XMl file in res/xml that lists them, use reflection, annotations, etc.

hackbod
  • 90,665
  • 16
  • 140
  • 154
  • I am having trouble finding much information on either of the methods you suggested to load "plugins" without me specifying every one to run by hand. I could really use a good link or example because I am a major java noob Thanks – silverchris Apr 23 '11 at 07:17
  • 15
    The ServiceLoader class is included in Android API level 9 so it is relevant. Including the list of implementing classes in a resource file won't work as it needs to go into the a file called `META-INF/services/` in the APK. – SubmittedDenied Jun 11 '11 at 12:41