Saturday 22 June 2013

Debugging Android code


While trying to get some feedback in our attempt to use Cydia and actually see it working, our first approach was to print stuff.
If you are new using Android and also Cydia, you will soon found out is not that easy to print what's going on whenever your hooked classes or methods do something. That's because you don't actually want to debug your application but the system. Yes, you read it right. For most of us who are used to develop some code from time to time, you should be familiar with printing text in a console as a way to easily debug your code (to get some feedback from your application whenever it's running). But with Cydia hooks, its quite different, just because you want to print some text when the class or method you hooked is running, is it right?

our first decision was to print text in the Eclipse IDE console but, as I've already told you, this was worthless. 
If you use java code and place it inside your hooked method code, let's say... something like this: 

System.out.println("some words I would like to see as feedback");

You won't see it! Because this method will be called most of the time when your application is already closed (don't forget you have to restart your system from Cydia Substrate to see your changes applied).

Once you get it, let's talk about LogCat.
The first step is to notice there is another console called LogCat among your Eclipse's windows; if you don't see it, go to: 
Window -> Show View -> Other... -> Android -> LogCat. This little boy (or girl) prints lots of stuff but will also help you debugging. 

To use it just put, wherever you want to print something: 

Log.i("TAG", "text you want to print")

The TAG here is pretty important to filter the infinite lines of text LogCat prints later.
The i stands for information. You have various options to log errors, warnings, etc, but it's enough with the i. I may say my favorite it wtf, because I like its expressiveness. 

Then open a cmd and type:

> adb shell
> logcat

And you'll see lots of text printing everywhere.

To filter all this information use:

> logcat -s "TAG" 

And you'll see only the logs that have the tag TAG. 

No, enjoy debugging!






No comments:

Post a Comment