Android - 是否可以创建一个自定义库以在多个应用程序中使用?

发布于 2024-08-13 02:44:36 字数 397 浏览 7 评论 0原文

是否可以在 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 技术交流群。

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

发布评论

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

评论(4

苦笑流年记忆 2024-08-20 02:44:36

可能回复晚了,但发现这很有趣。
设置库项目

Android 库项目是
共享开发项目
Android 源代码和资源。
其他Android应用程序项目可以
参考图书馆项目,并在
构建时间,包括其编译时间
其 .apk 文件中的源代码。多种的
应用项目可以参考
相同的图书馆项目和任何单个
应用项目可参考
多个图书馆项目。

如果您有源代码和资源
是多个共有的
应用项目,可以移动
他们到一个图书馆项目,以便
更容易维护
应用程序和版本。这里有
一些常见的场景,您
可以利用图书馆项目:

Might be late to reply but found this which is interesting.
Setting up a Library Project

An Android library project is a
development project that holds shared
Android source code and resources.
Other Android application projects can
reference the library project and, at
build time, include its compiled
sources in their .apk files. Multiple
application projects can reference the
same library project and any single
application project can reference
multiple library projects.

If you have source code and resources
that are common to multiple
application projects, you can move
them to a library project so that it
is easier to maintain across
applications and versions. Here are
some common scenarios in which you
could make use of library projects:

脸赞 2024-08-20 02:44:36

好吧,我认为有一种方法可以做到这一点(尽管我自己没有尝试过),但它要求您的库的用户必须通过 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.

时光与爱终年不遇 2024-08-20 02:44:36

是否可以创建自定义
android 中的库(有自己的
布局资源)以供跨用途使用
几个 Android 应用程序?

不可以。JAR 不能拥有资源。您可以通过这种方式共享代码,但资源必须驻留在重复使用 JAR 的应用程序中。

甚至引用简单的样式
android.jar 文件,例如
android.attr.listSeparatorTextViewStyle
没有工作。

确保您将 JAR 链接到 android.jar。您可以在我的 github 页面 上的 CWAC 组件中看到许多这样的示例。

Is it possible to create a custom
library in android (having its own
layout resources) for use across
several android applications?

No. JARs cannot have resources. You can share code this way, but resources have to reside in the applications reusing the JARs.

Even referencing simple styles from
the android.jar file such as
android.attr.listSeparatorTextViewStyle
did not work.

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.

南城旧梦 2024-08-20 02:44:36

R21 仍然如此吗?我能够将库项目导出到 jar 文件并成功地在其他客户项目中使用,无论是代码方面还是访问资产文件方面。

然而 http://developer.android.com/tools/projects/index.html 仍然有以下声明,但似乎不再正确:

您无法将库项目导出到 JAR 文件 库不能
作为二进制文件(例如 JAR 文件)分发。这将被添加
在 SDK 工具的未来版本中。

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:

You cannot export a library project to a JAR file A library cannot be
distributed as a binary file (such as a JAR file). This will be added
in a future version of the SDK Tools.

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