Uncategorized

Productivity Tips After a Year of Working in Android Studio

I first started serious Android development about a year ago when I joined my current team. Since then, I have learned a litany of tips and tricks from my coworkers and the internet that I have come to use almost every day. These are, in no particular order, the things I find most useful when developing for Android on Android Studio. These are default keybindings, so if you have a custom setup going, this won’t be as useful to you.

Keyboard Shortcuts (mac shortcut / windows shortcut):

cmd + b / ctrl + b: This is probably my most used shortcut. If you use it on an instance of something, it jumps to its definition. If used on the definition of something, it searches for uses of it.
cmd + u / ctrl + u: This jumps to the definition of the super of the currently highlighted overridden method or subclass.
alt + enter / alt + enter: This quickly fixes errors detected by android studio (imports, changing method signatures, etc) – it picks the fix Android Studio thinks is most likely to be right, and in my experience it is the correct fix most of the time. Make sure to verify the fix is what you wanted.
alt + shift + up/down / alt + shift + up/down: quickly move lines up and down. It basically swaps the current line with the one above/below it.
shift + f6 / shift + f6: This renames what is highlighted through out the project, helping to avoid tedious copy/pasting
ctrl + t / ctrl + alt + shift + t: This brings up a refactoring menu which includes the name refactor above. There are many options to choose from and the menu is different for variables, functions, classes, etc.
cmd + shift + o / ctrl + shift + n: This shortcut brings up a quick search through file names. It allows you to avoid searching through the project view for files, as long as you know what they are named.
cmd + alt + o / alt + shift + n: In a similar vein, this command searches through variable names in the project. If you are looking for things related to a certain feature, this comes in handy quite often.
cmd + f12 / ctrl + f12: This is similar to the above command, except it looks for variable names only in the current file.
double tap shift / double tap shift: search everywhere for class names, variables, etc. Basically all above search commands combined.
alt + space / ctrl + shift + i: quick def lookup (in a popup). This is quite useful if you want just a quick peek at the definition of something. It brings up a resizable popup window w/ the definition of any variable, function, class, etc.
ctrl + shift + f12 / ctrl + shift + f12: This collapses all views except the editor- repeating it reopens the collapsed views. It saves a lot of manual clicking.
cmd + backspace / ctrl + y: this deletes the current line. It’s pretty self explanatory.
ctrl + alt + o / ctrl + alt + o: Optimize imports. This deletes unused imports, as well as organizes them in a logical manner (alphabetical by package)
cmd + e / ctrl + e: Opens a searchable window of recently opened files. Simply start typing to view file names that match.
cmd + shift + 8 / shift + alt + insert: This allows you to edit multiple lines at once. First select multiple lines, then hit this shortcut. The selection created on all lines is the smallest selected portion of any one line highlited.
ctrl + o / ctrl + o: Quickly setup overrides on methods. Allows you to choose from a menu from the current class’s overrideable methods.
ctrl + alt + l / ctrl + alt + l: Auto formats code style (things like spacing, linebreaks, etc.)
cmd + n / alt + insert: Autogenerate generic code like getters and setters, blank constructors, etc.
(cmd + /) / (ctrl + /): Line comment out/uncomment all selected code. Add shift to block comment instead.
– Not a keyboard shortcut but related: This plugin for Android Studio tells you what the keyboard shortcut is for things you do manually.

Non Keyboard Tips and Tricks:

– When you want to view a file in the project view, and you have it open in the editor, hit the crosshair symbol in the project view to jump to the file in the project view.
– To create a copy of a class, hold control while dragging the class in the project view, and Android Studio will copy the file and ask you for a name.
– Instead of creating conditionals in your code for breakpoints, you can right click on a break point and enter any valid java statement in the current scope to make the breakpoint a conditional breakpoint.
– show line numbers: In the current version of Android Studio, you can show line numbers on the editor by right clicking on the lefthand side where breakpoints normally live, and selecting ‘Show Line Numbers’
– If you use gradle for your build, it can be painfully slow some times. One thing that I found dramatically increased my build speeds was allowing gradle to build offline when possible. To enable this, go to Android Studio -> Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle, and select the box for ‘Offline Work’
– The bundled Android Studio emulators are relatively slow and not the easy to configure. Genymotion is much faster, and allows you to specify custom dimensions as well as select from a list of predefinied DPIs, making it easier to test UI elements on many different types of phones. Read more about linking Genymotion to Android Studio here: https://www.genymotion.com/#!/developers/user-guide#genymotion-plugin-for-android-studio
– It’s helpful to take the default Android Studio variable names, because they are usually good names for variables, and it helps standardize naming conventions when working with many collaborators.
– This is not Android development specific, but for command line search, I use a tool called ‘The Silver Searcher’, or ag. It is much, much faster than grep, and is even faster than ack. From the git page, a search through an 8GB code directory took ack 110 seconds, while ag took 3.2 seconds. Find it here: https://github.com/ggreer/the_silver_searcher
– For Mac users, the ditto command is useful for a variety of things. It’s like cp, but with various enhancements, the most relevant for my use being that it merges the source with the destination. So if you download something in the form of res/drawable/hdpi, you can ditto the downloaded res directory to the project res directory and it will correctly place files in their destination folders without removing other files.

This is a list of keyboard shortcuts that work for IntelliJ IDEA, which is the base of Android Studio. Most work as described. Also, another great resource for keyboard shortcuts is this series of blog posts: http://www.developerphil.com/android-studio-tips-of-the-day-roundup-1 . It includes animated visual aids.

Thanks for checking this out- if you know of anything useful that I missed here, comment and I’ll add it to the post!

Tags:

Discussion

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s