Things you can do in Android Studio

Based on a recent discussion on Twitter: a list of things that you can do in Android Studio which can make your life much easier.

Hide files you don’t care about

If you work in a project with lots of generated code (e.g.: you use Dagger), you can exclude generated files from searches using Scopes:

Preferences -> Appearance & Behaviour -> Scopes -> + -> local -> Use whatever pattern you need, for example !file:*intermediates*/&&!file:*generated*/&&!file:R.java.

If you want to go the nuclear option, Preferences -> Editor -> File Types -> Ignored Files and Folders will let you hide entire directories (e.g. /build, /generated) from Android Studio and prevent them from being indexed. This might be useful in very large, unperformant projects but I wouldn’t necessarily recommend it.

Rapidly protoype ideas

Scratch files are a thing in IntelliJ products. These are throwaway files where you can quickly sketch out ideas, and if you want to you can choose to use the classpath of a specific module, should you want to pull in a dependency.

The cool thing about these is they evaluate expressions in real-time on the right-hand side of the editor (similar to a Swift playground), so if you are unsure about how a specific function behaves and want to test it quickly without writing a unit test, you can.

An example of how scratch files evaluate each line

I use these a lot for sanity-checking when I’m second guessing an interaction, and they’re also great for quickly demoing an idea to someone in a pairing session.

Create a new one with CMD + SHIFT + N.

Edit many lines at once

Sometimes we have to do some tedious task which involves editing many lines. As a contrived example, perhaps changing all annotations in a file from one thing to another thing (yes, you could use find and replace for this).

Intellij/Android Studio lets you edit multiple lines at once, and there are several ways of doing this. SHIFT + OPTION + click lets you place multiple cursors one-by-one. CMD + SHIFT + OPTION + click and drag lets you drag to add multiple cursors to the same point on many lines.

Dragging to select many lines at once

Perhaps more usefully, you can select all instances of any text which matches what you currently have selected using CTRL + G and then edit them all at the same time.

Selecting many matching items at once

Selected one-too-many? No problem - adding SHIFT (CTRL + SHIFT + G) lets you step backwards.

Copy/paste all the things

Did you know that IntelliJ products have a clipboard history? CMD + SHIFT + V lets you access it - arrow keys let you choose which item you want, and as with all windows in IntelliJ/Android Studio, it’s searchable too. Say you want to copy multiple functions to another file - copy each one, then use the history to dump all of them into the new file. No need to go backwards and forwards, copy/pasting individually.

Recover what you did previously

Pretty frequently I’ll work on something, change direction, and then much later realise that my initial approach was better. For these instances, right-click -> Local History -> Show History will let you go back in time and revert your changes, even line-by-line. This has saved me many times.

Quickly locate where you were working

Sometimes you’re working in a large class and stepping through code, and finding where you were working becomes quite annoying. Assuming that you’ve enabled VCS integration, you can seek to changed lines in the class you’re currently viewing with SHIFT + CTRL + OPTION + UP/DOWN. This lets you immediately locate where you were working.

You can also do this via the gutter on the right-hand side - additions are green, changes are blue and deletions are grey. You can click these bars to get back to where you’ve been making changes. This is another good argument for making sure that the gutter is clear in every file - this is generally achieved by clearing or surpressing warnings, which display as yellow or red.

If you’ve been stepping through code for a while across multiple files, you can use CMD + [ or ] to step backwards or forwards. This also lets you skip forward or backwards in a browser or many webapps for desktop, which is why I remember it.

Quickly locate modules with changes

Preferences -> Version Control -> Show directories with changed descendents is super useful in a codebase with many modules - any directories in which you’ve changed/added/deleted a file is displayed in blue. This appears to be on by default in Electric Eel.

Folder structure with a changed descendent

Note points of interest

In a large or unfamiliar codebase, it’s often difficult to recall points of interest or files that you know you’ll need to come back to. For this, we have bookmarks.

In the gutter on the left-hand side, where you typically set breakpoints, CMD + click to set a bookmark, or right-click -> Add Bookmark. Click on the newly created bookmark for options, and I would generally recommend re-naming these to something meaningful.

To view these, use CMD + F3.

A list of bookmarks

You can also set what’s called Mnemonic Bookmarks using CMD + OPTION + F3. These are assigned a number or letter, and you can quickly navigate between them using ^ + whatever you assigned the bookmark.

Diff code using the clipboard

To compare two blobs of code, copy one (CMD + C), highlight the other, then right-click -> Compare with clipboard. This will show you the IntelliJ diff window which you might have seen when using git integration, and it’s incredibly useful for quickly working out the difference between snippets of JSON, large methods which are subtly different, or whatever.

Have plugins do tedious work

Three essential plugins which have saved me hours of time:

  1. String Manipulation lets you change the casing of a string without doing it manually. camelCase, snake_case, SCREAMING_SNAKE_CASE, filtering, sorting, incrementing… tonnes of tedious tasks can be automated. Use CMD + A and search for the format you want to apply.

  2. Kotlin Fill Class is incredible if you use fakes a lot in unit tests. Invoking it will automatically fill a function or class constructor for you (as much as it is able to) using named arguments, which can save a tonne of time. Also useful if you’re invoking a function you’re not familiar with and want to see all of the arguments without digging into docs.

  3. JSON to Kotlin Class is also great. Creating model classes from JSON by hand is time consuming and error prone, and whilst there are websites which will do it for you, why not do it directly in the IDE? Supports all the usual serialisers and is very customisable.

Fin

That’s all for now. If I think of anything else I’ll add it here. Thanks for reading.

comments powered by Disqus