Building an APK for Google Play

Before you open Google Play to register and publish this app (which is the next step), let's double-check a couple of things to ensure that our metadata is correct.

Open app/App_Resources/Android/app.gradle and ensure that the applicationId is correct for your package name:

Also, open package.json at the root of the project and double-check the nativescript.id there as well for good measure:

Now, you will need to generate an executable Android file for your application. On Android, this file has a .apk extension, and you can generate this file using the NativeScript CLI.

The tns run command you were using during NativeScript development actually generates a .apk file for you and installs that file on an Android emulator or device. However, for a Google Play release, the build you create must also be code signed. You can refer to Android's documentation (https://developer.android.com/studio/publish/app-signing.html) on code signing if you want to dive into the cryptographic details, but at a high level, you will need to do the following two things to create a release version of your Android app:

  • Create a .keystore or .jks (Java keystore) file
  • Use that .keystore or .jks file to sign in to your app during a build

The Android documentation provides you a few options on how you can create your keystore file (https://developer.android.com/studio/publish/app-signing.html#release-mode). Our preferred approach is the keytool command-line utility, which is included in the Java JDK that NativeScript installs for you, so it should already be available on your development machine's command line.

To use keytool to generate a keystore for code signing our app, we will use the following command:

keytool -genkey -v -keystore nstudio.jks -keyalg RSA -keysize 2048 -validity 10000 -alias nstudio

The keytool utility will ask you a number of questions, several of which are optional (name of organization and the names of your city, state, and country), but the most important ones are the passwords for both the keystore and the alias (more on that later on). Here's what the keytool process looks like when we generate the keystore:

Before we move on to how to use this .jks file, there's one important thing you need to know. Place this .jks file somewhere safe, and do not forget the password for the keystore or for the alias. (Personally, I like using the same password for my keystore and my aliases to simplify my life.) Android requires you to use this exact same .jks file to sign in to any and all updates to your app. This means that if you lose this .jks file, or its password, you will not be able to update your Android app. You'll have to create a brand new entry in Google Play, and your existing users will not be able to upgrade—so take care not to lose it!

Oh, and one more thing to note in most cases is that you'll want to use a single keystore file to sign in to all of your personal or company's Android applications. Remember how you had to pass a -alias flag to the keytool utility, and how that alias had its own password? It turns out that one keystore can have many aliases, and you'll want to create one for each Android app that you build. 

Okay, so now that you have this .jks file, and you have it stored in a nice and secure location, the rest of the process is quite easy.

Build our Android app using webpack and pass it the information you just used to create the .jks file. For example, the following command is used to create a release build of nStudio:

npm run build-android-bundle -- --release --keyStorePath ~/path/to/nstudio.jks --keyStorePassword our-pass --keyStoreAlias nstudio --keyStoreAliasPassword our-alias-pass

Once the command finishes running, you'll have a releasable .apk file in your app's platforms/android/build/outputs/apk folder; note the location of that file, as you'll need it in the next step—deploying your app on Google Play:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.217.151