Modifying existing methods in Smali

whitfin picture whitfin · Aug 27, 2013 · Viewed 11.4k times · Source

I've been looking at a simple method in smali (coming from Android here), and any I was wondering how I'd do something simple to it, such as return false. I've been trying to understand it from comparing with the Java counterparts to some examples and I think I get how most of it works, but any changes I make don't seem to go well.

Currently, in Java the method is:

public static boolean isEnabled(){
    return com.example.test.isEnabled();
}

and in smali it's:

.method public static isEnabled()Z
    .registers 1

    .prologue
    .line 3714
    invoke-static {}, Lcom/example/test;->isEnabled()Z

    move-result v0

    return v0
.end method

In the context above, how would I modify the method to just do the equivalent of Java return false;? From what I've been able to see, 0x0 is false, but just returning that causing issue when compiling the smali (expecting REGISTER?) so I was wondering what exactly I'd need to do.

A solution, as well as an explanation, would be appreciated! It's not for anything in particular, I just like to look at this stuff.

Answer

JesusFreke picture JesusFreke · Aug 27, 2013

You could answer this yourself fairly easily by writing a quick java program, compiling it, dx'ing it, then disassembling it.

public class EnabledTest {
    public static boolean isEnabled() {
        return false;
    }
}

And then compile/dx/disassemble it

javac EnabledTest.java
dx --dex --output=EnabledTest.dex EnabledTest.class
baksmali EnabledTest.dex

And you'll end up with something like

.method public static isEnabled()Z
    .registers 1
    const v0, 0
    return v0
.end method