Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Maven Plugin apklib Mojo doesn't include compiled R in resulting apklib

Tags:

android

maven

apk

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?

like image 802
Fil Avatar asked Jan 11 '12 17:01

Fil


1 Answers

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>

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.

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.

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 ".

like image 131
yorkw Avatar answered Sep 27 '22 18:09

yorkw