22 November 2014

How to use maven for Android


If you are Android developer you must used some development IDE(eclipse, idea) for developingandroid application.

But when you want to build the APK file you no need to use IDE. So Maven introduce the maven build plugin for android.

Here i have describe how to install maven(http://sajithforu.blogspot.com/2014/10/maven-installation-guide.html).

First you need android keystore for verify the apk file.So this will help you to how to create keystore in android.(http://developer.android.com/tools/publishing/app-signing.html).

First you have to install android sdk development kit in your machine.Then First set ANDROID_HOME path variable in your machine.



Second step genarate the keystore set KEYSTORE_HOME path variable in your PC.




Third create your android application using any IDE.

Inside the project create pom.xml file and put this code. and change the  App_name and relevant changes.

Make sure you have correct platform version



<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>App</groupId>
        <artifactId>modules</artifactId>
        <version>1.0.8</version>
    </parent>
 
    <groupId>App</groupId>
    <artifactId>App</artifactId>
    <version>1.0.8</version>
    <packaging>apk</packaging>
    <name>App-name</name>
 
    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${platform.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>

        <dependency>
            <groupId>org.roboguice</groupId>
            <artifactId>roboguice</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>App</finalName>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jarsigner-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                        <id>signing</id>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                        <phase>package</phase>
                        <inherited>true</inherited>
                        <configuration>
                            <includes>
                                <include>target/*.apk</include>
                            </includes>
                            <keystore>${env.KEYSTORE_HOME}/android-pos.keystore</keystore>
                            <storepass>keystorepassword</storepass>
                            <keypass>keypass</keypass>
                            <alias>your_alias</alias>
                            <verbose>true</verbose>
                            <arguments>
                                <argument>-sigalg</argument>
                                <argument>MD5withRSA</argument>
                                <argument>-digestalg</argument>
                                <argument>SHA1</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <release>true</release>
                    <sdk>
                        <platform>17</platform>
                        <path>${env.ANDROID_HOME}</path>
                    </sdk>
                    <sign>
                        <debug>false</debug>
                    </sign>
                    <extractDuplicates>true</extractDuplicates>
                    <proguard>
                        <skip>false</skip>
                        <config>proguard.cfg</config>
                        <configs>
                            <config>${env.ANDROID_HOME}/tools/proguard/proguard-android.txt</config>
                        </configs>
                        <filterMavenDescriptor>false</filterMavenDescriptor>
                        <filterManifest>false</filterManifest>
                        <jvmArguments>
                            <jvmArgument>-Xms256m</jvmArgument>
                            <jvmArgument>-Xmx512m</jvmArgument>
                        </jvmArguments>
                    </proguard>
                </configuration>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>maven-android-plugin</artifactId>
                <configuration>
                    <zipalign>
                        <verbose>true</verbose>
                        <skip>false</skip>            
                        <inputApk>${project.build.directory}/${project.artifactId}-${project.version}.apk</inputApk>
                        <outputApk>${project.build.directory}/${project.artifactId}-${project.version}-RELEASE.apk>
                        </outputApk>
                    </zipalign>
                    <manifest>
                        <debuggable>false</debuggable>
                        <versionCodeAutoIncrement>true</versionCodeAutoIncrement>
                    </manifest>
                    <proguard>
                        <skip>true</skip>
                    </proguard>
                </configuration>
                <executions>
                    <execution>
                        <id>zipalign</id>
                        <phase>install</phase>
                        <goals>
                            <goal>zipalign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <configuration>
                    <reportPlugins>
                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>findbugs-maven-plugin</artifactId>
                        </plugin>
                    </reportPlugins>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


Then go to the project and run these maven cvommands to compile and run the project.
         mvn clean install
         mvn android:compile
         mvn android:run

And that is all.Thanks

1 comment: