Posts

12 Android Studio Tips and Shortcuts to Navigate Your Codebase Faster Header Image

12 Android Studio Tips and Shortcuts to Navigate Your Codebase Faster

We Android developers spend most of our time in Android Studio. In my case, coding mostly means wandering in the codebase from one file to another. I spend more time reading code than writing and you probably are the same.

You need to figure out the context, and find the perfect place to add code that does what you intended. This requires a lot of navigating between files.

Navigating the codebase faster speeds up the overall development. Here are my tips and shortcuts that make navigating the codebase a breeze.

Android Studio Tips and Shortcuts to Navigate Your Codebase Faster

Cmd + Click

If you don’t know already you can Cmd + Click on so many things to get awesome help from Android Studio. Clicking a declaration would list you all the usages or list them if there are more than one. Clicking a variable or a function would beam your caret to its declaration point.

If you want to learn one thing from this article, it should be this.

Jump between parent and child classes

When you are overriding a function in a child class or implementing an interface, you may want to go back and forth. Android Studio has mini buttons available for your convenience. Use them to quickly jump from the overriding function and overridden one.

Navigate dependency injection graph

If you are using Dagger or Hilt, you would want to navigate in the dependency injection graph. Finding where an injected instance is provided or listing all dependency-related files is easy in Android Studio.

Use the graph icons to navigate the dependency graph conveniently.

Find classes or files to navigate

This is one of the shortcuts I use repetitively every day to navigate around. If you want to jump to a class of which you know the name, Cmd + O is your best friend. You can also hit Shift to go to the next tab and search for a file instead.

Use camel case search when finding files

Camel case search is a really powerful text-based search functionality in Android Studio. You can use it in autocomplete or use it when you’re looking for a file to navigate. In the below example we have two very similarly named files: BlueLinePickingViewModel and BlueLinePackingViewModel. You don’t need to type the whole name to jump to this file. You can use just the initials of every word and also the differentiating letter “i” after “P”.

So BLPaVM will return BlueLinePackingViewModel whereas BLPiVM will find BlueLinePickingViewModel in the codebase.

Search some text in the codebase

When I don’t know the file name I’m looking for, I search some text instead.

Hitting Cmd + Shift + F keys to open the Find in Files dialog, I can quickly find the part of the project I want to take a look.

Jump to a line within the file

When you want to visit a specific line in a long file, sometimes it could be easier to move with the Go To Line dialog. Especially, if you are reading source code and not very familiar with that particular file. Hit Cmd + L and write the line number to move your caret there.

Jump to a specific line in any file

You can use the Find a File or File a Class dialogs to jump to a specific line in that file. This could be useful when you are reviewing code in the browser and you want to open that exact location in the IDE. What you need to do is to use # character after writing your query and postfix it with the line number. For example, if you want to go to line 42 of SignInFragment you can write SignInFragment#42 and jump directly to that line.

Also, don’t forget that this becomes more powerful with the use of camel case search.

Navigate caret history back and forth

When I learned about this feature my efficiency in navigating the codebase almost doubled!

Android Studio keeps a history of your caret position within a file and among many files. So if you want to go back (or forward) to see that thing you were looking for seconds ago, you don’t have to remember the file name or the line number.

You can just use Back (Cmd + [) and Forward (Cmd + ]) shortcuts to move your caret in time!

See recent files

This shortcut is not something I’ve known of until recently. If you’re like me and don’t know about this gem yet, here it is: you can hit Cmd + E to list recent files to pick one from.

This would save you some time if you’re working on projects with many many files.

Spot the open file in the project hierarchy

Let’s say you navigated a file using the search or caret history and you would like to jump to the files nearby. This is a common scenario when you want to jump from a Fragment to its ViewModel or to other classes within the same package.

The easiest way to do it is to spot the file in the project hierarchy to get some visuals of the surroundings of the file.

Android Studio has a small button with a sniper scope icon for that.

When I learned about this button, I was really happy.

Also, don’t forget to use Collapse All button at the same row to hide everything to remove the clutter in your project view.

Close all tabs but one

Speaking of removing the clutter, I sometimes hate finding myself trying to reach a tab in the tab bar. That’s when I use an Option + Click to remove all tabs but the open one and breathe a sigh of relief.

I hope these small tips and shortcuts add up and make your life a little bit better when navigating in the codebase in an Android project.

7 Android Studio Tips to Improve Productivity

7 Android Studio Tips to Improve Productivity

Android Studio is a complex IDE with many features, shortcuts, and plugins.

As an experienced Android developer, I have some habits built around using the IDE after spending long years with it.

These are some of my favorite Android Studio tips that I believe improve my productivity and save me a lot of my valuable time.

1. Use a Physical Test Device Instead of an Emulator

This may sound a bit odd if you are a fan of using the emulator for your daily development.

After all these years of seeing the emulator getting in really good shape, I can understand you.

However, after switching back and forth between an emulator and a test device during my Android Development career, I finally found peace by always relying on my test device.

Test devices are fast, don’t bring any CPU or RAM load on your development machine, can be tested for real-life scenarios (like getting a call, putting the device to sleep, locking/unlocking) better, and allows you to touch and use what you just created.

If you haven’t had a physical test device as your programming companion, you should definitely give it a shot.

2. Use Wireless Debugging

There’s another tip for test devices: use the wireless debugging option!

Having a USB cable connected all the time to your computer is no bueno.

After Android 11, the wireless debugging and QR pairing are working like a breeze. I’ve been using it for almost a year now and I’m pretty happy with its performance and reliability.

Besides, I’m using a dock to keep my test device charged all the time so my workspace setup can stay sane and clean.

Bonus: You can use Developer Tiles to create a shortcut to enable wireless debugging right from your notification tray!

Android Wireless Debugging

3. Navigate the Codebase Faster with Shortcuts

If you are maintaining a huge codebase you’ll know finding the correct place to write code is as time-consuming as writing the code itself.

You may find yourself jumping between parent and child classes, abstract functions and their implementations, injected instances and their providers, etc.

Android Studio offers mini buttons to help you navigate between all these. If you haven’t started using these buttons they are time-savers!

Also, you can Cmd + Click (or Ctrl + Click) variable, function, and class declarations to find their usages or you can click the usages to jump to the declarations.

Android Studio Tips: Use Wireless Debugging

Another shortcut I use hundreds of times each day is Navigate > Back (Cmd + [) and Navigate > Forward (Cmd + ]) actions.

Using these actions Android Studio moves you through your caret history. If you jump from one file to another, Android Studio remembers it. If you have moved your caret to different positions within a file Android Studio remembers it.

So if you are working on multiple files, going back and forth several times will be a breeze with these.

There are other useful shortcuts to navigate, check out the “Navigating and searching within Studio” section of the official shortcuts page to learn more about them.

4. Deal with Imports on the Fly

When writing code or copying & pasting snippets, dealing with imports will always be a pain.

Luckily Android Studio can figure out the package you want to import in most cases and can do it for you.

You just need to enable the “Add unambiguous imports on the fly” preference under Preferences > Editor > General > Auto Import menu to let Android Studio add the correct imports for the code you are adding to the codebase.

Preferences > Editor > General > Auto Import menu

From time to time, I tried to check the “Optimize imports on the fly” option too but that option causes trouble for certain scenarios when I try to bulk edit strings or do other advanced operations.

So, feel free to experiment with that option too if you’re feeling adventurous.

But remember, you can always optimize imports before committing your code with another setting, which is safer than using this option.

5. Master Effective Editor Usage

All operating systems have keyboard shortcuts to jump a word or a line. Combining it with a selection shortcut you can easily navigate through lines and modify them.

On macOS I use the following:

Option + Left / Right Arrow > Jump Left / Right Word by Word

Cmd + Left / Right Arrow > Jump to Start / End of the Line

Shift + Option + Left / Right Arrow > Jump Left / Right Word by Word and Select

Shift + Cmd + Left / Right Arrow > Jump to Start / End of the Line and Select

To up your game in effective editor usage, you can learn some additional shortcuts.

You can use Shift + Cmd + Up / Down Arrow to move a line, up and down in the file.

You can use Cmd + D to duplicate a line or Cmd + Backspace to remove it.

Another one I use frequently is Control + G (Add selection for next occurrence) shortcut. After selecting a word, you can keep hitting Control + G to keep adding more instances of the same text.

Android studio keymaps

There are a lot of editor shortcuts to discover in Android Studio, take a look at the Preferences > Keymap > Editor Actions section to learn some keyboard skills.

As a bonus, check out the String Manipulation plugin and thank me later.

6. Utilize the Version Control Support of the IDE

I’m a big fan of using Git without any GUI and staying on the terminal. However, Android Studio has great tooling around Git so even I can’t resist using them.

The commit dialog (Cmd + K) is a really convenient way to commit the files you wish with some powerful settings.

After you hit the gear icon in the commit dialog you can enable some actions for Android Studio to run before each commit.

My favorites are “Reformat code”, “Optimize imports” and “Check TODO”.

Version Control Support

Other than the commit dialog I use the push dialog, interactive rebase dialog and conflict resolution screens to ease my daily git routine.

I totally recommend giving it a shot, even if you love using git on the CLI!

7. Create Live Templates

My last productivity tip is to create live templates for repetitive structures in your codebase.

Live templates allow placing boilerplate snippets with editable areas into which Android Studio will guide your caret.

For my last project, I have two templates: one for creating test functions and the other for creating ViewModels with preset configurations.

Android studio Create Live Templates

Don’t forget to see this article (which includes a video too) to see how to write code faster by utilizing live templates.

Thank you for reading and stay tuned for more Android Studio tips and productivity articles!

What’s New at Android Studio Electric Eel (Google I/O 2022)

Google announced quite a lot of new features, APIs and new devices at Google I/O 2022. One of the main focus of the developer keynote was for Android Studio. We’ve created a comprehensive list of everything new announced for Android Studio at Google I/O 2022. Here’s what’s new at Android Studio Electric Eel Canary:

Better stability

Of course with every version there is some kind of work to improve the stability. This year, Google claims that they’ve fixed over 1600 bugs, 20 UI hangs and 12 memory leaks.

Run Jetpack Benchmark

Microbenchmark is a Jetpack library that measures the execution time of your Kotlin or Java code. It’s especially good for code that runs repeatedly like RecyclerView scrolling, data manipulation and such.

Macrobenchmark

Macrobenchmark library is used for testing larger use-cases of your application, such as application startup and complex UI manipulations. It can be run on CI systems as well.

Macrobenchmark

Baseline Profilers

Adding Baseline Profilers to your app lets Android to improve startup speed, method call performance and make your app faster in general.

Crashlytics in Android Studio

Firebase Crashlytics will directly display its reports in Android Studio. You can access the reports through App Quality Insights window. You can see where your app crashed, and go to the line responsible for that crash in Android Studio.

Crashlytics in Android Studio

Resizable Emulators

A new Android emulator type, resizable is available. Instead of running apps for various sized screen devices, you can test your apps by resizing the emulator window to desired size.

Resizable Emulators

Live Edit

If you’re building using Jetpack Compose, you can see the results of your code changes in your Compose Preview, running emulators and connected physical devices. This feature is experimental, so look out for bugs.

Live Edit

Animation Preview

Previewing animation required a lot of building and running the app. Those who tweak animations to perfect parameters know that most of the time is spent on waiting for the changes to build. New Android Studio adds previewing animations for Jetpack Compose. You can see a timeline of animations running and tweak them.

Animation Preview

Dependency Insights

You can view popular dependencies and how they are used by other developers from Android Studio. If a dependency is marked as outdated, Android Studio will warn you to update to a supported version. More info on Google Play SDK Index, check out its developer page: https://goo.gle/play-sdk

Dependency Insights

There are many more improvements to Android Studio Electric Eel like wearOS emulators, wearOS device connect, and so on. For a full list of improvements, check out Android Developers Blog page: https://android-developers.googleblog.com/2022/05/whats-new-in-android-studio.html