Mastering the Terminal side of Android development
As a terminal aficionado I’ve been wanting to write about this topic for awhile. Besides being cool, knowing how to use the terminal simply makes things faster. It’s much quicker to type a few letters and press TAB than to click through endless menu options.
My goal with this article is to share with you how I use the terminal in my android development workflow.
iTerm2
Since we’re going to be spending a lot of time in the command-line, let’s install a better terminal than the default one. Download and install iTerm2.
iTerm2 brings the terminal into the modern age with features you never knew you always wanted. This includes pane splitting, custom colour schemes, paste history, fine-grained control over hotkeys, together with dozens of other handy preferences that you will find useful as you become more comfortable in the terminal.
oh-my-zsh
When you start a terminal application, it is running a shell called Bash. Bash is by far the most popular shell and comes with pretty much every UNIX-based operating system. There are, however, alternatives to Bash that make using the terminal faster and more comfortable for developers.
By default, oh-my-zsh comes with a git plugin which provides many aliases and a lot of useful functions.
Terminal Autosuggestions
zsh-autosuggestions are Fish-like fast/unobtrusive autosuggestions for zsh. It suggests commands as you type based on command history:
Reverse intelligent search
You can hit Control+R to do a reverse intelligent search in the command history. Start typing the command and the shell will autocomplete with previously entered commands.
Then you can either press Enter to execute the command again, use the arrow left or right key to edit the command, or keep pressing Control+R to cycle through other possible completions.
dryrun
You just came across a cool library on github, what do you need to do to test it on your phone?
- Click the
download zip
- Extract the
zip file
- Open Android Studio
- Import the project you just downloaded
- Sync gradle
- Run the project
- Choose the device you want to run
- Test all you want
- Delete the
project folder
and thezip file
when you don't want it anymore
Or….
You can use dryrun:
dryrun REMOTE_GIT_URL
Build faster, build offline
The --offline
flag tells gradle to always use dependency modules from the cache, regardless if they are due to be checked again. When running offline, gradle will never attempt to access the network to perform dependency resolution. If required modules are not present in the dependency cache, build execution will fail.
Assembling develop debug
at full speed:
./gradlew assembleDevelopDebug --offline
Running your unit tests at full speed:
./gradlew test --offline
As an alternative, in Android Studio you can make gradle build your apps fully offline by activating this option:
Settings -> Build, Execution, Deployment -> Build tools -> Gradle
alfi
As an Android developer, you are probably using Android Studio with Gradle. One of the cool advantages is the dependency management that automatically downloads artifacts from a repository and makes them available to your application. Basically, you just add one line into your build.gradle file and your library is included. Pretty easy, right?
But do you know exactly that line?
For this purpose I’ve created ALFI:
- Type: alfi NAME_OF_THE_LIBRARY
- Copy the desired library;
- Paste in your build.gradle.
No UI interaction required and up to 20 results at once!
alfi picasso
Understanding gradle tasks shortcuts
if you run ./gradlew tasks
you’ll get the list of gradle tasks available, what is not listed is the short versions of all of the commands, so here’s a small list of assumptions you can make,
- iDD for installDevelopmentDebug
- aDD for assembleDevelopmentDebug
- cC for connectedCheck
- etc.
You can start doing things like this:
./gradlew :App:iDD
See, everything is smaller now.
Android Rocket Launcher
It’s a gradle plugin that adds tasks to your Android modules for installing and launching all variants, so, no need to run a ./gradlew installDebug
and have to manually go to the phone, find the app among the 50 apps you have installed and launch the right one.
All you need to do is add 2 lines to your build.gradle
Tip: Don’t forget that even when you’re launching the app from the terminal you can always attach the process to the debugger, no need to restart the app in Debug mode.
Output unit tests directly to the console
A small neat trick so we can use to see Android unit tests logging results as they happen.
android {
... testOptions.unitTests.all {
testLogging {
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
outputs.upToDateWhen { false }
showStandardStreams = true
}
}
}
Now when you run your tests they will output something like this:
Convenient logcats
During application development you often want to only display log messages coming from your app. Unfortunately, because the process ID changes every time you deploy to the phone, it becomes a challenge to grep for the right thing.
This small tool solves that problem by filtering by application package. Supply the target package as the sole argument and enjoy a more convenient development process.
pidcat github.cesarferreira.helloworld
TL;DR
Summing it up:
- Install iterm2, a better terminal than the default one;
- Use the oh-my-zsh shell with autosuggestions;
- compact the commands
./gradlew iDD
- Run the commands faster by using the offline flag
--offline
- Pretty print your tests;
- Don’t manually open the app on the phone after install, use this plugin to automate it;
- Attach the process to debugger without restarting the app in debug mode;
- Enjoy a more convenient logcat output with pidcat.
Wrap up
Thanks for getting this far! I would love to know what do you think and if you do something in a different way. Also it would be awesome if you click the little ❤️ and share the article so more people would benefit from it.
If you are interested in more Android development, please check my other articles, follow me on Twitter or check my GitHub projects.