Android Studio - Live Template - @StringDef, @IntDef
Today I created a useful Android Studio live template for auto generating @StringDef and @IntDef in java source code, that I would like to share with you. My team does not use Enums in our Java android code for efficiency reasons. Instead constants are typically used in their place. Android Typedef Annotations are great for making sure the use of the constants stay consistent in your code base. They can be used to annotate function parameters to restrict the parameter to only accept Stings or ints that are of that Typedef.
The Typedef Annotations can be a little bit on the lengthy side code wise, so having a live template to quickly generate them is helpful.
The idea is we can write our constants, and then just generate the Typedef annotation interface based on those constants.
Goto preferences in your IntelliJ application:
In your preferences select Live Templates under Editor:
Expand Android in the Live Templates window and click the + sign to add a new live template
Select Live Template:
Now first let’s create our @StringDef template. Fill out your abbreviation to be stringdef. This can be another value of your liking if you wish. Typically this is what you would type to start your live template.
( note: discussed later, this template will be triggered via the option+command+j hotkey ).
Next let’s fill out the Template text with the following code:
$SELECTION$ $END$
@Retention(RetentionPolicy.SOURCE) @StringDef({$enums$}) public @interface $name$ {}
The $SELECTION$ variable indicates that we would like to capture the current selected text in the editor. This allows us to use this selected value to generate our enum names inside of @StringDef. We will be selecting our constants that we want add an @StringDef too.
$END$ Indicates that our template code to be inserted comes after our selection.
$enums$ is a placeholder for the enum values we will generate, and $name$ will be filled out by us to name our new interface for restricting input for these enums.
Next you will notice some red text saying: No applicable context. Define
Click on Define.
Then select Java.
We are almost there! We just need to generate the enums inside @StringDef. Click on Edit variables.
We are going to use the Expression field to generate the value for our enums variable.
A little bit of groovy code will give us the behaviour we want:
groovyScript("_1.findAll(/String ([A-Za-z0-9_]+)/) { match, constant -> constant }.join(', '); ", SELECTION)
Paste this code into the Expression field for the enums variable, and select Skip if defined for enums.
Now click apply on your preferences pane and you are ready to try out this template!
To use the template you need to select some enums for it to generate an interface for. Then press option+command+l (control+alt+j) for the surround with live template action. If your hotkeys are different you can find this command via shift+command+a (a search for commands by name). After pressing option+command+j, select stringdef. Type an interface name and press tab. Vola! You are done.
Repeat these steps using what I taught you here to create a live template for @IntDef. You’ll need to replace @StringDef with @IntDef and in the groovy snippet in the regular expression, String with int.
Note: I ran into a bug while trying to write this blog post. I was unable to paste text into the Expression field and have it stay. If you run into this problem you can add the live template via an XML file in your settings folder for the IDE. Using this handy github repo: https://github.com/keyboardsurfer/idea-live-templates I was able to figure out where to put the live template file. For me it was in ~/Library/Preferences/AndroidStudio2.2/templates. The version number of Android Studio may be different for you.
Here is this live template as an XML file for importing: