java – maven support for android projects?-ThrowExceptions

Exception or error:

I want to start a project for Android but i really like to build it using Maven. Does Google provide support for Maven or plan to support it? It would be great if anybody know at least an archetype for Maven that I can use meanwhile. Thanks in advance.

How to solve:

Seems like there is a maven plugin for that 🙂

###

What I really wanted was an article like this, but i found it after the question was answered.

Update:
People at SpringSource did a Spring Android project that supports the usage of the Spring Framework in an Android environment and have Maven support. I will give it a try.

Here is an article about Spring Android and Maven using Eclipse 3.6 and Android SDK 9, split in two parts:

First part

Second part

###

The Android Maven plugin has been updated to support changes made in the Android SDK r14 release. You will need to adjust your POM to use the new version. I experienced an out of memory error with the new version when building my app, so note the dex jvmArguments section to allow for more available memory.

<plugin>
    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
    <artifactId>android-maven-plugin</artifactId>
    <version>3.0.0-alpha-13</version>
    <configuration>
        <sdk>
            <platform>${android-platform}</platform>
        </sdk>
        <dex>
            <jvmArguments>
                <jvmArgument>-Xms256m</jvmArgument>
                <jvmArgument>-Xmx512m</jvmArgument>
            </jvmArguments>
        </dex>
        <deleteConflictingFiles>true</deleteConflictingFiles>
        <undeployBeforeDeploy>true</undeployBeforeDeploy>
    </configuration>
    <extensions>true</extensions>
</plugin>

The latest version of the Android Configurator (m2e-android) for Eclipse also supports the changes in r14. Lastly, I’ve posted a follow up blog on the SpringSource site called Updated Maven Support for Android Projects, which goes over these updates to the tools.

###

Here is what I did to add maven support to an existing android project in Eclipse. I installed the ‘m2e’ plugin via the Eclipse market place. Then I installed the ‘m2e-android’ plugin. At the moment it is called ‘Android Configurator for M2E’ in Eclipse market place. After installing the two plugins and restarting Eclipse, right click on an existing android project–>Configure–>Convert to Maven Project. Choose a unique group id and an artifact id for your project then click finish. Copy the following contents to the pom.xml of the project and replace all the existing contents. Change the value of the ‘version’ tag under ‘dependencies’ to the SDK version you are using. Also change the value of the ‘platform’ tag near the end of the file to the value of your platform. Do not forget to also change the groupId, artifactId and name of the xml.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.myproject</groupId>
    <artifactId>MyProject</artifactId>
    <version>0.1.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>MyProject</name>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>2.3.3</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <sourceDirectory>src</sourceDirectory>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <version>3.3.0</version>
                    <extensions>true</extensions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <configuration>
                    <sdk>
                        <!-- platform or api level (api level 4 = platform 1.6)-->
                        <platform>10</platform>
                    </sdk>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

After that right click on the project–>Maven–>Update Project. If Eclipse is complaining about errors in the xml, you may want to install maven then run

mvn clean install

from the console in the folder that contains the pom.xml file.

###

as @Riduidel said before, you can use com.jayway.maven.plugins.android.generation2 plugin.
Note, that you don’t need download any plugins, you need to have just the maven for using this plugin.
How I did it:

  1. manually add pom.xml to your android project (to the root of project).

  2. download apache-maven-3.1.1 and add your bin folder ( ex D:\java\apache-maven-3.1.1\bin;) to path in Environment Variables.

  3. configure settings.xml in [Your_maven_path]\conf with next:

    <pluginGroups>  
    
         <pluginGroup>com.jayway.maven.plugins.android.generation2</pluginGroup>
    
    </pluginGroups> 
    
  4. Add content to pom.xml. My example:

        <?xml version="1.0" encoding="UTF-8"?>
        <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
            <modelVersion>4.0.0</modelVersion>
            <groupId>com.example.ENumbers</groupId>
            <artifactId>ENumbers</artifactId>
            <version>1.0</version>
            <packaging>apk</packaging>
            <name>MainApp</name>
    
            <properties>
                <platform.version>2.2.1</platform.version>
            </properties>
    
            <dependencies>
                <dependency>
                    <groupId>com.google.android</groupId>
                    <artifactId>android</artifactId>
                    <version>${platform.version}</version>
                    <scope>provided</scope>
                </dependency>
                <dependency>
                    <groupId>org.simpleframework</groupId>
                    <artifactId>simple-xml</artifactId>
                    <version>2.7.1</version>
                    <scope>provided</scope>
                <exclusions>
                    <exclusion>
                        <artifactId>xpp3</artifactId>
                        <groupId>xpp3</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>stax-api</artifactId>
                        <groupId>stax</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>stax</artifactId>
                        <groupId>stax</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>spring-web</artifactId>
                        <groupId>org.springframework</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>commons-logging</artifactId>
                        <groupId>commons-logging</groupId>
                    </exclusion>
                   </exclusions>
                </dependency>
            </dependencies>
    
            <build>
                <sourceDirectory>src</sourceDirectory>
                <plugins>
                    <plugin>
                        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                        <artifactId>android-maven-plugin</artifactId>
                        <version>3.8.1</version>
                        <configuration>
                            <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
                            <assetsDirectory>${project.basedir}/assets</assetsDirectory>
                            <resourceDirectory>${project.basedir}/res</resourceDirectory>
                            <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
                            <sdk>
                                <path>
                                    D:\Program Files\Android\android-sdk
                                </path>
                                <platform>22</platform>
                            </sdk>
                            <undeployBeforeDeploy>true</undeployBeforeDeploy>
                        </configuration>
                        <extensions>true</extensions>
                    </plugin>
    
                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>2.3.2</version>
                        <configuration>
                            <source>1.6</source>
                            <target>1.6</target>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </project>
    

Look at important <packaging>, <build> nodes, it’s content and com.google.android dependency.

  1. Now you can open Maven window in your IDE. For Intellij Idea I do it next:
    Edit->Tool Windows->Maven and add your pom.xml for initializing maven directory.
  2. That’s all.

Leave a Reply

Your email address will not be published. Required fields are marked *