Blog Infos
Author
Published
Topics
Published

DALL·E generated image (panda scratching his head in front of laptop in office)

 

How many times you’ve faced this issue where you wanted to verify some Java/Kotlin code in your Android app and you ended up putting that code on launch activity to verify that it works properly and spent minutes waiting for code to get deployed on the device/emulator? 🙈

Why people do it?

Not sure about you, but I’m guilty of doing this a lot of times earlier in my career. Now some of you might be thinking that why not just have a pet Java/Kotlin project for these kind of cases? You’re definitely correct but having Android Studio as my main weapon of choice, doesn’t help much here as it doesn’t provide an option to create/view standalone Java/Kotlin project 😕. And if your argument is to use another IDE (e.g. Intellij IDEA or VS Code) for this then again it doesn’t help here as context switch via opening/switching to different IDE is again a pain.

Context switching is particularly destructive because it takes a lot of time and effort to pick up where you left off and get back in the zone.

Intellij’s Scratch File feature

But fret not you don’t need to take any of the above routes as most (maybe all) of the JetBrains IDEs (also applies to Android Studio) already has a really neat feature called Scratch Files, and as the name suggested you can use it to create temporary notes or draft up some code outside the project context.

You can create scratch files using ⇧ ⌘ N (on mac) and Ctrl+Alt+Shift+Insert (on windows/linux).

How many times you’ve faced this issue where you wanted to verify some Java/Kotlin code in your Android app and you ended up putting that code on launch activity to verify that it works properly and spent minutes waiting for code to get deployed on the device/emulator? 🙈

Scratch File Feature in Android Studio demo

 

Few points to note:
  • Scratch files supports multiple languages like HTML, Java, JavaScript, SQL, YAML etc
  • These files are not linked to any project and they are present in a common global directory so every scratch file that you will create will be accessible no matter which project you might be working on.
  • You can use Use classpath of module selection dropdown in case you want to use some code present in your current opened project
  • Enable/Check the Interactive Mode to execute the code without explicitely executing it using the Run Scracth File option present on the top left of the editor window
  • One important point to note is that even though you can write some code dependent on Android platform, you won’t be able to execute it using scratch files as you need an Android OS to provide platform functionality.
  • There is really nice feature in Intellij IDEA using what you can create scratch file from the selected code as shown in below screenshot. You just need to select the code and hit ⇧ ⌘ N (on mac) and Ctrl+Alt+Shift+Insert (on windows/linux) and use the top most option.

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

,

Reap the benefits of Android Studio

Most of Android engineers use Android studio as their IDE and every new version of AS release we get many efficient features that we miss to use which can help to code and debug efficiently.
Watch Video

Reap the benefits of Android Studio

Sweety Bertilla
Principal Engineer
Comcast

Reap the benefits of Android Studio

Sweety Bertilla
Principal Engineer
Comcast

Reap the benefits of Android Studio

Sweety Bertilla
Principal Engineer
Comcast

Jobs

You can see a small demo of the Scratch file in action in below gif

 

Scratch File code demo

 

And that is it 🙂. I got introduced to this feature roughly 2.5 years back and since then I’ve saved ton of time by not waiting for code deployment to heppen on an device just to verify some basic JAVA/Kotlin code. BTW you might need this rarely atleast for Kotlin/Java code if you’re following TDD where you end up writing unit test cases anyways. I’ve seen these problem mainly in companies where there is no culture of TDD or writing unit tests.

Hope you liked reading the article. Feel free to reach out to me on Twitter. Thank You!

This article was originally published on proandroiddev.com on December 24, 2022

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Compose is part of the Jetpack Library released by Android last spring. Create Android…
READ MORE
blog
Today, I will share my recent experience of not reading the documentation 📖carefully about…
READ MORE
blog
Software projects rarely work in isolation. Projects often rely on reusable functionality from libraries.…
READ MORE
blog
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu