Android - 是否可以创建一个自定义库以在多个应用程序中使用?
是否可以在 Android 中创建自定义库(具有自己的布局资源)以供多个 Android 应用程序使用?
我创建了一个常规的 *.jar 文件,但是当我尝试动态创建/设计视图时,大多数属性都不起作用。即使从 android.jar 文件引用简单样式(例如 android.attr.listSeparatorTextViewStyle)也不起作用。
我创建了一个没有任何 Activity 的 Android 项目,拥有自己的资源文件,然后从另一个 Android 项目引用该项目以在其构建路径中使用。一切似乎都工作正常,但当我尝试运行该项目时,模拟器不断崩溃(LogCat 中没有有意义的错误消息)。
我错过了什么吗?
Is it possible to create a custom library in Android (having its own layout resources) for use across several Android applications?
I created a regular *.jar file but when I tried to create/style my views dynamically most of the properties do not work. Even referencing simple styles from the android.jar file such as android.attr.listSeparatorTextViewStyle did not work.
I created an Android project without any Activity, having its own resource files and then referenced this project from another Android project to use in its build path. Everything seems to work fine but the emulator keeps crashing (with no meaningful error message in the LogCat) when I try to run the project.
Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能回复晚了,但发现这很有趣。
设置库项目
Might be late to reply but found this which is interesting.
Setting up a Library Project
好吧,我认为有一种方法可以做到这一点(尽管我自己没有尝试过),但它要求您的库的用户必须通过
aapt
手动构建他们的 APK。问题是这样的:您导出的 JAR 包含
R
类文件,该文件保存您的库的资源 ID。因此,任何应用程序都可以通过简单地链接您的 JAR 来访问这些 ID。然而,这还不够,因为默认情况下,只有应用程序的 res/ 文件夹中的资源与 APK 捆绑在一起。因此,您必须使用 aapt 自己创建 APK,并告诉它也包含您库中的所有资源。检查 aapt 工具帮助以获取更多信息。由于这意味着您的库的用户必须引入手动构建过程(或至少修改它),因此体验很差。
Well, I think there is a way to do it (although I didn't try it myself), but it requires the users of your library having to build their APKs manually via
aapt
.The problem is this: The JAR you're exporting contains the
R
class file which holds the resource IDs of your library. So, any application can access these IDs by simply linking your JAR. However, that's not enough, because by default, only resources in the app's res/ folder are bundled with the APK. Hence, you have to create the APK yourself using aapt, telling it to also include any resources from your library. Check the aapt tool help for more information.Since this implies that users of your library have to introduce a manual build process (or at least modify it), it makes for a poor experience.
不可以。JAR 不能拥有资源。您可以通过这种方式共享代码,但资源必须驻留在重复使用 JAR 的应用程序中。
确保您将 JAR 链接到 android.jar。您可以在我的 github 页面 上的 CWAC 组件中看到许多这样的示例。
No. JARs cannot have resources. You can share code this way, but resources have to reside in the applications reusing the JARs.
Make sure you are linking your JAR against android.jar. You can see many examples of this in the CWAC components on my github page.
R21 仍然如此吗?我能够将库项目导出到 jar 文件并成功地在其他客户项目中使用,无论是代码方面还是访问资产文件方面。
然而 http://developer.android.com/tools/projects/index.html 仍然有以下声明,但似乎不再正确:
Is this still true with R21? I am able to export a library project into jar file and used in other customer projects successfully, both code wise and accessing assets file wise.
yet http://developer.android.com/tools/projects/index.html still has the following statement which seems not true anymore: