Our Day-to-Day Guide to Productivity for Android developers — Part 2

leboncoin
leboncoin
Aug 14 · 9 min read

By Christian Kula (Android developer)

Image for post
Image for post

For this second part of our series of Our Day-to-Day Guide to Productivity for Android developers, let’s talk about Code Editor and Git tips and tricks.

You may know some, you may not know others. In any case, feel free to share them!

Android Studio

Since we use Android Studio everyday, it’s very important to know how to use our tools in order to perform better. Also, since Android Studio is powered by the IntelliJ platform, most of these tricks can be used in other IntelliJ-based IDEs!

Editor

The IntelliJ editor is very powerful. Besides basic “Find & Replace” or move lines around, it can do so much more ! And since it’s language-aware, it won’t mess up your code (too much at least). Each section title refers to the name of the command in Android Studio so you can easily find it by name if needed.

Option + Shift + Up/Down (macOS)

Alt + Shift + Up/Down (Windows/Linux)

Let’s start with an easy one, you just want to move a line up or down. No need to recopy or cut & paste the whole line, use the keybinding to move the line the caret is currently at up or down. Easy!

Image for post
Image for post

Cmd + Shift + Up/Down (macOS)

Ctrl + Shift + Up/Down (Windows/Linux)

Now, let’s say you wrote an awesome class and you want to move whole blocks of code to be logically located inside your class. You could cut & paste your method block. Or, you could simply move your caret to the method’s signature and press the keybinding. The whole method block will be moved up or down, with respect to code syntax. Like the name implies, you can move statements so it includes methods, variable declarations, expressions, whole classes etc.

Image for post
Image for post

Option location:

Preferences > Editor > General > Smart Keys > Use ‘CamelHumps’ words

With this option enabled, every selection or navigation inside a camelCase word will honor the “hump”. In other words, in a word like , , and Activity each represents a Camel hump and the editor will treat them as a word even though there’s no space separating them. You may not see the usage but wait for the next trick below!

You can also enable “’ words settings when selecting on double click” if you use the mouse.

Preferences > Editor > General > Honor ‘CamelHumps’ words settings when selecting on double click

Option + Up/Down(macOS)

Alt + Up/Down (Windows/Linux)

This feature is also very useful for quickly selecting chunks of code! The “Extend selection” command allows you to successively select expanding logical blocks of code so that you can easily select any expression in the code by placing the caret somewhere inside it and pressing the shortcut a few times. And “Shrink selection” is the opposite. And it also works nicely with CamelHump words!

Rename…

Shift + F6 (macOS, Windows & Linux)

Add Selection for Next Occurence / Unselect Occurrence

Control + G / Ctrl + Shift + G (macOS)

Alt + J / Alt + Shift + J (Windows/Linux)

, if you need to rename a class/method/variable/parameter, don’t rename it manually as you’ll likely forget to rename one instance somewhere in your app, thus leading to compilation errors.

Use the “Rename…” function, it’s super smart because it can rename every reference to the changed name (even in Java/KDoc and in String values!) and it’s scope-aware.

Be careful, though. Sometimes it may go too far and AS will want to rename unwanted String values in files. Fortunately, AS will prompt you to confirm the change if it’s not trivial and you can omit specific references from the rename process.

Add Selection for Next Occurence” serves a similar function but its use case is a bit different. When you press on , AS will select the current word (remember “CamelHump” word). If you press it again, it will select the next occurence of the selection in the current file. And so on. And now, you have duplicated carets at each selection place so you can edit all occurrences at once. It’s a kinda fancier and more flexible “Find & Replace”.

Image for post
Image for post

Navigation

Android Studio has a lot of very convenient features to navigate to and in files and classes etc.

You probably know “Press Shift twice to search everywhere” but it’s most likely overkill. Most of the time, you want to find something specific like a class or file. There’s no need to make Android Studio search in (classes, files, symbols, actions, settings etc.) if you’re looking only for a class.

Make your searches faster by using the corresponding optimized “Find …” feature and use “Search everywhere” only if you don’t know exactly what you’re looking for.

Cmd + O (macOS)

Ctrl + N (Windows/Linux)

When you’re looking for a class or an interface with a specific name

Cmd + Shift + O (macOS)

Ctrl + Shift + N (Windows/Linux)

When you’re looking for a file with a specific name

Cmd + Shift + A (macOS)

Ctrl + Shift + A (Windows/Linux)

