Productivity Hack: AndroidStudio Kotlin Scratch File

Shivam
4 min readDec 29, 2019

--

What Is Scratch File?

  • A temporary source file.
  • Scratch file isn’t considered as a part of our projects and won’t be stored in the workspace of the current project.
  • Scratch files are stored in the directory where the android studio is installed on your machine under the `scratches` directory.
  • Once created, the scratch file will be available in all our projects.

When Will I Need It?

Sometimes while working on a project we may need to:

  • Create some temporary notes.
  • Write some code to check some ideas outside of the project context.

Instead of switching to a different application for this purpose, we can use scratch files.

What Extra Benefits Does It Provide?

You might be thinking that we can create a temporary file using other ways then why should we use scratch files? 😉

Well following are the few reasons to start using scratch files.

Scratch file supports:

  • Syntax highlighting.
  • Code completion.
  • And all other features for the corresponding file type.

How Can I Create a Scratch File?

Do one of the following:

  1. From the File menu, select New, and then click Scratch File.
  2. Press ⇧⌘N (Shift + Command +N)
  3. Using find action (Press ⇧⌘A), start typing scratch file and then pick the corresponding action.

Working On Kotlin Scratch file

Just like other types of files Android Studio also provides support to create the Kotlin scratch file but there is more here:

Benefits

  • Run code on the fly without Emulator. 🙃
  • As it doesn’t need an emulator it means we can’t execute any code which has reference to the android framework.
  • Utility classes/methods which are written purely in Java or Kotlin work like a charm.

Features

The Kotlin scratch file provides the following features:

Module Selection

  • Most of the android applications contain multiple modules, if you want to target code of only a particular module then you can select it from the editor tab itself.
  • If you don’t select any module then on the scratch file you will not be able to access any existing code from the current project.

Interactive Mode

  • This mode will execute the code written in the scratch file after you stop writing for 2 seconds.

REPL Mode

  • This is a Read-Evaluate-Print Loop, this will execute only the new code you have added in the scratch file.

Where Can I See The Output?

You can see the output of each line in the side panel of the editor window itself.

Where Can I See The Errors?

  • If any error occurred during the execution of the scratch file then you will see an error on the `scratch output` tool window.
  • If you try to execute any code which contains a reference to android code you will see an error on the `scratch output` tool window. For example, if we add a Log statement in utility method then the scratch file won’t compile and will give the following error :

Exception in thread “main” java.lang.RuntimeException: Stub!
at android.util.Log.d(Log.java:96)

  • For example, if you add new utility file in your app module and before rebuilding the project you created a scratch file to test the behavior of methods in the utility file then you will get

java.lang.NoClassDefFoundError` on scratch output window

  • If you don’t find the `Scratch Output` Window then you can find it using (Press ⇧⌘A), start typing scratch file output and then pick the corresponding action.

Limitations

There are few obvious limitations of Kotlin scratch file:

  • We can’t use android components not even classes like Log which doesn’t require a device. Use print() instead of Android Log.
  • We will need to rebuild the module/app if we do any changes in the file which we are referring to in the scratch file.

That’s it for today. Happy Coding.

--

--

Shivam

Product Engineer @ Gojek. Likes to write on Productivity, Android App Development, Kotlin, Software Engineering, etc.