Setting up Build Environment for Cordova (Android) Applications on Cent OS 7
Sometimes, it becomes necessary to set up a Cordova build process on a Linux server – to let your colleagues make builds without doing any set up or make the built files available for download from the web. Here we will set up Android application build process on a clear Red Hat Cent OS 7 server.
We do not setup complex development environments often. Each time we have to do this, we usually forgot what we did last time, and have to go through lots of documentation and stumble upon the same errors again. Recently, I had to configure Android builds on a Linux server. This time, I documented each step carefully, so I could share it with you and hopefully save your precious time.
Let’s get to the business.
- Clear Cent OS 7 virtual machine. Note that Cent OS 6 wouldn't work due to low gcc version - Android build would throw an error.
- 1 CPU and 2 GB RAM. More resources – faster builds.
- 10 GB of free disk space
- NVM and Node JS
- Cordova CLI - 8.1.2 at the moment
- Android SDK and CLI
- Java Development Kit 1.8.0
Installing the dependencies
Installing NVM and Node JS
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
To install the latest version, get the up-to-date command here.
Re-open the terminal so that npm command would become available and run:
nvm install 11
This will install the latest Node JS version. At the moment of writing it is 11.7.0. If you are reading this when later major Node version is out, use its version number instead.
Since this is your first Node installed via NPM, it would be used as a default Node. Read this article if you would like to find out how to change Node JS version with NVM.
To check that you are currently using the correct version of Node, run:
Installing Node global packages
You need to have some Node JS packages installed globally. This will make CLI commands necessary for making a build available from the terminal.
npm i -g cordova ionic
Of course, you can skip ionic, if you do not use Ionic Framework for your Cordova app. Do not forget to installed global NPM packages required by your application – be it yarn, webpack, gulp, etc.
Installing Java Development Kit
The next thing necessary for Cordova/Android development is JDK. Installation command:
yum install java-1.8.0-openjdk-devel
You need also set $JAVA_HOME environmental variable to /usr/lib/jvm/java-1.8.0-openjdk-22.214.171.124.b12-1.el7_6.x86_64. Your path may be different, but $JAVA_HOME should certainly point to the directory that contains ./bin/java executable.
To do this, open your ~/.bash_profile file in Vim editor and append the following:
The changes in .bash_profile do not take place immediately, and to apply them, you need to reconnect to the server or run source ~/.bash_profile.
Gradle is a build-automation system which is necessary for making Android builds. In order to install Gradle, we need to download it first. Grab the link to the latest version from this page.
Now create the destination folder and unzip Gradle in there:
mkdir /opt/gradle unzip -d /opt/gradle gradle-5.1-bin.zip
Add Gradle path to your $PATH variable in your ~/.bash_profile file:
Check if Gradle is installed correctly:
source ~/.bash_profile gradle -v
Installing Android CLI and SDK packages
Download the latest Android Command Line tools from here. Do not download Android Studio, scroll to command line tools only.
wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip mkdir -p /opt/android
Move the archive to /opt/android (so that it is available under /opt/android/tools), unzip it in there, and add to the $PATH environment variable /opt/android/tools and /opt/android/tools/bin. android, sdkmanager, and other CLI commands should become available, but note that they would work only if you set $JAVA_HOME environment variable correctly.
Eventually, your $PATH variable should look like the following:
Do not forget to source ~/.bash_profile before proceeding to the next step.
Now it's time to install SDK packages. To check available packages, run
Among the list, we'd need the latest version of build tools and platform-tools, as well as Google Play Services – if you use their features such as push notifications. To install Google Play Services, run:
sdkmanager "build-tools;28.0.3" "platform-tools" "platforms;android-28" "extras;google;google_play_services"
Instead of "platforms;android-28" or "build-tools;28.0.3" use the latest version available.
The script will start downloading the updates. Accept the license agreement and wait till installation finishes.
If you run sdkmanager --list once again, you will see the installed packages at the beginning of the output. And just in case, If you received Warning: File /root/.android/repositories.cfg could not be loaded when trying to use sdkmanager, run touch ~/.android/repositories.cfg.
Now add the $ANDROID_HOME variable to your .bash_profile:
Cordova CLI will throw an error if you do not have $JAVA_HOME and $ANDROID_HOME environment variables or if they contain incorrect paths.
...So now we are ready to go. Clone your project to the server. If you have not installed Git already, run yum install git. The first Android build would take some time, but the subsequent ones will finish much faster.
Hope, you went through all of these steps without any issues, and now are able to build .apk files from your smartphone even when you are far in the woods. Thanks for reading!