Android Studio Tips and Tricks
Master some hidden gems of Android Studio and improve your overall development efficiency with these Android Development tips and tricks. By Nishant Srivastava.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Android Studio Tips and Tricks
30 mins
- Getting Started
- Code Refactoring
- Using Shortcuts to Move Code Around
- Shifting Code Up and Down
- Moving Sections of Code
- Duplicating Code
- Deleting Code
- Renaming Code Constructs
- Changing Method Signatures
- Extracting Variables
- Extracting Methods
- Extracting Method Parameters
- Inlining With Shortcuts
- The Magic of Quick Fix
- Using Android Studio to Navigate Your Codebase
- Searching the Codebase
- Running More Specific Searches
- Showing Methods and Properties
- Go to an Implementation/Declaration
- Locating Errors
- Debugging Like a Pro
- Color Code Logcat
- Logging Using Non-Suspending Breakpoints
- Mastering the Android Studio UI
- Grouping Your Projects
- Avoid Jumping to Design Tab
- Using Better Fonts
- Plugins: Power-Ups for Android Studio
- Helpful Plugins for Android Studio
- Miscellaneous Tricks
- Create Command-Line Launcher
- Including On-Demand Dependency
- Using Android Studio’s Local History
- Verifying Java-Kotlin Interop
- Copying Tooltips
- Keeping Your Code Tidy With Region Blocks
- Executing Code Blocks
- Sharing Your Project as a ZIP File
- Where to Go From Here?
Using Android Studio’s Local History
Most developers love the Git versioning system, but not everyone chooses to use it. Sometimes, you’ll mess up your git history, and that’s a real bummer. :[
Luckily, Android Studio has a built-in Local History, which automatically saves a history of changes that you’ve made. When you’re stuck and want to revert to an earlier version, right-click inside the editor and select Local History ▸ Show History:
which opens a window with all the changes you’ve made in the past:
Once you select one of those changes, click on the Revert button at the top-left corner:
This can save you hours of fiddling with the codebase and manually attempting to restore earlier code. :]
Verifying Java-Kotlin Interop
Many apps in production still use Java and Kotlin side-by-side in their codebase. This is unavoidable since you can’t write the whole app in Kotlin overnight.
What do you do in such a situation? You write code in Java as well as Kotlin, making sure they can interop properly. There’s even a full guide on how to do this in the official documentation.
But checking the guide manually is error-prone and breaks the continuity in the development process. Here, Android Studio comes to the rescue once again.
This little-known feature isn’t enabled by default, but Android Studio has inspection rules that can validate Java-Kotlin interop compatibility as you write your code.
Find this feature under Preferences ▸ Inspection. Search for the keyword interop and enable the option Kotlin Interoperability.
Copying Tooltips
How many times have you been in a situation where Android Studio uses a tooltip to tell you that it’s detected an issue?
You’d like to copy the tooltip’s message and search for it online to find a solution. But to do that, you have to type out the whole message, word by word. Ugh.
Well, actually, Android Studio can copy the text for you. Just move the cursor over the tooltip, press ⌥ in MacOS or Alt in Windows or Linux and click the tooltip itself.
Keeping Your Code Tidy With Region Blocks
Here’s a neat trick: Oftentimes, you want to group code inside a file into small sections, because there is just too much to look at. Ideally, you should split your code into meaningful classes and functions, but even then you might have a lot to look at inside one file.
To solve this, Android Studio offers what’s known as Region comments, which you can collapse and expand.
To create a region, start a single-line comment and, without adding spaces, write region
. Add a space and the name of the region.
Next, at the point where you want to close the region, start another single line comment and, without adding spaces, write endregion
. The code looks something like this:
... //region Print Functions fun methodOne(){ // Awesome code } fun methodSecond(){ // Awesome code } //endregion
That’s it. You can collapse this using the – icon in the caret region on the left-hand side of Android Studio:
Executing Code Blocks
Sometimes, you only want to execute a small block of code instead of running the whole app. The use-case for this is usually when you’re building a utility function and you want to verify that it works properly.
Android Studio provides a very neat solution: Just add a top-level main
function. The IDE will let you execute that function locally as if it were a unit test.
Even better, use a live template to set it up by typing psvm
and hitting Enter. Super neat!
Check the gif to see how it works:
Sharing Your Project as a ZIP File
Oftentimes, if you’re not using the Git versioning system, you want to share your project as a compressed zip file.
The trouble with doing so manually is that you’d bundle the generated build files along with your project. So you’ll either have to delete all those files or run the gradlew clean
command before zipping your project folder.
Android Studio makes this super easy by generating a zip file without including the generated files. You end up with a clean and compressed zip archive, just by using a simple command in the File menu.
Phew, that was a lot of tips and tricks related to Android Studio, but you’ve only touched the tip of the iceberg. Android Studio is full of productivity tricks for developers.
Where to Go From Here?
You can find the final project in the tutorial .zip file, which you can download using the Download Materials button at the top and bottom of the tutorial.
If you’re new to Android Studio, be sure to check out our tutorial, Beginning Android Development with Kotlin, Part One: Installing Android Studio.
You can also learn more Android Studio tips and tricks by reading the official documentation.
I hope you enjoyed exploring Android Studio tips and tricks. If you have any questions or comments, please join the forum discussion below!