Working with smali in VTS

Whether you are a hardened smali freak or just setting out on your first serious project VTS has a wide range of features designed to make working with smali much easier.  From the easy to follow colour-coded formatting in the smali display, to quick jump features, tooltips and Help reference VTS has a formidable array of tools in its arsenal to help you do battle with smali.

The aim of this guide is not to teach you how to code in smali – there a plenty of excellent resources online to help you with that – it is merely here to introduce you to some of the key smali features in VTS and how to get the most out of them. We can assume by this point that you are familiar with using VTS, so we start from the point where we already have our solution set up and the various files we’ll be working with correctly imported.

 

Smali Formatting

The first thing to get a feel for is how smali code is displayed in VTS.  When you open a smali file you will immediately notice the formatting and colour-coding has been designed to make it easy to scan through the code and ‘see’ what is going on.  To give you an idea what’s going on some of the colour coding used is as follows:

Comments

Strings

Variables

Keywords (eg synthetic, static, public, const…)

IfElse (eg if-eq, if-nez…)

Hex Numbers

Classes

ClassPath

ClassProperty

ClassMethod

ParameterType

Goto

Full details of the formatting used can be found in the SmaliSyntax.xml file in the Program Files directory for VTS in the Resources folder.

Smali formatting

 

Smali Help

There are couple of really useful Help features in VTS which give you a much clearer understanding of what the code you are staring at is actually doing.  VTS has a built in Smali Help reference accessed from the Help tab:

Smali Help

 

This is a really useful aid for anyone new to smali coding to get a better understanding about how smali works, and also as a reference tool for more experienced users.  In addition to the main Help function VTS also gives you comprehensive code explanations directly in the main code window.  Simply hover your mouse pointer over various items of code and you’ll get ToolTip pop-ups explaining various elements:

Smali ToolTips

 

Resource references in smali code

And while we’re on the subject of ToolTips there is another really cool feature when you are working with smali in VTS.  Smali uses hex codes to reference items from public.xml.  When you hover over these hex codes VTS tries to resolve them and shows you a tooltip with information about the reference.

So when you hover over a hex code that calls a string reference it also shows you the public.xml entry and the actual string value of the referenced item (if possible):

Smali String ToolTip

If you hover over a colour hex code in smali VTS will show you a little preview of the actual colour value:

Smali Colour ToolTip

And if you hover over a hex code that references a drawable resource it will preview the actual image name:

Smali Drawable ToolTip

At the bottom of all of these ToolTips you will notice instructions for another neat feature – CTRL + Left Click to jump to the reference in public.xml.

Smali Methods & Smali Fields windows

While we are on the subject of clicking for quick access, another really useful way of navigating around your smali code is to make use of the Smali Methods and Smali Fields windows which are available whenever you have a smali file open:

Smali MethodsSmali Fields

With either of these windows open you can simply double click on any method or field name and you will automatically jump to the relevant line in the smali code.

Jumping within the code

You can also jump around your code by simply holding ctrl and left clicking on cond_xxx,  goto_xxx, pswitch_xxx  and so on to jump directly to the target: 

Smali code jumpSmali code jumped

The reference and target in this example are only a few lines apart, however code jumping is really useful when you are working in large smali files and the two may be hundreds of lines apart.

Code highlighting

If you drag over a section of code VTS will automatically highlight identical sections of code within the file, which makes it really easy for example to track what is happening to a specific element as a method progresses, or for working backwards to see where that element is coming from:

Smali code highlighting

 

Fold/Unfold All

Before we get on to using the Search features another handy function is the Fold/Unfold All option on the Home tab.  This allows you to quickly collapse all methods and fields. You can then quickly jump to the method you want to edit (using the Smali Methods window to jump) and then expand and work with that method without the distraction of the surrounding code!

FoldUnfold

Search Features

VTS comes equipped with a comprehensive set of search tools to help you find what you are looking for.  These are accessed from the Home tab:

Search

Quick Find provides a quick and easy way to search within the current file.  Simply enter what you are looking for in the text box and click on Quick Find and VTS will jump to and highlight the first instance in the file.  Click on Quick Find again and VTS will jump to the next instance and so on:

Quickfind

If you need to do a more specific or more extensive search then clicking on the Find button will bring up the main search window with Find, Find & Replace and Find in Files tabs (if you are used to working with Notepad++ this will be very familiar to you).

FindFindReplace

 

Perhaps the most useful search function is the Find in Files tab, which allows you to search through your entire project, solution or beyond.  This is useful if you are starting on an new mod and don’t want to have to wade through endless code searching for a starting point.  Say for instance you want to add a new item to your Power Menu – you could start by decompiling framework-res and android.policy from a ROM you know has the mod, then looking up strings.xml for one of the text string values that appears in the menu (eg ‘Boot straight to recovery’):

SearchStrings

 

You’d then copy the string name (in this example global_action_recovery_desc), open public.xml, paste the string name into the QuickFind box and click QuickFind to find the resource ID:

SearchPublic

 

You would then copy the ID (in this instance without any leading zeros as they are not shown in smali), bring up the Find window and enter the ID in the text box on the Find in Files tab.  Then select the dropdown in the Search Directory box and select the path for the decompiled android.policy:

FindInFiles

 

Click on the Find All button and VTS will then search the entire directory for matches.  You will see the search progress in the Tasks panel, and when finished the Search Results panel will display any matches.  You can drill down in the search window until you find a result you want to look at further:

FindAll

 

Double click on the chosen result in the search window and VTS will open the relevant file, jump you straight to the relevant line in the code, and even highlight it so you can’t miss it!

SearchSuccess

Using Toggles

You will often find when looking through smali code that you find something you think might be relevant but still want to keep looking.  How many times have you carried on searching and then not been able to find that original section of code?  Well, VTS has this covered as well with the option to ‘toggle’ lines of code.  This essentially works like bookmarking lines and then being able to quickly jump backwards and forwards between toggled lines without having to search again or remember line numbers.

Simply click on the line of code you want to mark, and then from the Navigation tab click on Toggle Line Marker.  Do this as many times as you like, and then use the other buttons on the tab to jump between the bookmarked lines.  Once you are done you can simply click on Remove All Markers.
Toggles

 

Code Snippets

If you have a mod which you need to port every time a new base comes out then the Code Snippets feature in VTS can come in very handy.  It basically allows you to store your frequently used blocks of code (individual methods, etc) in individual snippets, which can then be dropped in to the files you are editing without having to first open a source file, then copy and paste.

To create a new snippet all you need to do it highlight the section of code you want to store, and then either right click on it to bring up the context menu and select Create Snippet from selection, or click on the Smali document tab and click the New from selection button there.  You will then be prompted to name your new snippet before saving for later use.

SnippetNew

 

To then make of any of your saved snippets simply open the smali file you want to add the code to and navigate to the line where you want to add the code.  Then from the Smali Document tab simply click on Paste Snippet and from the dropdown list that appears select the snippet you want to drop in:

SnippetPaste

Comment/Uncomment

Often when working with code you will want to try disabling something without actually deleting the code entirely just in case you need to quickly revert your changes.  This is made easy by using the Comment/Uncomment buttons on the Smali Document tab. Simply highlight the section of code you want to ‘disable’ and click on Comment Selection:

CommentCommented

 

And if you do need to ‘reactivate’ the code simply highlight it and click on Uncomment Selection.

Viewing Java Source

If you are struggling to understand exactly what is going on in a piece of smali code it sometimes helps to look at the original Java code.  To do this you need to have selected the Generate Java Source option on the Import tab when you were creating your project:

GenerateJava

 

If you selected that option you can view the Java source code for the current open smali file by clicking on the Smali Document tab, and selecting View Java Source:

JavaSourceJavaSample

NB – You can only view the source code in VTS. It is not possible to edit the java source code and recompile from there.  This feature is purely to help give you a clearer understanding of what is going on in the smali code.