Mobile Angular UI - Creating APK File


Advertisements

In this chapter, we will create an apk file of the Mobile Angular UI app. We are going to make use of app setup done using cordova and phonegap in the previous chapter. So please check the previous chapter about App Setup using PhoneGap and Cordova where we have already installed cordova, phonegap and created Mobile Angular UI app in cordova.

Let us now create a APK file for the app created. For that we need to install the following −

  • Java Development Kit (jdk 1.8)
  • Gradle
  • Android SDK Tools

Java Development Kit (jdk 1.8)

To create the app and build it, one important requirement is JDK 1.8. We need the version of jdk as 1.8 as that is a stable one so far with cordova build.

Go to the following link to install jdk1.8.

JDK

Install the Jdk as per your operating system. Once the installation is done, it is necessary to update the environment path JAVA_HOME. In case you face difficulty in installation of Jdk refer this Java installation tutorial.

For windows users to update Envionment Path, right click on My computer → properties→ Advanced System Settings

It will display screen as shown below −

JDK Screen

Click on Environment Variables. It will display screen as shown below −

Environment Variables

Click on New button, it will display a screen as shown below −

Environment Button

The variable name will be JAVA_HOME and the Variable value will be the path where jdk 1.8 is located.

Once done close your command prompt and open it again. Now type java –version, it should display the version of java you have installed as shown below −

java version

We are done with installing JAVA.

Gradle

Now install gradle, which is one of important requirements to build the app.

Go to Gradle install page and install latest version of gradle as per your operating system.Download and install it on your system. Once installed you need to update the path of Gradle in environment variables. In case you face any difficulty in installation refer this gradle tutorial.

For windows users to update environment variable, add the location of gradle to in Path variable as shown below −

environment variable

Once done, close the command prompt if open. Now open the command prompt and check for the version of gradle installed.

command prompt

We have installed Gradle version 6.2.2 for this tutorial.

Android SDK Tools

Now install Android studio on your system. Along with Android studio, the Android sdk package will also get installed. We are more interested in the Android SDK package. The SDK package will have tools and build-tools, we need to add the same to Environment Variables for cordova to access during build. Incase you face any difficulty refer this tutorial for Android Studio Installation.

For windows users locate the path of android sdk and add the Variable ANDROID _SDK_ROOT to environment variable as shown below −

SDK ROOT

Now add the tools and build-tools path for example −

C:\Users\AppData\Local\Android\Sdk\build-tools;C:\Users\AppData\Local\Android\Sdk\tools\bin;

To the path variable at the end as shown below −

path variable

Once done close the command prompt and open it again.

Execute the command: sdkmanager "platform-tools" "platforms;android-28" in your the command prompt. It will install the android-28 target api that we need. For more details on sdkmanager refer the below link −

https://www.tutorialspoint.com/android/android_sdk_manager.htm

We have already created a project setup using cordova and phonegap in the previous chapter. This is how the app looks in the browser −

path Setup

Let us now check if cordova has all the details to build the app. To do that we can use the following command − cordova requirements.

cordova requirements

Build the App

To build the app using cordova the command is as follows −

cordova build

The above command will build the app for all the platforms you have added.

The run the build platform wise you can make use of following command −

cordova build ios
cordova build android

We have added for only one platform i.e android, hence can use the cordova build command directly.

build command

If the build is successful, you should get the following output −

build Output

It displays the location of the app apk file. Now we have the apk, but we cannot publish the same, as it is an app-debug.apk. We need a release apk that can be published in Google Play Store.

App Release

To create app-release, we need to create a keystore. A keystore is a file that has private keys and certificates.

To create a keystore file will make use of JAVA keytool. A JAVA keytool is a tool that helps to create a certificate.

Following is the command to create a keystore −

keytool -genkey -v -keystore testapp-key.keystore 
-alias testapp-key -keyalg RSA -keysize 2048 -validity 10000

The name of the keystore we have used is as follows −

testapp-key.keystore

The alias is testapp-key same as the name.

Execute the command in the command line from the project root folder.

project root

When the command executes, it will ask you some questions, like password, first and last name, organization unit, city, state, etc. You can enter the information and once done the keystore will be created and the keystore file will be stored inside the project root folder.

Once the keystore is done, create a build.json inside myfirstapp\testapp\platforms\android\build.json.

The details are as shown below −

{
   "android":{
      "release":{
         "keystore":"testapp-key.keystore",
         "storePassword":"testapp123",
         "alias":"testapp-key",
         "password":"testapp123",
         "keystoreType":""
      }
   }
}

You will have to enter the keystore details, as well the password you entered while generating the keystore.

Once the keystore and build.json are done, we are now ready to build the apk for release.

Following is the command to build it −

cordova build android --release
build_json

Once the build is successful, you will get the release apk as shown below −

release apk

Now you can use this apk in your Google Play Store to publish it and get your app live.

Google Play Store

Once you have app-release.apk ready, you need to upload in to Google Play Store. To upload you need to sign-in in Google Play Store. The first time user has to pay $25 as the developer start price. Once that is done you can proceed and upload your apk file. You can follow the steps given here to upload your APK file.

Advertisements