You’re almost there — sign up to start building in Notion today.
Sign up or login
Classes & Objects In Smali

Classes & Objects In Smali

In programming, a class is a blueprint or template for creating objects that share similar characteristics and behaviors. It defines a set of attributes (properties) and methods (functions) that the objects of that class will have.
Think of a class as a recipe for making a specific type of object, like a cookie cutter for making cookies. The class describes the shape, size, and ingredients of the cookies, as well as the steps needed to bake them. When you use the class to create an object, it's like using the cookie cutter to cut out a cookie from the dough.
Each object created from the same class will have the same properties and methods, but the specific values of those properties may be different for each object.
Example:

Class in smali

.class public MyClass .super java/lang/Object # Fields .field private myField:I # Constructor .method public constructor <init>()V .registers 1 invoke-direct {p0}, Ljava/lang/Object;-><init>()V return-void .end method # Methods .method public setMyField(I)V .registers 2 .param p1, "value" # the value to set myField to iput p1, p0, LMyClass;->myField:I return-void .end method .method public getMyField()I .registers 1 iget p0, LMyClass;->myField:I return p0 .end method
In this example, we define a class called 
MyClass
 that extends the 
java/lang/Object
 class (which is the base class for all objects in Java/Smali). The class has a single field called 
myField
 of type 
int
, a constructor that calls the superclass constructor, and two methods, 
setMyField
 and 
getMyField
, that respectively set and retrieve the value of 
myField
.

Object in smali

# Allocate memory for the object # Getting referance of MyClass and store on v0 new-instance v0, Llab.seczone64/MyClass; # Invoke the constructor of the class and creating object invoke-direct {v0}, lab.seczone64/MyClass;->init # Set the value of the myField field to 42 const/16 v1, 42 iput v1, v0, lab.seczone64/MyClass;->myField:I
In this example, we allocate memory for the object of 
MyClass
 and store it in register 
v0
. We then invoke the constructor of 
MyClass
 using the 
invoke-direct
 instruction, passing 
v0
 as the 
this
 parameter. Finally, we set the value of the 
myField
 field to 
42
 using the 
iput
 instruction.

Calling an method in class

Consider this example. We have an Interface named
Human
and a class with name
Person
which implement that interface.
package lab.seczone64.classedandobjectsinsmali.models; import lab.seczone64.classedandobjectsinsmali.interfaces.Human; public class Person implements Human { private String name; private int age; private String gender; public Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } @Override public String getName() { return name; } @Override public void setName(String name) { this.name = name; } @Override public int getAge() { return age; } @Override public void setAge(int age) { this.age = age; } @Override public String getGender() { return gender; } @Override public void setGender(String gender) { this.gender = gender; } @Override public void eat(String food) { System.out.println(name + " is eating " + food); } @Override public void sleep(int hours) { System.out.println(name + " is sleeping for " + hours + " hours"); } @Override public void talk(String message) { System.out.println(name + " says: " + message); } }
For create
Person
object and calling methods:
.method protected onCreate(Landroid/os/Bundle;)V .locals 4 .param p1, "savedInstanceState" # Landroid/os/Bundle; .line 14 invoke-super {p0, p1}, Landroidx/appcompat/app/AppCompatActivity;->onCreate(Landroid/os/Bundle;)V .line 15 sget v0, Llab/seczone64/classedandobjectsinsmali/R$layout;->activity_main:I invoke-virtual {p0, v0}, Llab/seczone64/classedandobjectsinsmali/MainActivity;->setContentView(I)V .line 17 new-instance v0, Llab/seczone64/classedandobjectsinsmali/models/Person; const/16 v1, 0x1a const-string v2, "male" const-string v3, "Mohammad Hossein Ashofte Yazdi" invoke-direct {v0, v3, v1, v2}, Llab/seczone64/classedandobjectsinsmali/models/Person;-><init>(Ljava/lang/String;ILjava/lang/String;)V .line 23 .local v0, "mHossein":Llab/seczone64/classedandobjectsinsmali/models/Person; const-string v1, "Pizza" invoke-virtual {v0, v1}, Llab/seczone64/classedandobjectsinsmali/models/Person;->eat(Ljava/lang/String;)V .line 24 const/4 v1, 0x6 invoke-virtual {v0, v1}, Llab/seczone64/classedandobjectsinsmali/models/Person;->sleep(I)V .line 25 const-string v1, "Hi there." invoke-virtual {v0, v1}, Llab/seczone64/classedandobjectsinsmali/models/Person;->talk(Ljava/lang/String;)V .line 26 return-void .end method

Static Method

In Java, a static method is a method that belongs to a class rather than an instance of the class. This means you can call the method using the class name, without needing to create an object of the class.
How to define static method in
smali
:
.class public final LMathUtils; .super Ljava/lang/Object; .method public static add(II)I .registers 3 .param p0, "a" # I .param p1, "b" # I add-int/2addr v0, p0 add-int/2addr v0, p1 return v0 .end method
You can this method like this:
.class public LMain; .super Ljava/lang/Object; .method public static main([Ljava/lang/String;)V .registers 3 const/4 v0, 2 const/4 v1, 3 invokestatic {v0, v1}, LMathUtils;->add(II)I move-result v0 sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream; invoke-virtual {v1, v0}, Ljava/io/PrintStream;->println(I)V return-void .end method

Casting Integer To String

.method public myMethod(I)V .registers 3 # Convert the integer to a string const-string v1, "" invoke-static {p1}, Ljava/lang/Integer;->toString(I)Ljava/lang/String; move-result-object v1 # Pass the string as a parameter invoke-static {v1}, Lcom/example/MyClass;->myMethod(Ljava/lang/String;)V return-void .end method

Tasks

Find out the encrypted text in
lab.seczone64.xorcryptography
app.
Find secret of
AES Crypto
app.
Solution

Tasks

Patch
spacepang
app and delete file-rate.
Solution
Patch
spacepang
app and don’t let user fire blocked.