---
title: "example-structures.smali"
author: "netxdiy"
date: "2012-02-10T15:54:00Z"
tags: []
slug: "example-structures-smali"
draft: false
lastmod: "2012-09-27T10:56:58Z"
id: 1686573
url: "https://netxdiy.iteye.com/blog/1686573"
---

转载自 **：http://androidcracking.blogspot.com/p/way-of-android-cracker-lessons.html（得![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)）**

**many people find this blog by searching for smali syntax examples and finding a previous post on**[example smali syntax](http://s11.proxycn.co/baidu.com.php?u=040efbd5208dOi8vYW5kcm9pZGNyYWNraW5nLmJsb2dzcG90LmNvbS8yMDEwLzA5L2V4YW1wbGVzbWFsaS5odG1s&b=15)**. since i originally hastily wrote it, i have learned more so i updated it as best i could for now. more helpfully, i worked out a syntax highlighter for smali using**[syntaxhighlighter](http://s11.proxycn.co/baidu.com.php?u=040efbd5208dOi8vYWxleGdvcmJhdGNoZXYuY29tL1N5bnRheEhpZ2hsaWdodGVyLw%3D%3D&b=15)**. i'll upload it**[somewhere official](http://s11.proxycn.co/baidu.com.php?u=040efbd5208dOi8vYWxleGdvcmJhdGNoZXYuY29tL2ZvcnVtcy92aWV3dG9waWMucGhwP2Y9MiZ0PTExOA%3D%3D&b=15)**when i'm finished so others can use it. because i know there are throngs of people just itching to have highlighted smali code!**

**so for the googler's out there trying to make sense of smali, here's example-structures.smali from**[lesson 1 from way of the android cracker](http://s11.proxycn.co/baidu.com.php?u=040efbd5208dOi8vYW5kcm9pZGNyYWNraW5nLmJsb2dzcG90LmNvbS8yMDEwLzEwL3dheS1vZi1hbmRyb2lkLWNyYWNrZXItMS5odG1s&b=15)**.**

.class publicLcom/lohan/crackme1/example;

.superLjava/lang/Object;

.source"example.java"

\# staticfields

.field privatestatic Counter:I

\# directmethods

\# all theconstructor does is set Counter to 0x10 (or 16)

.method publicconstructor &lt;init&gt;()V

.registers 2

.prologue

.line 11

invoke-direct {p0},Ljava/lang/Object;-&gt;&lt;init&gt;()V

const/16 v0, 0x10

sput v0,Lcom/lohan/crackme1/example;-&gt;Counter:I

return-void

.end method

.method publicstatic ArrayExample()V

.registers 4

.prologue

const/4 v3, 0x1

const/4 v2, 0x0

.line 50

const/4 v1, 0x5

new-array v0, v1, [Ljava/lang/String;

.line 52

.local v0, someArray:[Ljava/lang/String;

\# put value v1 inside array v0 at index v2(0x0)

const-string v1, "set value at index0"

aput-object v1, v0, v2

.line 53

\# put value v1 inside array v0 at index v3(0x1)

const-string v1, "index 1 has thisvalue"

aput-object v1, v0, v3

.line 55

\# store in v1 the value from array v0 atindex v2

aget-object v1, v0, v2

\# store in v2 the value from array v0 atindex v3

aget-object v2, v0, v3

\# compare two strings

invoke-virtual {v1, v2},Ljava/lang/String;-&gt;equals(Ljava/lang/Object;)Z

move-result v1

\# if equals() returns 0, it means they arenot equal, so just return

if-eqz v1, :cond\_1e

.line 57

sget-object v1,Ljava/lang/System;-&gt;out:Ljava/io/PrintStream;

const-string v2, "array at index 0 = 1(wont happen)"

invoke-virtual {v1, v2},Ljava/io/PrintStream;-&gt;println(Ljava/lang/String;)V

.line 59

:cond\_1e

return-void

.end method

\# loop from 0to Counter

\# just a fewlines in java

.method publicstatic LoopExample()V

.registers 4

.prologue

.line 15

const/4 v0, 0x0

.local v0, i:I

:goto\_1

sget v1,Lcom/lohan/crackme1/example;-&gt;Counter:I

if-lt v0, v1, :cond\_6

.line 17

return-void

.line 16

:cond\_6

sget-object v1,Ljava/lang/System;-&gt;out:Ljava/io/PrintStream;

new-instance v2, Ljava/lang/StringBuilder;

const-string v3, "current val forloop: "

invoke-direct {v2, v3},Ljava/lang/StringBuilder;-&gt;&lt;init&gt;(Ljava/lang/String;)V

invoke-virtual {v2, v0},Ljava/lang/StringBuilder;-&gt;append(I)Ljava/lang/StringBuilder;

move-result-object v2

invoke-virtual {v2},Ljava/lang/StringBuilder;-&gt;toString()Ljava/lang/String;

move-result-object v2

invoke-virtual {v1, v2},Ljava/io/PrintStream;-&gt;println(Ljava/lang/String;)V

.line 15

add-int/lit8 v0, v0, 0x1

goto :goto\_1

.end method

.method publicstatic SwitchExample()V

.registers 3

.prologue

.line 21

const/16 v0, 0x2a

.line 22

.local v0, val:I

\# begin the switch

\# look down at .sparse\_switch directive

sparse-switch v0, :sswitch\_data\_2e

\# switch default just passes through tohere

.line 27

sget-object v1,Ljava/lang/System;-&gt;out:Ljava/io/PrintStream;

const-string v2, "invalid value"

invoke-virtual {v1, v2},Ljava/io/PrintStream;-&gt;println(Ljava/lang/String;)V

.line 29

:goto\_c

return-void

.line 23

:sswitch\_d # if v0 is 1, we'll be here

sget-object v1,Ljava/lang/System;-&gt;out:Ljava/io/PrintStream;

const-string v2, "val 1"

invoke-virtual {v1, v2},Ljava/io/PrintStream;-&gt;println(Ljava/lang/String;)V

goto :goto\_c

.line 24

:sswitch\_15 # if v0 is 2

sget-object v1,Ljava/lang/System;-&gt;out:Ljava/io/PrintStream;

const-string v2, "val 2"

invoke-virtual {v1, v2},Ljava/io/PrintStream;-&gt;println(Ljava/lang/String;)V

goto :goto\_c

.line 25

:sswitch\_1d # if v0 is 42

sget-object v1,Ljava/lang/System;-&gt;out:Ljava/io/PrintStream;

const-string v2, "val 42"

invoke-virtual {v1, v2}, Ljava/io/PrintStream;-&gt;println(Ljava/lang/String;)V

goto :goto\_c

.line 26

:sswitch\_25 # if v0 is 5

sget-object v1,Ljava/lang/System;-&gt;out:Ljava/io/PrintStream;

const-string v2, "val 5"

invoke-virtual {v1, v2},Ljava/io/PrintStream;-&gt;println(Ljava/lang/String;)V

goto :goto\_c

.line 22

nop

:sswitch\_data\_2e

\# if v0 is 0x1 goto :sswitch\_d

\# if 0x2 :sswitch\_15

\# and so on...

.sparse-switch

0x1 -&gt; :sswitch\_d # 1

0x2 -&gt; :sswitch\_15 # 2

0x5 -&gt; :sswitch\_25 # 5

0x2a -&gt; :sswitch\_1d # 42

.end sparse-switch

.end method

.method publicstatic TryCatchExample()V

.registers 8

.prologue

const-string v7, ": "

.line 33

const-string v3, "google.com"

.line 36

.local v3, urlStr:Ljava/lang/String;

\# begin try here

:try\_start\_4

new-instance v2, Ljava/net/URL;

invoke-direct {v2, v3},Ljava/net/URL;-&gt;&lt;init&gt;(Ljava/lang/String;)V

.line 37

.local v2, url:Ljava/net/URL;

invoke-virtual {v2},Ljava/net/URL;-&gt;openStream()Ljava/io/InputStream;

move-result-object v1

.line 38

.local v1, is:Ljava/io/InputStream;

invoke-virtual {v1},Ljava/io/InputStream;-&gt;close()V

:try\_end\_10

\# end of the try

\# if there was a MalformedURLException,goto :catch\_11

.catch Ljava/net/MalformedURLException;{:try\_start\_4 .. :try\_end\_10} :catch\_11

\# IOException goes to :catch\_36

.catch Ljava/io/IOException; {:try\_start\_4.. :try\_end\_10} :catch\_36

\# ifNO exception, just pass through:

.line 46

.end local v1 #is:Ljava/io/InputStream;

.end local v2 #url:Ljava/net/URL;

:goto\_10

return-void

.line 39

:catch\_11

\# move the exception to v4, then to v0

\# allof the code below is what happens with an exception

move-exception v4

move-object v0, v4

.line 41

.local v0,e:Ljava/net/MalformedURLException;

sget-object v4,Ljava/lang/System;-&gt;out:Ljava/io/PrintStream;

new-instance v5, Ljava/lang/StringBuilder;

const-string v6, "Invalid URL "

invoke-direct {v5, v6},Ljava/lang/StringBuilder;-&gt;&lt;init&gt;(Ljava/lang/String;)V

invoke-virtual {v5, v3},Ljava/lang/StringBuilder;-&gt;append(Ljava/lang/String;)Ljava/lang/StringBuilder;

move-result-object v5

const-string v6, ": "

invoke-virtual {v5, v7},Ljava/lang/StringBuilder;-&gt;append(Ljava/lang/String;)Ljava/lang/StringBuilder;

move-result-object v5

invoke-virtual {v0},Ljava/net/MalformedURLException;-&gt;getMessage()Ljava/lang/String;

move-result-object v6

invoke-virtual {v5, v6},Ljava/lang/StringBuilder;-&gt;append(Ljava/lang/String;)Ljava/lang/StringBuilder;

move-result-object v5

invoke-virtual {v5},Ljava/lang/StringBuilder;-&gt;toString()Ljava/lang/String;

move-result-object v5

invoke-virtual {v4, v5},Ljava/io/PrintStream;-&gt;println(Ljava/lang/String;)V

goto :goto\_10

.line 42

.end local v0 #e:Ljava/net/MalformedURLException;

\# end exception code

\# begin exception code

:catch\_36

move-exception v4

move-object v0, v4

.line 44

.local v0, e:Ljava/io/IOException;

sget-object v4,Ljava/lang/System;-&gt;out:Ljava/io/PrintStream;

new-instance v5, Ljava/lang/StringBuilder;

const-string v6, "Unable to execute"

invoke-direct {v5, v6},Ljava/lang/StringBuilder;-&gt;&lt;init&gt;(Ljava/lang/String;)V

invoke-virtual {v5, v3},Ljava/lang/StringBuilder;-&gt;append(Ljava/lang/String;)Ljava/lang/StringBuilder;

move-result-object v5

const-string v6, ": "

invoke-virtual {v5, v7},Ljava/lang/StringBuilder;-&gt;append(Ljava/lang/String;)Ljava/lang/StringBuilder;

move-result-object v5

invoke-virtual {v0},Ljava/io/IOException;-&gt;getMessage()Ljava/lang/String;

move-result-object v6

invoke-virtual {v5, v6},Ljava/lang/StringBuilder;-&gt;append(Ljava/lang/String;)Ljava/lang/StringBuilder;

move-result-object v5

invoke-virtual {v5},Ljava/lang/StringBuilder;-&gt;toString()Ljava/lang/String;

move-result-object v5

invoke-virtual {v4, v5},Ljava/io/PrintStream;-&gt;println(Ljava/lang/String;)V

goto :goto\_10

\# end exception code

.end method