Publishing a NativeScript Android App in Google Play

[Fuente: https://developer.android.com/studio/publish/app-signing#signing-manually]

Compilar y firmar tu aplicación desde la línea de comandos

No necesitas Android Studio para firmar tu aplicación. Puedes hacerlo desde la línea de comandos con la herramienta apksigner, o bien configurar Gradle para que se encargue de ello durante la compilación. En cualquier caso, debes generar primero una clave privada usando keytool. Por ejemplo:

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

Nota: keytool se encuentra en el directorio bin/ de tu JDK. Para encontrar tu JDK desde Android Studio, selecciona File > Project Structure y luego haz clic en SDK Location. Con esto, verás la ubicación del JDK.

En este ejemplo se te solicitan contraseñas para el keystore y la clave, y para otorgar los campos de nombre distinguido de tu clave. Luego, se genera el keystore como un archivo llamado my-release-key.jks, que se guarda en el directorio actual (puedes moverlo cuando lo desees). El keystore contiene una clave única válida por 10 000 días.

Ahora puedes compilar un APK sin firma y firmarlo manualmente o configurar Gradle para que firme tu APK.

Compilar un APK sin firma y firmarlo manualmente

  1. Abre una línea de comandos, dirígete a la raíz del directorio de tu proyecto, desde Android Studio, y selecciona View > Tool Windows > Terminal. Luego invoca la tarea assembleRelease:
    gradlew assembleRelease

    De esta forma, se creará un APK con el nombre module_name-unsigned.apk en project_name/module_name/build/outputs/apk/. En este punto, el APK no está firmado ni alineado; no puede instalarse hasta que se firme con tu clave privada.

  2. Alinea el APK sin firma usando zipalign:
    zipalign -v -p 4 my-app-unsigned.apk my-app-unsigned-aligned.apk
    

    zipalign garantiza que todos los datos descomprimidos comiencen con una alineación de bytes en particular relacionada con el comienzo del archivo, lo que reduce el consumo de memoria RAM de una aplicación.

  3. Firma tu APK con tu clave privada usando apksigner:
    apksigner sign --ks my-release-key.jks --out my-app-release.apk my-app-unsigned-aligned.apk
    

    En este ejemplo se genera el APK firmado en my-app-release.apk una vez que se firma con una clave privada y un certificado, que se almacenan en un único archivo de keystore: my-release-key.jks.

    La herramienta apksigner admite otras opciones de firmas, incluidas la firma de un archivo APK con archivos de clave y certificado privados separados y la firma de un APK con varios firmantes. Para obtener información detallada, consulta la referencia de apksigner.

    Nota: Para usar la herramienta apksigner, debes tener instalada la revisión 24.0.3 o revisiones posteriores de Android SDK Build Tools. Puedes actualizar este paquete con SDK Manager.

  4. Verifica que tu APK esté firmado.
    apksigner verify my-app-release.apk
    

Configurar Gradle para que firme tu APK

  1. Abre el archivo build.gradle de nivel de módulo y agrega el bloque signingConfigs {} con entradas para storeFilestorePasswordkeyAlias y keyPassword, y luego pasa ese objeto a la propiedad signingConfig en tu tipo de compilación. Por ejemplo:
    android {
        ...
        defaultConfig { ... }
        signingConfigs {
            release {
                storeFile file("my-release-key.jks")
                storePassword "password"
                keyAlias "my-alias"
                keyPassword "password"
            }
        }
        buildTypes {
            release {
                signingConfig signingConfigs.release
                ...
            }
        }
    }

    Debido a que Gradle lee las rutas de acceso relativas a build.gradle, el ejemplo anterior funciona únicamente si my-release-key.jks está en el mismo directorio que el archivo build.gradle.

    Nota: En este caso, el keystore y la contraseña de clave pueden verse directamente en el archivo build.gradle. Para una mayor seguridad, debes quitar la información de firmas de tu archivo de compilación.

  2. Abre una línea de comandos en el directorio raíz de tu proyecto e invoca la tarea assembleRelease:
    gradlew assembleRelease

De esta forma, se creará un APK con el nombre module_name-release.apk en project_name/module_name/build/outputs/apk/. Este archivo APK se firma con la clave privada especificada en tu archivo build.gradle y se alinea con zipalign.

Ahora que ya configuraste la compilación de lanzamiento con tu clave de firma, la tarea “install” estará disponible para ese tipo de compilación. Por lo tanto, podrás compilar, alinear e instalar el APK de lanzamiento en un emulador o dispositivo con la tarea installRelease.

Un APK firmado con tu clave privada está listo para la distribución, pero antes debes leer más información sobre cómo publicar tu aplicación y revisar la lista de comprobación de lanzamiento para Google Play.

[Fuente: https://docs.nativescript.org/tooling/publishing/publishing-android-apps]

Overview

Tip: Instead of the CLI, use NativeScript Sidekick for easy app store publishing to both the iOS App Store and Google Play. Sidekick also offers starter kits, plugin management, and cloud-based builds for iOS and Android.

You can publish a NativeScript app in Google Play the same way you would release a purely native Android app.

  1. Make sure that you have a .keystore file to sign your app with. For more information, see How to create a .keystore file?
  2. Build your project in release mode by running the following command:
    tns build android --release --key-store-path <path-to-your-keystore> --key-store-password <your-key-store-password> --key-store-alias <your-alias-name> --key-store-alias-password <your-alias-password>
    

    Note: At the end of <path-to-your-keystore> you should also add the exact name of your keystore.

Example(Windows): tns build android --release --key-store-path C:\keystore\Telerik.keystore --key-store-password sample_password --key-store-alias Telerik --key-store-alias-password sample_password .

Example(Mac): tns build android --release --key-store-path ~/Desktop/keystore/Telerik.keystore --key-store-password sample_password --key-store-alias Telerik --key-store-alias-password sample_password .

  1. Obtain the release .apk located at <app_name>/platforms/android/app/build/outputs/apk/<app_name>-release.apk.
  2. Publish your Android app by uploading the .apk file to the Google Developer Console. For more information, see How to publish an Android app?

Application Id and Package Name

Both Package Name, and Application Id, are unique identifiers, provided by you for your app.

  • Package Name is used to identify resources such as the R.
  • Application Id is used to identify your app on devices and at the Google Play.

In the NativeScript framework, both are set to the applicationId in app.gradle. The NativeScript CLI build system will set them as the package attribute in the generated project in platforms/android/src/main/AndroidManifest.xml. In the app/App_Resources/Android/AndroidManifest.xml it will use a placeholder: package="__PACKAGE__". Do not modify the package attribute there.

NOTE: To edit the Package Name and the Application Id, modify the package.jsonof your app and set the nativescript.id key. You may need to delete platforms/android and rebuild using the CLI command tns prepare android.

Read more about “ApplicationId versus PackageName”.

App name

This is the display name for your app. It is purely cosmetic but highly important. For example, it appears under the app icon. The value can be stored in the app/App_Resources/Android/values/strings.xml file

<resources>
    <string name="app_name">MyAppName</string>
    <string name="title_activity_kimera">MyAppName</string>
</resources>

By default (or if the project lacks the values above) your application name is generated with the create command (e.g. tns create testApp will have app name testApp)

You can check out more information about the elements you can define in the AndroidManifest.xml here.

App icons

App icons are defined similar to the app name. The icon name is defined in the app/App_Resources/Android/AndroidManifest.xml file, as an android:icon="@drawable/icon"attribute, on the <application> element.

The actual .PNG icons stay at the Android resources in app/App_Resource/Android/<DPI>/icon.png, DPIs:

DIRECTORY DPI SCREEN SIZE
drawable-ldpi 120 Low density screen 36px x 36px
drawable-mdpi 160 Medium density screen 48px x 48px
drawable-hdpi 240 High density screen 72px x 72px
drawable-xhdpi 320 Extra-high density screen 96px x 96px
drawable-xxhdpi 480 Extra-extra-high density screen 144px x 144px
drawable-xxxhdpi 640 Extra-extra-extra-high density screen 192px x 192px

Note: NativeScript supports adaptive icons on Android 8 and above (API 26+). No code changes are required – follow the Android guidelines for creating adaptive icons for your application.

Launch screen

Android has no built-in mechanism to provide launch screen image. Here is a documentation article that describes how to implement a launch screen in the NativeScript framework.

Certificates

Debug certificate

These are automatically generated by the Android SDK tools for you.

In debug mode, you sign your app with a debug certificate. This certificate has a private key with a known password. The process is handled by the Android tooling.

You can read more at “Signing in Debug Mode”.

Release certificate

The release certificate for Android is created by you; it does not have to be signed by a certificate authority. It is easier to create a release certificate for Android than it is for iOS. You should, however, be more careful with your certificate.

A few pitfalls are:

  • You create the certificate only once. If you lose it, you will not be able to publish any updates to your app, because you must always sign all versions of your app with the same key.
  • If your certificate expires, you will not be able to renew it. Ensure long validity when creating a new certificate (for 20+ years).
  • If a third party obtains your private key, that party could sign and distribute apps that maliciously replace your authentic apps or corrupt them.

You can generate a private key for a release certificate using the keytool.

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

This will run an interactive session collecting information about your name, organization and most importantly — keystore and alias passwords.

Google Play Developer Console

You will need a developer account and you will need to log into the Google Play Developer Console.

Go to the All applications section and click the + Add new application button.

  • You will get prompted to provide the app title
  • You can then proceed with the store listings.
  • You can fill in app description, screenshots and so on.
  • You can also submit an APK. Read about how to obtain an APK from a NativeScript app.

Builds

Build versioning

We have already explained how the Application Id is set in your project, how icons are added to your app and how you can set the display name.

Before the build, you need to set two important things: the versionCode and the android:versionName.

When a build is uploaded, its versionCode should be larger than previous builds. A new build with a higher versionCode is considered an upgrade to builds that have a lower versionCode. The versionCode is an integer so you should carefully consider a strategy for versioning.

Both values are stored in app/App_Resources/Android/AndroidManifest.xml.

NOTE: android:versionName is a string value, which is used to represent the application version to the user whereas android:versionCode, which is integer value showing version of the application code relative to the other versions. You can read more about “Versioning Your Applications”.

In the app/App_Resources/Android/AndroidManifest.xml, the versionCode and versionName appear as:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.nativescript.name"
      android:versionCode="2"
      android:versionName="1.1">
      ...

Build signed release APK

You can perform a full build and produce a signed APK using the NativeScript CLI:

tns build android --release --key-store-path <path-to-your-keystore> --key-store-password <your-key-store-password> --key-store-alias <your-alias-name> --key-store-alias-password <your-alias-password> --copy-to <apk-location>.apk

You can then use the produced <apk-location>.apk for upload to Google Play.

APKs with ABI splits

If you want to reduce the apk sizes you can check how to achieve this in Android ABI Split article.

Submit with the Google Play Developer Console

To submit your app to the Google Play Developer Console:

  1. Log into the Google Play Developer Console.
  2. Select your application and go to the APK section.
  3. Choose ProductionBeta or Alpha stage and click the Upload new APK.
  4. Select the APK produced by the CLI.

You can read more about these stages at “Set up alpha/beta tests”.

Once you upload your APK, it will go through a review. When approved, you can move it to production to make it available on Google Play.

Submission automation

Some tools allow the submission process to be automated – MIT Licensed one: fastlane. You can also hack your own scripts around the Google Play Developer API.

Publish

Once you successfully upload your APK, and it passes Google review, you will be able to move your APK to production, and it will go live on Google Play.