If you have to remember only one thing from this article, remember this shortcut because it is the one. I kid you not, this feature is super convenient to the point I wish more text editors would have it.

It’s simple : it will search for an action you can do in AS. Any action. Literally.

You don’t remember the shortcut of “Rename…”, where the option to turn on Presentation Mode is or you need to quickly change your editor theme to Darcula because your coworker can’t stand the Light theme? Press , type a few letters and ta-da, AS will show the matching actions.

Basically, you have access to feature of AS a few keystrokes away.

Image for post
Image for post

Cmd + Shift + F (macOS)

Ctrl + Shift + F (Windows/Linux)

Another useful search option. Let’s say you know that a file contains some string of characters but you can’t remember its filename, nor its location. By using “Find in path”, you can search for any string of characters that appear in a file. You can also specify if you want to search the whole words, match the case or filter by file mask. You can search in project, module, directory or even scope.

Image for post
Image for post

Another feature that I use all the time!

Recent Files

Cmd + E (macOS)

Ctrl + E (Windows/Linux)

Recent Locations

Cmd + Shift + E (macOS)

Ctrl + Shift + E (Windows/Linux)

Like the name implies, it shows the files you’ve recently opened. Useful if you need to go back to file you’ve previously closed but can’t find anymore.

And “Recent Locations” works the same but with your cursor locations. It shows where exactly you’ve recently put your cursor.

Git

We use Git as our VCS so it’s very important for us to master it. Using GUI tools is fine but the CLI is where you’ll see the true power of Git.

AS has very good tools to compare commits and check the commit history so I use them but for every other command, I use the CLI.

reflog, your safety net

Manipulating Git repositories from the CLI can be scary, especially when using not-well known commands.

First, remember that no commit is lost in your local Git repo. Your local Git repo will still keep all your commits info even though it seems you lose access to it (via hard resetting, checking out something else etc.). You can recover from almost every situation using the : its .

However, how can you retrieve a commit SHA-1 if you happen to have lost it somehow?

Enter the command.

This command will show every action that recently happened in your local repo : branch switching, merges, commits, rebases, tags etc. See it as a powerful “Repository History” feature.

Each line will represent an action in the repo with its corresponding so now, you have everything you need to recover from a wrong command.

Let me show you an example. Let’s say you committed something very cool but then decided to remove it and actually, you need this very cool something back.

 commit -m “Commit something very cool”  reset — hard HEAD¹// remove last commit on branch// but now you need the “Commit something very cool” commit back, how to do it ?

Using will give you something like this :

Image for post
Image for post

Let’s explain each line from bottom to top:

  1. Created “Initial commit” commit —
  2. Created “Commit something very cool” commit —
  3. Moved back the HEAD to the previous commit —

Note that the last action is the same as the one of the initial commit. That’s because we moved back to the previous commit ie. the “Initial commit”.

Also, note the second line : it shows a commit with the message “Commit something very cool” was created along with its : .

Great! Now you can recover the removed commit using the command

( applies a commit using its SHA-1 to the current branch.)

cherry-pick 1e6d263

Image for post
Image for post

We got our previous commit back!

Note: a new has been generated for the commit

This is a very simple use case of reflog but it can show you much more very valuable information so don’t be afraid to play around in your Git repositories.

Remember, is your safety net.

Image for post
Image for post

Example of what’s regularly happening on my branches (checkouts, rebases, resets etc.)

Also, don’t forget to . Committing your work is like saving a file : do it often to not lose your progress (and otherwise, won’t be able to help you!)

More information: https://git-scm.com/docs/git-reflog

rerere

Behind this strange name hides a very useful feature. stands for “use corded solution”.

As its name implies, when this feature is enabled, any conflict resolutions, when merging/rebasing, will be recorded by Git. Later, if you happen to stumble upon the same conflict again (for example, you rebase multiple branches and every rebase triggers the same conflicts), Git will use the previously corded conflicts solution and won’t prompt you to resolve it yourself again. Quite handy, right?

To enable functionality, you simply have to run this config setting:

 config — global rerere.enabled true // to enable it globally for all your Git repos on your machine

OR

config rerere.enabled true // to enable it only in the current Git repo

If Git can’t resolve a conflict itself, it will let you do it as usual.

More info: https://git-scm.com/book/en/v2/Git-Tools-Rerere

leboncoin Engineering Blog

Learn more about creative engineers & data scientists building a French virtual Flea Market

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store