Domanda

So I am using Android Maven Plugin to generate apklib snapshot for one of my library project. But the library project is also self sufficient to run on its own so I am thinking that may be we should generate .apklib and .apk both at the same time using maven.

Here is the pom configuration.

<groupId>com.comanyname.apps.lib</groupId>
<artifactId>MyProject-lib</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>apklib</packaging>
<name>MyProject-lib</name>

I tried adding another packaging like this

<groupId>com.comanyname.apps.lib</groupId>
<artifactId>MyProject-lib</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>apklib</packaging>
<packaging>apk</packaging>
<name>MyProject-lib</name>

I don't know if it is possible. I can switch the packaging to apk/apklib one at a time though.

È stato utile?

Soluzione

So I was able to create apk and apklib by creating two profiles.

<profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <projectVersion>1.0.0-SNAPSHOT</projectVersion>
                <packagingType>apk</packagingType>
            </properties>
        </profile>
        <profile>
            <id>build</id>
            <properties>
                <projectVersion>1.0.0-SNAPSHOT</projectVersion>
                <packagingType>apklib</packagingType>
            </properties>
        </profile>
</profiles>

Altri suggerimenti

The recommended approach is to create two projects:

  1. Project A is a library project (packagingType of apklib). It contains all the code, resources and assets.
  2. Project B is a standard Android project (packagingType of apklib), that depends on Project A. It contains no code, resources or assets (although you can add some if you want to).
  3. Optionally, you can have a parent project with Project A & B as modules. Then whenever you build the parent project, both the apk and apklib are built.

I use this approach with my own projects, and it works very well. I also add a test project as a third module, which depends on the apklib.

I added simplified samples of the project structure on https://gist.github.com/4224133.

"But the Library project is also self sufficient to run on its own."

This is not possible, at least not support by official Android SDK, check out the dev guide to see what is an Android library project:

However, a library project differs from an standard Android application project in that you cannot compile it directly to its own .apk and run it on an Android device. Similarly, you cannot export the library project to a self-contained JAR file, as you would do for a true library. Instead, you must compile the library indirectly, by referencing the library in the dependent application and building that application.

"I can switch the packaging to apk/apklib one at a time though."

This is probably doable by android-maven-plugin, as Android library project has similar project structure to a standard Android application project, stated in dev guide:

Structurally, a library project is similar to a standard Android application project. For example, it includes a manifest file at the project root, as well as src/, res/ and similar directories. The project can contain the same types of source code and resources as a standard Android project, stored in the same way. For example, source code in the library project can access its own resources through its R class.

I haven't tested it by myself, if it is true, I would consider this is as a bug in android-maven-plugin as it dose not respect android.library=true set in project.properties file. If it is a library project, android-maven-plugin should give error or at least a warning when you are trying to build it using <packaging>apk</packaging>.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top