When it comes to mobile development, Android has always been a pain to deal with. While iOS has a terrible deployment to production experience, Android has a terrible installation and configuration experience.
I’ve been developing Android applications since before hybrid web and cross-platform native were a thing and up until recently, the Android installation experience has been unchanged. However, now things are different with a heavy push towards Android Studio and less towards the command line interface (CLI).
If you’re like me and feel that Android Studio has no business in your NativeScript workflow, you’re probably still interested in the Android CLI’s less than ideal experience. We’re going to see how to get setup on macOS with not only the Android CLI, but also the various packages and appropriate simulators for NativeScript development.
Going forward it is important to note a few things:
If you’d rather do a cloud build for Android, check out NativeScript Sidekick. We’re going to do everything locally.
The first step in the Android installation and configuration process is to obtain the Android SDK itself. Remember, we’re not going to bother with Android Studio, so we’ll need the command line interface (CLI) instead. This can be obtained on the Android Developer website. Don’t be fooled, the CLI tools are at the bottom.
You can also run the following command from your Terminal to download build 4333796 of the CLI.
curl -o sdk-tools-darwin-4333796.zip https://dl.google.com/android/repository/sdk-tools-darwin-4333796.zip
Just remember that the latest version and the version I’ve listed may not match. The version I’ve listed is from July 2018.
Extract the downloaded ZIP archive and move it to a directory of your choosing. On my personal development computer, I’ve moved the extracted tools directory to ~/Library/android/sdk/tools, but feel free to do whatever makes sense. It is, however, important that the extracted tools directory end up inside a parent directory because when we install packages some will be installed to the parent.
By now we’re going to assume that you’ve downloaded the command line tools and moved the extracted directory to ~/Library/android/sdk/tools on your computer. Now we need to update our PATH variables for quick and easy access.
In your Terminal execute the following:
If you’re not familiar with Vim, use the text editor of your choice. We need to export a few paths so that things become more convenient for us. Within this file, at the bottom, include the following:
export ANDROID_HOME=/Users/nraboy/Library/android/sdk export PATH=$PATH:$ANDROID_HOME/tools/bin export PATH=$PATH:$ANDROID_HOME/platform-tools export PATH=$PATH:$ANDROID_HOME/emulator
You’ll notice in the above
ANDROID_HOME I’ve given the absolute path to my ~/Library directory. I’m not sure if you can export the shorthand so make sure you use absolute paths. You’ll also notice that platform-tools and emulator don’t yet exist. Don’t worry because we’re going to install a few things which will make them appear.
Make sure you restart your Terminal session for the profile changes to take effect.
With the Android SDK in place and it wired up to our macOS Terminal paths, we can now begin to install the necessary platform tooling and other dependencies.
If you wan’t to be automated and not accept the license agreements manually, you can first accept all of them up front then proceed to install the packages. To accept all the licenses up front, execute the following:
yes | sdkmanager --licenses
sdkmanager should already be in your path. The
--licenses option will present all licenses that are pending being accepted. By prefixing the command with
yes we are agreeing to everything that is presented to us.
Now we can download our platform build tools and other packages. From the Terminal, execute the following:
sdkmanager "platform-tools" "platforms;android-24" "build-tools;28.0.0" "add-ons;addon-google_apis-google-24" "extras;google;google_play_services" "extras;google;m2repository"
We’re downloading quite a few packages, but they are necessary for most NativeScript circumstances. At the time of writing this, the requirement was API 24, but things could change in the future. The process is still the same.
When it comes to Android emulators we have some flexibility. In fact, we don’t even have to go through the Android SDK to make this happen. While we could use a set of tools like VirtualBox and Genymotion, we’re going to stay as vanilla as possible with the core simulators that come with the Android SDK.
From the CLI, execute the following:
sdkmanager "extras;intel;Hardware_Accelerated_Execution_Manager" "emulator" "system-images;android-25;google_apis;x86_64"
The above command will download the Android API 25 version of the simulator. Just like with the Android build tools, you can download as many different varieties as you want.
With a system image on hand, we can create a new emulator, otherwise known as an AVD, from it. From the Terminal, execute the following:
echo no | avdmanager create avd -n nexus5 -k "system-images;android-25;google_apis;x86_64" -c 100M -d "Nexus 5"
The above command will use the system image that we just downloaded as well as a Nexus 5 device skin. Feel free to use whatever makes sense to you for your project.
With the AVD created, we can run the emulator with the following command:
Assuming you named it nexus5 like I did, the emulator should run.
You just saw how to configure your Apple computer for Android development using pretty much only the command line. This is an alternative to using Android Studio or the NativeScript Sidekick.
As previously mentioned, the content in this guide should apply beyond NativeScript, even though it was the specific target of the guide.
There are a lot of things you can do with the CLI tools for the Android SDK. I encourage you to check out the official documentation when it comes to installing, updating, or even seeing what packages or emulators are available.
Unsubscribe at any time without hassle.