31

Hope you have worked with Live Templates which is given by Android by default.

Like:

Generate debug log statement: "logd"+TAB
Generate error log statement: "loge"+TAB
Generate info log statement: "logi"+TAB
Generate TAG declaration: "logt"+TAB
Generate parameter logging: "logm"+TAB
Generate method return log: "logr"+TAB

enter image description here

Which it is not available in KOTLIN?

Is it not available in Android Studio 3.0 Canary Version?

Community
  • 1
  • 1
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
  • Accepted answer as of writing this comment is not the best way to reuse templates. – dev Nov 22 '17 at 02:40
  • Seems like Android Studio's Kotlin "out-of-the-box" support is still in development – Ihor Klimov Dec 05 '17 at 13:59
  • 1
    Just created the templates as described by [Leo Droidcoder](https://stackoverflow.com/a/46152625/4540114) (@Leo Droidcoder Thanks!) and uploaded them to [Github](https://github.com/TobiasUhmann/android-studio-log-templates-for-kotlin). Simply download the .jar and import it in Android Studio via 'File > Import Settings...' – Tobias Uhmann May 12 '18 at 08:58

6 Answers6

38

Here is the step-by-step guide:

Firstly, Copy and paste AndroidLog templates to Kotlin (Just select them and use CMD+C, CMD+V (or Ctrl+C, Ctrl+V)
Secondly, You have to adjust them manually:

  1. logd (loge, logv and others) Select the logd item and press "Edit variables" enter image description here

Change expression to: kotlinMethodName() enter image description here

Also, remove ; from the end of the template, as you don't need it in Kotlin.

Now your method name will be shown correctly

  1. logt

This one is a bit trickier.
Solution 1: TAG = class name.

Template text :

private val TAG = "$className$"

Edit variables -> Expression:

groovyScript("_1.take(Math.min(23, _1.length()));", kotlinClassName())

Solution 2: TAG = file name (can be used inside Companion)

Template text :

private const val TAG = "$className$"

or:

companion object {
    private const val TAG = "$className$"
}

Edit variables -> Expression:

groovyScript("_1.take(Math.min(23, _1.length()));", fileNameWithoutExtension())
Leo DroidCoder
  • 14,527
  • 4
  • 62
  • 54
19

Yet not added log template in Kotlin Live templates section in Android Studio.

Settings -> Editor -> Live Templates -> Kotlin for kotlin templates.

kotlin templates

Settings -> Editor -> Live Templates -> AndroidLog for AndroidLog templates

AndroidLog  templates.

So you can't get the same AndroidLog templates in Kotlin code.

So now Question is How to use same Log Functions using templates in Kotlin?

Ans: You can add same Log templates (AndroidLog Templates) in Kotlin Templates section in Android studio as below example.

Kotlin Templates section

Then It will be available in your Kotlin code!

Kotlin loge

I hope in this way you can get an advantage of Log functions templates in Kotlin.

pRaNaY
  • 24,642
  • 24
  • 96
  • 146
  • Excellent and clear explanations. However, whatever template I add under kotlin, never does Android Studio make it work for me as it would with Java. Indeed, whenever I start writing `log` it suggests logarithm functions... though, there are no templates for those. I am running Android Studio 3.5. Thanks for the help ! I have tried with different names, no suggestions at all. And I also tried by restarting AS. – Mackovich Sep 15 '19 at 10:59
13

You don't need to copy paste anything as mentioned in the other answers. Locate the "Applicable in *" text in the Live Templates section in Android Studio - clicking on "Change" button gives a list of all scopes. Choose Kotlin (or one of it's child nodes) and apply. Note that some statements from Java will not work in Kotlin (like the ones using static keyword) - It's better to create a new template for those, rather than editing the existing one.

enter image description here

dev
  • 11,071
  • 22
  • 74
  • 122
7

Here is the repository which contains all the templates for Android-Kotlin.

All you need to do this is download the repository, copy the templates directory and paste in your Android Studio config folder.

The config folder for your Android Studio can be found at

Windows: C:\Users\%userName%\.AndroidStudio<version>\config\templates
Linux: ~AndroidStudio<version>/config/templates
macOS: ~/Library/Preferences/AndroidStudio<version>/templates
Atiq
  • 14,435
  • 6
  • 54
  • 69
0

This is the combination of all good answers by @pRaNaY @Leo and @Jaguar with bonus of TAG-free and variable-ready usage (no need to type "${}")

Template text: android.util.Log.d("$className$", "$methodName$ $$$contents$")

Applicable in Kotlin: top-level, statement, class, expression

variable expressions can be found in dropdown menu

here you can see whole picture

underoid
  • 429
  • 4
  • 11
0

For logt the above two answers works but additionally need to add "define" as "class"

enter image description here

Falgee
  • 76
  • 5