PySmali’s documentation

Welcome to the documentation for pysmali, a Python3 package designed for parsing, transforming and generating Smali source code files, as well as interpreting source code files. This documentation is intended to provide an overview of the package’s features, installation instructions, and usage examples to help you get started with using the package in your Python projects.

Using this library

Installation

How to install the python package locally or in a virtal environment.

Using the Smali API

Introduction into Smali and the provided Smali API.

Using the Smali-Emulator

Introduction into the provided Smali-Interpreter.

Supported dependencies

Supported project dependencies.

Examples

Reading Smali code
 1from smali import SmaliReader, SmaliWriter
 2
 3reader = SmaliReader(comments=False)
 4writer = SmaliWriter(reader)
 5
 6with open('example.smali', 'r', encoding='utf-8') as fp:
 7   source = fp.read()
 8
 9# The writer can be any instance of a class visitor
10reader.visit(source, writer)
11print(writer.code)
12# or
13print(str(writer))
Modifying Smali code
 1from smali import SmaliWriter, AccessType
 2
 3writer = SmaliWriter()
 4# Create the .class statement
 5writer.visit_class("Lcom/example/Car;", AccessType.PUBLIC + AccessType.ABSTRACT)
 6# Declare the super class
 7writer.visit_super("Ljava/lang/Object;")
 8# Visit the interface implementation
 9writer.visit_implements("Ljava/lang/Runnable")
10
11# Create the field id
12writer.visit_field("id", AccessType.PRIVATE, "I")
13
14# Create the method
15m_writer = writer.visit_method("run", AccessType.PUBLIC + AccessType.ABSTRACT, [], "V")
16m_writer.visit_end()
17
18# finish class creation
19writer.visit_end()
20source_code = writer.code
Execute Smali code
 1from smali.bridge import SmaliClass, SmaliObject, SmaliVM
 2
 3# Let's assume, the class' source code is stored here
 4source = ...
 5
 6vm = SmaliVM()
 7# Load and define the class (don't call the <clinit> method)
 8my_class: SmaliClass = vm.classloader.load_class(source, init=False)
 9
10# To manually initialize the class, call <clinit>
11my_class.clinit()
12
13# Instances can be created by providing the class object
14instance: SmaliObject = SmaliObject(my_class)
15
16# Initialization must be done separately:
17instance.init(...)
18
19# Methods are stored in our class object (not in the
20# actual instance)
21toString: SmaliMethod = my_class.method("toString")
22
23# The first argument of instance method must be the
24# object itself (on static methods, just use None)
25value = toString(instance)
26
27# The returned value behaves like a string
28print("Returned value:", value)

To execute Smali files from the cli, just use the ismali command that comes with installation of this module:

$ ismali
>>> vars
{'p0': <SmaliObject@20776587040>}
>>> const-string v1, "Hello World"
>>> vars
{'p0': <SmaliObject@20776587040>, 'v1': 'Hello World'}

Development

Contributing

How to contribute changes to the project.

Development guidelines

Guidelines the theme developers use for developing and testing changes.

Changelog

The package development changelog.

Indices and tables