Android Maven 插件 apklib Mojo 在生成的 apklib 中不包含已编译的 R
我正在尝试将 Android /res
文件夹提取到一个单独的项目中,该项目作为 apklib 依赖项包含在我的主项目中。问题在于,虽然 /res
的内容包含在生成的 .apklib
中,但编译后的 R.class
却没有包含在内。更令人困惑的是,mvn clean install
命令生成.apklib
以及.jar
文件,并且jar文件有>R.class
,但没有 /res
文件夹中的任何内容。如何生成包含所有资源以及已编译类的单个包(.jar 或 .apklib)?
pom.xml
<packaging>apklib</packaging>
...
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<configuration>
<attachSources>true</attachSources>
<sdk>
<platform>12</platform>
</sdk>
</configuration>
<plugin>
生成以下
.jar
./morseflash-resources.jar
com/.../R.class
.apklib
./morseflash-resorces.apklib
META-INF
AndroidManifest.xml
res/
layout/
values/
我希望所有这些内容都在一个文件中,并且我想能够将其列为我的主 Android 项目中的依赖项。这可能吗?如果可以,我该如何实现?
I'm trying to extract the Android /res
folder into a separate project that's included in my main project as an apklib dependency. The problem is that while the contents of the /res
are included in the resulting .apklib
, the compiled R.class
is not. Even more confusing is that the mvn clean install
command generates the .apklib
as well as a .jar
file, and the jar file has R.class
, but none of the contents of the /res
folder. How do I generate a single package (either .jar or .apklib) that contains all of my resources as well as the compiled classes?
pom.xml
<packaging>apklib</packaging>
...
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<configuration>
<attachSources>true</attachSources>
<sdk>
<platform>12</platform>
</sdk>
</configuration>
<plugin>
Which generates the following
.jar
./morseflash-resources.jar
com/.../R.class
.apklib
./morseflash-resorces.apklib
META-INF
AndroidManifest.xml
res/
layout/
values/
I'd like all this content in a single file, and I'd like to be able to list it as a dependency in my main Android project. Is this possible, and if so, how could I accomplish it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是 android-maven-plugin 的工作方式,要将库项目添加为主项目的依赖项,请将以下依赖项添加到主项目的 pom.xml 中:
请注意,这与常规 jar 依赖项不同,即 apklib只是所有
src
和res
文件夹的 zip,它具有 Android/Eclipse 库项目的标准目录结构布局 (src/com/...
而不是src/main/java/com/...
)。这样做的原因是为了支持在其他非 mavenlized 项目中使用 apklib(查看 ApkLib 文档 了解详细信息)。您不是将库添加为已编译的 jar 依赖项,而是将库依赖项添加为源代码和资源(只需从 zip 文件中添加)。在执行
mvn clean install
时,在android-maven-plugin:3.0.0:generate-sources
目标中,android-maven-plugin 将解压您的 apklib 依赖项并合并在编译项目之前将源代码和资源放入主项目中;这就是 Android 库项目应该如何工作的。图书馆项目不仅仅是简单地编译所有内容。为了节省内存和磁盘空间,仅将使用的部分复制并编译到最终的 apk 中。您可以在主项目中轻松覆盖资源,因为它们只是在最终编译之前的合并步骤中替换库内容。它很丑陋,但这就是目前的工作方式,Android 开发团队目前正在研究这个问题,并将在未来支持 jar 依赖项,可能在 r17 中(我想这也需要 android-maven-plugin 更改/升级),更多详细信息请参阅其官方博客文章“更改到 Android SDK 工具中的库项目,r14 ”。
This is how android-maven-plugin is supposed to work, to add the library project as a dependency of your main project, add the following dependency into your main project's pom.xml:
Note that this is different from regular jar dependencies, the apklib is simply a zip of all your
src
andres
folders that has the standard directory structure layout of an Android/Eclipse library project (src/com/...
instead ofsrc/main/java/com/...
). The reason for that is to support using apklib in other non-mavenlized projects (check out the ApkLib documentation for details). You are not adding your library as a compiled jar dependency, instead, you are adding your library dependency as source-code and resources, simply from a zip file.When doing
mvn clean install
, in theandroid-maven-plugin:3.0.0:generate-sources
goal, android-maven-plugin will unzip your apklib dependency and merge the source-code and resources into your main project before compiling your project; this is how Android Library Projects are supposed to work. The Library Project is not just simply compiling everything. To save memory and disc-space only the used parts are copied and compiled into the final apk. You can override the resources easily in the main project, as they just replace the library stuff at the merge step before final compile.It's ugly but it is how things work at the moment, Android Dev team is currently working on this and will support the jar dependency in the future, probably in r17 (which also requires android-maven-plugin change/upgrade I suppose), more details in their official blog post "Changes to Library Projects in Android SDK Tools, r14 ".