Android Create Signed And Zipaligned APK File

09-04-2014

To create signed and zipaligned apk file with Maven, the necessary configurations are:

1. Firstly, you have to create a .keystore file:

keytool -genkey -v -keystore myCertificate.keystore -alias myKey -keyalg RSA -keysize 2048 -validity 20000
Note: myCertificate is the name of your .keystore file; and myKey is your alies. You can change these names.

keytool is an .exe file in the Java JDK bin folder. To run the above commands in the Command Prompt directly, we have to add JDK bin folder into Environment Settings. You can do this as follows:



2. Lastly, you should add the following codes into pom.xml file:

<profiles>
    <profile>
        <id>android-release</id>
        <properties>
            <sign.keystore>Url of .keystore file</sign.keystore>
            <sign.alias>Your Alis Name</sign.alias>
            <sign.storepass>Your Store Password</sign.storepass>
            <sign.keypass>Your Key Password</sign.keypass>
        </properties>
    </profile>
</profiles>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jarsigner-plugin</artifactId>
    <executions>
        <execution>
            <id>signing</id>
            <goals>
                <goal>sign</goal>
                <goal>verify</goal>
            </goals>
            <phase>package</phase>
            <inherited>true</inherited>
            <configuration>
                <removeExistingSignatures>true</removeExistingSignatures>
                <archiveDirectory/>
                <includes>
                    <include>${project.build.directory}/${project.artifactId}.apk</include>
                </includes>
                <keystore>${sign.keystore}</keystore>
                <alias>${sign.alias}</alias>
                <storepass>${sign.storepass}</storepass>
                <keypass>${sign.keypass}</keypass>
                <verbose>true</verbose>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
    <artifactId>android-maven-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <sign>
            <debug>false</debug>
        </sign>
        <zipalign>
            <verbose>true</verbose>
            <inputApk>${project.build.directory}/${project.artifactId}.apk</inputApk>
            <outputApk>${project.build.directory}/${project.artifactId}-signed-aligned.apk
            </outputApk>
        </zipalign>
        <device>usb</device>
        <run>
            <debug>true</debug>
        </run>
        <sdk>
            <path>C:\adt-bundle-windows-x86-20140321\sdk</path>
            <!--sdk dizini-->
            <platform>13</platform>
        </sdk>
 
        <undeployBeforeDeploy>true</undeployBeforeDeploy>
    </configuration>
    <executions>
        <execution>
            <id>alignApk</id>
            <phase>package</phase>
            <goals>
               <goal>zipalign</goal>
            </goals>
        </execution>
    </executions>
</plugin>

© 2019 All rights reserved. Codesenior.COM