在android上使用serviceloader

发布于 2024-11-02 19:41:14 字数 302 浏览 1 评论 0原文

我对 java 和 android 开发非常陌生,为了学习,我尝试从一个应用程序开始,像 munin 一样收集统计数据和信息。我试图在我的应用程序中加载“插件”。这些插件已经在应用程序中,但我不想单独调用它们,而是能够迭代它们。我试图使用 serviceloader,但永远无法将 META-INF/services 放入我的 apk 中。所以我想知道是否可以在android上使用serviceloader 谢谢
编辑:我问的是 java.util.ServiceLoader,我认为应该如此,但我不知道如何将我的 services 文件夹放入 apk 上的 META-INF 中

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最单纯的乌龟 2024-11-09 19:41:14

有一个针对此问题的开放错误报告。请参阅https://code.google.com/p/android/issues/detail?id=59658

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

肤浅与狂妄 2024-11-09 19:41:14

ApkBuilder 故意将 META-INF 文件夹从 APK 中排除; ApkBuilder.java 中唯一的注释是“我们需要排除其他一些文件夹(如 /META-INF)”,但没有其他解释。

即使用ant添加了META-INF后,如果你想使用Proguard,你仍然会遇到麻烦,它拒绝替换META-INF/services/*文件的内容或重命名它们(这是另一个故事,作者想保留Proguard 不可知论)。

但是,使用 maven 的人可能需要检查 https://github.com/pa314159/maven-android-插件(名为“modified”的分支),试图解决这两个问题。它是我一个月前为自己的 Android 项目修改的原始“android-maven-plugin”的一个分支。

它还提供了 Proguard-4.7 的补丁

希望这有帮助,欢迎任何反馈。

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.

不必了 2024-11-09 19:41:14

我已经找到了一个可能适用于某些情况的解决方案。我使用 NetBeans 附带的 org.openide.util.Lookup 类/库,而不是 ServiceLoader - 它是 ServiceLoader 的超集>。它本身不需要 NetBeans,并且似乎可以与 Eclipse 一起使用。有必要将您在应用程序中使用的任何 ServiceLoader 功能替换为 Lookup 等效项,并添加 org-openide-util-lookup 库。然后,您可以执行以下操作:

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

并将您的 ServiceLoader 文件从 META-INF/services/ 移动到 services/。

请注意,由于 ProxyLookup,这将继续在标准 Java 环境中保持不变(即,在这些情况下,它将继续在 META-INF/services 中查找)。

以下是该库文档的链接: http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/lookup/Lookups.html

更新

使用此命令后几天后,它似乎运行良好 - 我在环境(标准 Java 和 Android)之间移动,它在每个位置都能正常运行。主要缺点是必须手动将文件复制到 /services 目录。

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.

执笔绘流年 2024-11-09 19:41:14

ServiceLoader 是来自 Java 语言的东西,与 Android 无关。我建议不要使用它。如果您只想在 .apk 中查找要加载的类列表,可以使用各种方法来实现此目的 - 将列出它们的 XMl 文件放入 res/xml 中,使用反射、注释等。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文