Mastering Android NDK
上QQ阅读APP看书,第一时间看更新

Building and signing release Android applications

We learned how to use the command line to create Android applications with the native code. Let's put the final stroke on the topic of the command-line tools and learn how to prepare and sign the release version of your application.

The detailed explanation of the signing procedure on Android is given in the developer manual at http://developer.android.com/tools/publishing/app-signing.html. Let's do it using Ant and Gradle.

First of all, we need to rebuild the project and create a release version of the .apk package. Let's do it with our 3_NDK project. We invoke ndk-build and Apache Ant using the following commands:

>ndk-build
>ant release

The tail of the output from Ant looks as follows:

-release-nosign:
[echo] No key.store and key.alias properties found in build.properties.
[echo] Please sign F:\Book_MasteringNDK\Sources\Chapter1\3_NDK\bin\App1-release-unsigned.apk manually
[echo] and run zipalign from the Android SDK tools.
[propertyfile] Updating property file: F:\Book_MasteringNDK\Sources\Chapter1\3_NDK\bin\build.prop
[propertyfile] Updating property file: F:\Book_MasteringNDK\Sources\Chapter1\3_NDK\bin\build.prop
[propertyfile] Updating property file: F:\Book_MasteringNDK\Sources\Chapter1\3_NDK\bin\build.prop
[propertyfile] Updating property file: F:\Book_MasteringNDK\Sources\Chapter1\3_NDK\bin\build.prop
-release-sign:
-post-build:
release:
BUILD SUCCESSFUL
Total time: 2 seconds

Let's do the same thing with Gradle. Maybe you have already noticed when we run gradle build there is a 3_NDK-release-unsigned.apk file in the build/outputs/apk folder. This is exactly what we need. This will be our raw material for the signing procedure.

Now, we need to have a valid release key. We can create a self-signed release key using keytool from the Java Development Kit using the following command:

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

This will ask us to fill out all the fields necessary for the key:

Enter keystore password:
Re-enter new password:
What is your first and last name?
 [Unknown]: Sergey Kosarevsky
What is the name of your organizational unit?
 [Unknown]: SD
What is the name of your organization?
 [Unknown]: Linderdaum
What is the name of your City or Locality?
 [Unknown]: St.Petersburg
What is the name of your State or Province?
 [Unknown]: Kolpino
What is the two-letter country code for this unit?
 [Unknown]: RU
Is CN=Sergey Kosarevsky, OU=SD, O=Linderdaum, L=St.Petersburg, ST=Kolpino, C=RU correct?
 [no]: yes
Generating 2048 bit RSA key pair and self-signed certificate (SHA1withRSA) with a validity of 10000 days
for: CN=Sergey Kosarevsky, OU=SD, O=Linderdaum, L=St.Petersburg, ST=Kolpino, C=RU
Enter key password for <alias_name>
 (RETURN if same as keystore password):
[Storing my-release-key.keystore]

Now, we are ready to proceed with the actual .apk package signing. Use the jarsigner tool from the Java Development Kit to do this:

>jarsigner -verbose -sigalg MD5withRSA -digestalg SHA1 -keystore my-release-key.keystore 3_NDK-release-unsigned.apk alias_name

This command is interactive, and it will require the user to enter the keystore and the key passwords. However, we can provide both passwords as arguments to this command in the following way:

>jarsigner -verbose -sigalg MD5withRSA -digestalg SHA1 -keystore my-release-key.keystore -storepass 123456 –keypass 123456 3_NDK-release-unsigned.apk alias_name

Of course, passwords should match with what you have entered while creating your release key and keystore.

There is one more important thing left before we can safely proceed with publishing our .apk package on Google Play. Android applications can access uncompressed content within .apk using memory-mapped files and mmap() system calls, yet mmap() may imply some alignment restrictions on the underlying data. We need to align all uncompressed data within .apk on 4-byte boundaries. The Android SDK has the zipalign tool to do this, as seen in the following command:

>zipalign -v 4 3_NDK-release-unsigned.apk 3_NDK-release.apk

Now, our .apk is ready to be published on Google Play.