[LG Optimus Black] dxdiag32:
Hi guys,i translated the key part of this guide,untill now,this guide is not completed ,if it updated ,i'll update this thread too :P

and until now,our MIUI PORT TEAM have these guys:

me,gabwerkz,redy2006

N00BY0815 and SquaDrive after read this post and if you want to join in plz let me know :D anyone else wants to join in are welcomed:D

===========================================:D:Dline

1.A Sample For Smali

imagane that there's a Hello worlk application,we install the apk and open it,we can see a blackscreen and at the first line written:Hello world!

ok,this is a simple app.now we decompile it.

===================

we get a folder,then find the HelloActivity.smali,there's its content:

.class public Lcom/example/android/helloactivity/HelloActivity;
.super Landroid/app/Activity;
.source "HelloActivity.java"

# direct methods
.method public constructor <init>()V
.locals 0

.prologue
.line 27
invoke-direct {p0}, Landroid/app/Activity;-><init>()V

return-void
.end method

# virtual methods
.method public onCreate(Landroid/os/Bundle;)V
.locals 2
.parameter "savedInstanceState"

.prologue
.line 33
invoke-super {p0, p1}, Landroid/app/Activity;->onCreate(Landroid/os/Bundle;)V

.line 37
const/high16 v1, 0x7f03

invoke-virtual {p0, v1},
Lcom/example/android/helloactivity/HelloActivity;->setContentView(I)V

.line 38
const/high16 v1, 0x7f05

invoke-virtual {p0, v1},
Lcom/example/android/helloactivity/HelloActivity;->findViewById(I)Landroid/view/View;

move-result-object v0

check-cast v0, Landroid/widget/TextView;

.line 39
.local v0, txtView:android/widget/TextView;
const/high16 v1, 0x7f04

invoke-virtual {v0, v1}, Landroid/widget/TextView;->setText(I)V

.line 40
return-void
.end method

"#" refers to notes.

begin with a dot named "annotations".

".line" means line number,it mainly used for debug.

.metho and .end method means a method's starting and endding.

more code plz goto http://pallergabor.uw.hu/androidblog/dalvik_opcodes.html

now we want to change the "Hello,world!" into "Happy,Craker!",what should we do?

in this smali

.lne 39 is

.local v0, txtView:android/widget/TextView;
const/high16 v1, 0x7f04

invoke-virtual {v0, v1}, Landroid/widget/TextView;->setText(I)V

and actually the source code is

txtView.setText(R.string.hello_activity_text_text)

here we can see it define a textview with R.string.hello_activity_text_text ,and i guess the string "Hello world!" is in it.

now we could not change the string,but we can replace it by change it directly

but how we do it in smali?

here we go

.line 39
.local v0, txtView:android/widget/TextView;
const-string v1, "Happy, Cracker!"
invoke-virtual {v0, v1}, Landroid/widget/TextView;->setText(Ljava/lang/CharSequence;)V

ps.i can't explane it more clearly,but we can see we replace the id with our string.

==========================

2.Porting MIUI Framework

Before starting i should tell us :all porting is based on deodexed files.and there's a stock android rom and miui rom to show you a example to understand how we make the porting work.

the main idea is change the smali code to port it.cause we can't get the souce code of miui,and by this way we can port it to our phone too.

the three files we should touch is :framework.jar ,android.policy.jar andservices.jar.they are the "core" of android system

there's an attach file download and open it we can see:

porting-miui/

|-----------------android

|------------framework.jar

|------------services.jar

|------------android.policy.jar

|------------------miui

|----------framework.jar

|----------services.jar

|----------android.policy.jar

|-----------framework-res/

|-----------framework-miui-res.apk

the android folder is from stock android ,miui folder is miui's files,and we need your phone's files here too.and in this guide we assume your phone is I9100.now we should decompile all of them and compare,to "patch" miui things to your phone.

that means ,we need to compare stock files with our files,compare stock files with miui files

i.porting resources

decompile framework-res.apk,all the resources in miui we need porting to our rom,so copy them to your compiled framework-res folder and then recompile it.

framework-miui-res.apk is a resouces package,all the miui apps need it.in our rom we can find RES_cappuccino.apk,RES_sui.apk and RES_model.apk.

usually miui's resource's id is started with 0x03,and the stock rom has two resouce package,framework-res.apk is started with 0x01 and another is started with 0x02.so if your phone has more than two package,you need contact with us.in the future we'll considering make the resouce's id started with 0x06 in miui.

ii.after we finish the decompile we can use the script rmline.sh to delete all the lines started with .line to make us more easier to compare the difference with two smali code.but backup the original decomplied files first plz.we can debug from it.

then,if you based on linux,you'd try meld to compare,if you are on windows,use beyoun compare

between stock and miui,we can see there's a lot of new classes which started with Miui,and a new miui folder,just copy those new files and folders to our stock folders(with .line)

in the attach file,there's a change-list file,which list what miui did change.maybe it's a little different with what we compared by ourselves,but it's nop (dummy instruction),it caused by apktool,so we don't need to care it,just compare listed files.

there are 3 solutions

1.ex. ActivityThread.smali,miui changed the method "getTopLevelResources",but i9100's and stock is the same,this is the easiest situation and we can replace the miui code into our files happily :P

2.also in ActivityThread.smali,miui changed the another method "applyConfigurationToResourcesLocked",and after comparation,we can see miui changed this method,so i9100 does.then what should we do?let's see the miui's code first

