Android Maven 插件 apklib Mojo 在生成的 apklib 中不包含已编译的 R [英] Android Maven Plugin apklib Mojo doesn't include compiled R in resulting apklib
问题描述
我正在尝试将 Android /res
文件夹提取到一个单独的项目中,该项目作为 apklib 依赖项包含在我的主项目中.问题是,虽然 /res
的内容包含在结果 .apklib
中,但编译后的 R.class
没有.更令人困惑的是,mvn clean install
命令会生成.apklib
以及.jar
文件,而jar 文件有R.class
,但 /res
文件夹中没有任何内容.如何生成包含我的所有资源以及已编译类的单个包(.jar 或 .apklib)?
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>
生成以下内容
.jar
./morseflash-resources.jar
com/.../R.class
.apklib
./morseflash-resorces.apklib
META-INF
AndroidManifest.xml
res/
layout/
values/
我希望在一个文件中包含所有这些内容,并且我希望能够将其列为我的主要 Android 项目中的依赖项.这可能吗,如果可以,我该如何实现?
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?
推荐答案
这就是 android-maven-plugin 应该如何工作,要将库项目添加为您的主项目的依赖项,将以下依赖项添加到您的主项目中项目的 pom.xml:
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:
<dependencies>
<dependency>
<groupId>com.company.common.lib</groupId>
<artifactId>common-lib</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>apklib</type>
</dependency>
</dependencies>
请注意,这与常规 jar 依赖项不同,apklib 只是您所有 src
和 res
文件夹的 zip 文件,具有 Android 的标准目录结构布局/Eclipse 库项目(src/com/...
而不是 src/main/java/com/...
).这样做的原因是支持在其他非 mavenlized 项目中使用 apklib(查看 ApkLib 文档 了解详情).您不是将库添加为已编译的 jar 依赖项,而是将库依赖项添加为源代码和资源,只需从 zip 文件中即可.
Note that this is different from regular jar dependencies, the apklib is simply a zip of all your src
and res
folders that has the standard directory structure layout of an Android/Eclipse library project (src/com/...
instead of src/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.
在执行 mvn clean install
时,在 android-maven-plugin:3.0.0:generate-sources
目标中,android-maven-plugin 将解压您的 apklib 依赖项并在编译项目之前将源代码和资源合并到您的主项目中;这就是 Android 库项目应该如何工作.图书馆计划不仅仅是简单地编译所有内容.为了节省内存和磁盘空间,只有使用过的部分被复制并编译到最终的 apk 中.您可以轻松地覆盖主项目中的资源,因为它们只是在最终编译之前的合并步骤中替换库内容.
When doing mvn clean install
, in the android-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.
这很丑陋,但目前它是如何工作的,Android 开发团队目前正在处理这个问题,并将在未来支持 jar 依赖项,可能在 r17 中(这也需要 android-maven-plugin 更改/升级我想),更多细节在他们的官方博客文章Android SDK 工具中库项目的更改,r14 ".
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 ".
这篇关于Android Maven 插件 apklib Mojo 在生成的 apklib 中不包含已编译的 R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!