【问题标题】:how to include resource file in distributed jar file?如何在分布式jar文件中包含资源文件?
【发布时间】:2013-04-25 17:05:40
【问题描述】:

我进行了搜索,但没有看到任何答案。

我正在构建一个 jar 作为 SDK 用于分发。我在构建路径中将其标记为“is library”后,jar 文件构建成功。

现在的挑战是还包括我的 js 库 (js.zip) 以进行分发。我看到将 js.zip 放在 PROJECT_ROOT/res/raw 下后,它已包含在我的 .APK 文件中。但我没有通过“jar tf my.jar”在 jar 文件中看到它。如果将我的文件放在资产文件夹中,则不会产生差异。

这可能吗?

谢谢,

【问题讨论】:

标签: android eclipse jar resources


【解决方案1】:

在构建路径中将 jar 文件标记为“is library”后,jar 文件构建成功

该 JAR 不是为重新分发而设计的。它是在宿主项目中使用 Android 库项目的工件。

这可能吗?

在撰写本文时,Android 并未在 JAR 中分发 Android 资源。 也许这将在当前正在开发的基于 Gradle 的构建系统中提供。

您将需要分发一个包含您的资源的 Android 库项目,其中还包含您的 src/ 树或在 libs/ 中为该代码手动构建的 JAR。

例如,这里有一对相当粗糙的 Ant 任务:

<target name="jar" depends="debug">
    <delete file="bin/CWAC-ColorMixer.jar" />
    <jar destfile="bin/CWAC-ColorMixer.jar" basedir="bin/classes" excludes="**/BuildConfig.class **/R*.class" />
</target>

<target name="dist-lib" depends="jar">
    <copy todir="/tmp/CWAC-ColorMixer/libs">
        <fileset dir="libs/" />
    </copy>
    <copy todir="/tmp/CWAC-ColorMixer/res">
        <fileset dir="res/" />
    </copy>
    <copy file="bin/CWAC-ColorMixer.jar" todir="/tmp/CWAC-ColorMixer/libs" />
    <copy file="AndroidManifest.xml" todir="/tmp/CWAC-ColorMixer" />
    <copy file="build.xml" todir="/tmp/CWAC-ColorMixer" />
    <copy file="local.properties" todir="/tmp/CWAC-ColorMixer" />
    <copy file="project.properties" todir="/tmp/CWAC-ColorMixer" />
    <copy file="LICENSE" todir="/tmp/CWAC-ColorMixer" />
    <mkdir dir="/tmp/CWAC-ColorMixer/src" />
</target>

第一个创建 JAR 文件。第二个在临时目录中创建一个 Android 库项目。尤其是第二个 Ant 任务确实需要改进(例如,可配置的位置、自动压缩结果),但它确实有效。

【讨论】:

  • 感谢您的精彩书籍和您的意见。 “您将需要分发一个包含您的资源的 Android 库项目,其中还包含您的 src/树” - 怎么做? " 或在 libs/ 中为该代码手动构建的 JAR。"如何手动构建 JAR 文件?使用 AAPT?
  • 这篇文章怎么样?似乎它是可行的。 link
  • @user443461:“怎么做?” ——见编辑。 “如何手动构建 JAR 文件?” ——就像过去 15 年左右人们在 Java 中所做的一样。 “这个帖子怎么样?” -- 除了不工作的胡言乱语?
  • 感谢@commonsware。(@ 只是不工作)。感谢您提供详细信息!
猜你喜欢
  • 1970-01-01
  • 2011-01-22
  • 2015-06-12
  • 1970-01-01
  • 2014-08-26
  • 2018-04-16
  • 1970-01-01
  • 2018-04-26
  • 1970-01-01
相关资源
最近更新 更多