10 min read

In this article written by Patroklos Papapetrou and Jonathan LALOU, authors of the book Android Application Development with Maven, we’ll learn different modes of digital signing and also using Maven to digitally sign the applications. The topics that we will explore in this article are:

(For more resources related to this topic, see here.)

Signing an application

Android requires that all packages, in order to be valid for installation in devices, need to be digitally signed with a certificate. This certificate is used by the Android ecosystem to validate the author of the application. Thankfully, the certificate is not required to be issued by a certificate authority. It would be a total nightmare for every Android developer and it would increase the cost of developing applications. However, if you want to sign the certificate by a trusted authority like the majority of the certificates used in web servers, you are free to do it.

Android supports two modes of signing: debug and release. Debug mode is used by default during the development of the application, and the release mode when we are ready to release and publish it. In debug mode, when building and packaging an application the Android SDK automatically generates a certificate and signs the package. So don’t worry; even though we haven’t told Maven to do anything about signing, Android knows what to do and behind the scenes signs the package with the autogenerated key.

When it comes to distributing an application, debug mode is not enough; so, we need to prepare our own self-signed certificate and instruct Maven to use it instead of the default one. Before we dive to Maven configuration, let us quickly remind you how to issue your own certificate. Open a command window, and type the following command:

keytool -genkey -v -keystore my-android-release-key.keystore -alias my-android-key -keyalg RSA -keysize 2048 -validity 10000

If the keytool command line utility is not in your path, then it’s a good idea to add it. It’s located under the %JAVA_HOME%/bin directory. Alternatively, you can execute the command inside this directory. Let us explain some parameters of this command. We use the keytool command line utility to create a new keystore file under the name my-android-release-key.keystore inside the current directory. The -alias parameter is used to define an alias name for this key, and it will be used later on in Maven configuration. We also specify the algorithm, RSA, and the key size, 2048; finally we set the validity period in days. The generated key will be valid for 10,000 days—long enough for many many new versions of our application!

After running the command, you will be prompted to answer a series of questions. First, type twice a password for the keystore file. It’s a good idea to note it down because we will use it again in our Maven configuration. Type the word :secret in both prompts. Then, we need to provide some identification data, like name, surname, organization details, and location. Finally, we need to set a password for the key. If we want to keep the same password with the keystore file, we can just hit RETURN.

If everything goes well, we will see the final message that informs us that the key is being stored in the keystore file with the name we just defined. After this, our key is ready to be used to sign our Android application.

The key used in debug mode can be found in this file: ~/.android.debug.keystore and contains the following information:

Keystore name: "debug.keystore"
Keystore password: "android"
Key alias: "androiddebugkey"
Key password: "android"
CN: "CN=Android Debug,O=Android,C=US"

Now, it’s time to let Maven use the key we just generated. Before we add the necessary configuration to our pom.xml file, we need to add a Maven profile to the global Maven settings. The profiles defined in the user settings.xml file can be used by all Maven projects in the same machine. This file is usually located under this folder: %M2_HOME%/conf/settings.xml. One fundamental advantage of defining global profiles in user’s Maven settings is that this configuration is not shared in the pom.xml file to all developers that work on the application. The settings.xml file should never be kept under the Source Control Management (SCM) tool. Users can safely enter personal or critical information like passwords and keys, which is exactly the case of our example.

Now, edit the settings.xml file and add the following lines inside the <profiles> attribute:

<profile>
<id>release</id>
<properties>
   <sign.keystore>/path/to/my/keystore/my-android-release- 
    key.keystore</sign.keystore>
   <sign.alias>my-android-key</sign.alias>
   <sign.storepass>secret</sign.storepass>
   <sign.keypass>secret</sign.keypass>
</properties>
</profile>

Keep in mind that the keystore name, the alias name, the keystore password, and the key password should be the ones we used when we created the keystore file.

Clearly, storing passwords in plain text, even in a file that is normally protected from other users, is not a very good practice. A quick way to make it slightly less easy to read the password is to use XML entities to write the value. Some sites on the internet like this one http://coderstoolbox.net/string/#!encoding=xml&action=encode&charset=none provide such encryptions.

It will be resolved as plain text when the file is loaded; so Maven won’t even notice it. In this case, this would become:

<sign.storepass>&#115;&#101;&#99;&#114;&#101;&#116;</sign.storepass>

We have prepared our global profile and the corresponding properties, and so we can now edit the pom.xml file of the parent project and do the proper configuration. Adding common configuration in the parent file for all Maven submodules is a good practice in our case because at some point, we would like to release both free and paid versions, and it’s preferable to avoid duplicating the same configuration in two files.

We want to create a new profile and add all the necessary settings there, because the release process is not something that runs every day during the development phase. It should run only at a final stage, when we are ready to deploy our application. Our first priority is to tell Maven to disable debug mode. Then, we need to specify a new Maven plugin name: maven-jarsigner-plugin, which is responsible for driving the verification and signing process for custom/private certificates. You can find the complete release profile as follows:

<profiles>
<profile>
   <id>release</id>
   <build>
     <plugins>
       <plugin>
         <groupId>com.jayway.maven.plugins.android.generation2 
          </groupId>
         <artifactId>android-maven-plugin</artifactId>
         <extensions>true</extensions>
         <configuration>
           <sdk>
             <platform>19</platform>
           </sdk>
           <sign>
             <debug>false</debug>
           </sign>
         </configuration>
       </plugin>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jarsigner-plugin</artifactId>
         <executions>
           <execution>
             <id>signing</id>
             <phase>package</phase>
             <goals>
               <goal>sign</goal>
               <goal>verify</goal>
             </goals>
             <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>
     </plugins>
   </build>
</profile>
</profiles>

We instruct the JAR signer plugin to be triggered during the package phase and run the goals of verification and signing. Furthermore, we tell the plugin to remove any existing signatures from the package and use the variable values we have defined in our global profile, $sign.alias, $sign.keystore, $sign.storepass and $sign.keypass. The “verbose” setting is used here to verify that the private key is used instead of the debug key.

Before we run our new profile, for comparison purposes, let’s package our application without using the signing capability. Open a terminal window, and type the following Maven command:

mvn clean package

When the command finishes, navigate to the paid version target directory, /PaidVersion/target, and take a look at its contents. You will notice that there are two packaging files: a PaidVersion.jar (size 14KB) and PaidVersion.apk (size 46KB).

Since we haven’t discussed yet about releasing an application, we can just run the following command in a terminal window and see how the private key is used for signing the package:

mvn clean package -Prelease

You must have probably noticed that we use only one profile name, and that is the beauty of Maven. Profiles with the same ID are merged together, and so it’s easier to understand and maintain the build scripts.

If you want to double-check that the package is signed with your private certificate, you can monitor the Maven output, and at some point you will see something similar to the following image:

Android Application Development with Maven

This output verifies that the classes have been properly signed through the execution of the Maven JAR signer plugin.

To better understand how signing and optimization affects the packages generation, we can navigate again to the /PaidVersion/target directory and take a look at the files created. You will be surprised to see that the same packages exist again but they have different sizes. The PaidVersion.jar file has a size of 18KB, which is greater than the file generated without signing. However, the PaidVersion.apk is smaller (size 44KB) than the first version. These differences happen because the .jar file is signed with the new certificate; so the size is getting slightly bigger, but what about the .apk file. Should be also bigger because every file is signed with the certificate.

The answer can be easily found if we open both the .apk files and compare them. They are compressed files so any well-known tool that opens compressed files can do this. If you take a closer look at the contents of the .apk files, you will notice that the contents of the .apk file that was generated using the private certificate are slightly larger except the resources.arsc file. This file, in the case of custom signing, is compressed, whereas in the debug signing mode it is in raw format. This explains why the signed version of the .apk file is smaller than the original one.

There’s also one last thing that verifies the correct completion of signing. Keep the compressed .apk files opened and navigate to the META-INF directory. These directories contain a couple of different files.

The signed package with our personal certificate contains the key files named with the alias we used when we created the certificate and the package signed in debug mode contains the default certificate used by Android.

Summary

We know that Android developers struggle when it comes to proper package and release of an application to the public. We have analyzed in many details the necessary steps for a correct and complete packaging of Maven configuration.

After reading this article, you should have basic knowledge of digitally signing packages with and without the help of Mavin in Android.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here