.method final applyConfigurationToResourcesLocked(Landroid/content/res/Configuration;)Z
invoke-virtual {v5, p1}, Landroid/content/res/Configuration;->updateFrom(Landroid/content/res/Configuration;)I
move-result v0
.local v0, changes:I
invoke-static {v0}, Landroid/app/MiuiThemeHelper;->handleExtraConfigurationChanges(I)V
invoke-virtual {p0, v7}, Landroid/app/ActivityThread;->getDisplayMetricsLocked(Z)Landroid/util/DisplayMetrics;
move-result-object v1
.local v1, dm:Landroid/util/DisplayMetrics;

at line 5 is what miui changed.before that,we should know some rule about smali

all the local variable is started with "v"

.locals 8 means this method use 8 local variables.

all the parameters are started by "p".and local variable and parameters started from 0.for all the nonstatic method,p0 means itself,i.e "this" pointer.

here we can see miui added a new static method,the code like this we call it linear code.it as one entry and one exit.and in compiler it called basic block.

so we just need to copy this block and paste into 9100's files at the same position,and we done.

3.for example,in Resources.smali,miui changed the "loadDrawable" method.here's the code

.method loadDrawable(Landroid/util/TypedValue;I)Landroid/graphics/drawable/Drawable;
.end local v8 #e:Ljava/lang/Exception;
.end local v13 #rnf:Landroid/content/res/Resources$NotFoundException;
:cond_6

invoke-virtual/range {p0 .. p2},
Landroid/content/res/Resources;->loadOverlayDrawable(Landroid/util/TypedValue;I)Landroid/graphics/drawable/Drawable;
move-result-object v6
if-nez v6, :cond_1

:try_start_1
move-object/from16 v0, p0

from line 6 to line 9 is what miui added.then we compare 9100 and stock android,we can find it's totally different.then how should we do now?

the key is find the added code's entry and exit.

at line 4 we see a ":cond_6",it says there should be a goto command to this :cond_6.so we got to find where used the :cond_6 and we finally got this:

const-string v15, ".xml"
invoke-virtual {v9, v15}, Ljava/lang/String;->endsWith(Ljava/lang/String;)Z
move-result v15
if-eqz v15, :cond_6

read this code block carefully,and seems it is check if the string "v9" is ending with ".xml",if not,goto :cond_6 .ok ,let's go to see 9100's framework.

so we search ".xml" in the loadDrable method.find this:

const-string v17, ".xml"
move-object v0, v10
move-object/from16 v1, v17
invoke-virtual {v0, v1}, Ljava/lang/String;->endsWith(Ljava/lang/String;)Z
move-result v17
if-eqz v17, :cond_b

the logic is same as which in miui hmm?so goto :cond_b ,what we can see is totally same as miui's :cond_6 right? so we are sure this is where miui changed

look at the exit,miui has two exit point

one is if-nez v6, :cond_1 ,if so,goto :cond_1,if not,go on.so let's see what the :cond_1 did

here's cond_1's code

:cond_1
:goto_1
if-eqz v6, :cond_2
move-object/from16 v0, p1
iget v0, v0, Landroid/util/TypedValue;->changingConfigurations:I

and in 9100's file we can find this

:cond_1
:goto_1
if-eqz v7, :cond_2
move-object/from16 v0, p1
iget v0, v0, Landroid/util/TypedValue;->changingConfigurations:I

this time it check the v7's value,so we should add this into 9100

invoke-virtual/range {p0 .. p2},
Landroid/content/res/Resources;->loadOverlayDrawable(Landroid/util/TypedValue;I)Landroid/graphics/drawable/Drawable;
move-result-object v7
if-nez v7, :cond_1

a little dizzy hmm?take a break time and go back:D

iii.and the last we'll talk about inner class.

every inner class have a separete smali file.

e. ActivityThread$1.smali

if it is a Anonymous class,the name should be "outer class+$+number",else it should be "outer class+$+inner class"as its name.

if a inner class use outer class's privte method,the compiler will auto fill a static function like this:

public class Hello {
public class A {
void func() {
setup();
}
}
private void setup() {
}

we use the outer class's setup method in inner class A's "func" method.and we'll get the smali code:

part of Hello$A.smali

# virtual methods
.method func()V
.locals 1

.prologue
.line 5
iget-object v0, p0, LHello$A;->this$0:LHello;

#calls: LHello;->setup()V
invoke-static {v0}, LHello;->access$000(LHello;)V

.line 6
return-void
.end method

part of Hello.smali

.method static synthetic access$000(LHello;)V
.locals 0
.parameter

.prologue
.line 1
invoke-direct {p0}, LHello;->setup()V

return-void
.end method

we can see the compiler auto made a access$000 method,if we use a outer class's private method in a more complicate inner class,it'll made a new method,but every class may have various new class name,it changes a lot .compare it carefully,find the private method and find a name that you ever seened.

iiii.Finally there are some advices:

1.careful,find where to add miui's code

2.notice the local variable number

3.step by step,if you done a part of port,recompile it to see if it work

4.find a problem is not neccessary,use adb logcat and find what caused the problem.

5.more practice and you'll handle the smali code :P

==================================

because of my bad english ,if you can understand what i mean,plz point me out and i'll correct it as i can:p

if you think this post may help you,press the thanks button:D

here's the attachment：

http://www.multiupload.com/I33A07PRTF