Thursday, November 24, 2011

Taking a peek at Android source code (decompiling)

Are you curious about some Android apps behavior?
Do you want to learn how some things are done on Android?

Android apps are written in Java, so it's easy to assume that a .apk file is the same as a .jar file. And if you know a bit of the Java world, you know that a .jar file is nothing more than a renamed .zip file containing compile binaries (.java files).
In the case of .apk files you can find a single classes.dex file (if the app is optimized, you'll find an .odex file) for code and resources and manifest related files. These files are generally not directly accessible. For reading them you'll need some tools.

For some info about the difference between .dex and .odex files you can go here. I won't talk about odexing apps in this article.

Remember that when decompiling an app you won't get the exact original source code, but an approximate that may not even compile back.

Here are the tools for you to open an .apk file:
- dex2jar;
- JD-GUI (Java Decompiler);
- apktool;

You can obtain two different results depending on the tools you use. With dex2jar you can convert a classes.dex file to a regular .jar which can be decompiled by Java Decompiler.
With apktool you can obtain the AndroidManifest.xml, all resource files and the code in a kind of assembler language.

Both of the tools are easy to use:
dex2jar (windows) --> dex2jar.bat classes.dex
apktool (windows) --> apktool.bat d .apk

Now to the hard part. Go play!

Thursday, November 17, 2011

First Hack - The LED Strip

Finally finished my first hack.

I needed a light for my kitchen counter, so I ordered a LED Strip from eBay and hacked an AC adapter to include an ON / OFF button and to connect it to mains without using a power socket.

So here's the theoretical work:

To cover the surface of my counter I used 48 segments of the LED strip (about 2 meters). Each segment has 3 white SMD leds (type 3528) and a 150 Ohm resistor. For it to work it needs a 12V power source with sufficient current output.
After learning from [ladyada] about these strips I figured that the theoretical current each segment needs to function at it's maximum is 16 mA. That is:
12V total - (3 leds * 3.2V) / 150R = 0.016A

For the total 48 segments that's 768 mA, resulting in under 10W lighting!!!
For that I bought a SMPS AC adapter in order to guarantee the regulated 12V while using that much current. An average (unregulated) AC adapter wouldn't provide nor the 12V neither near the maximum 1A stated. [ladyada] also shed me some lights on that matter.

White LED StripSuitable AC AdapterThe best switch button I could find

For the practical work:

As you will see for yourself, the tools weren't exactly the best suited for the job: a kitchen knife to cut, a soldering iron to remove plastic, a hammer and a nail to open holes...

The innards of a power outletMains power derivationAdhesive cable duct

In order to cut the space for the switch button in the AC adapter, my kitchen knife wasn't enough, so I used my soldering iron to heat a metal needle and a kind of a thick clip used by stores to keep shirts folded in it's package. Even so it was a hard work.

Pierced top cover from the AC AdapterCreating the space for the switch buttonThe working conditions

Then I just needed to solder the switch button and the wire to the board and cover it all with electrical tape.

The final cutConnecting the switch buttonThat's a wrap

And there we have it!
Our working LED Strip!