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
Is it not available in Android Studio 3.0 Canary Version?
Live templates Use live templates to insert common constructs into your code, such as loops, conditions, various declarations, or print statements. To expand a code snippet, type the corresponding template abbreviation and press Tab . Keep pressing Tab to jump from one variable in the template to the next one.
Import live template configurationChoose File | Manage IDE Settings | Import Settings from the menu. Specify the path to the archive with the exported live template configuration. In the Import Settings dialog, select the Live templates checkbox and click OK.
Press Ctrl+Alt+S to open the IDE settings and select Editor | Live Templates. Select the template group where you want to create a new live template (for example, other). If you do not select a template group, the live template will be added to the user group. and select Live Template.
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:
Change expression to: kotlinMethodName()
Also, remove ;
from the end of the template, as you don't need it in Kotlin.
Now your method name will be shown correctly
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())
Yet not added log template in Kotlin Live templates section in Android Studio.
Settings -> Editor -> Live Templates -> Kotlin for kotlin templates.
Settings -> Editor -> Live Templates -> AndroidLog for 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.
Then It will be available in your Kotlin code!
I hope in this way you can get an advantage of Log functions templates in Kotlin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With