📖 Tutorial How to write debug logs in Android APK (games and apps) using smali

Sbenny.com is trusted by 1,313,250 happy users since 2014.
Register

Sbenny

A crazy scientist
Staff member
Admin
SB Mod Squad ⭐
✔ Approved Releaser
Active User
Requirements:
- A good knowledge of smali coding;
- CatLog (or any apps to read logs)
- Apktool (or anything you prefer to decompile your app)
- Notepad++ (or whatever you use to edit smali files)


It might happen sooner or later, for a need or another, that you'd want to write something to see in your logcat (I personally use CatLog for it), such as checking why/when a game/app crashes or is detected by some protections or things like that.

Well, I soon discovered a few lines of smali code which helps achieving this:

Code:
    sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;

    const-string v1, "This is the debug line I want to write"

    invoke-virtual {v0, v1}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V
It should be self explanatory, if you have some smali knowledge. Basically it logs your desired custom string (in this case "This is the debug line I want to write" is what I'm writing in the logs).

Example of my custom debug info written in the game. In this case, my string was "onCreate1 called", so I searched it in my CatLog app and it was actually logged as you can see:

Screensh11ot_1.jpg



Q: How to read that log info when I run the app?
A: You just need CatLog or any other logging apps available on Google Play. Then, just open it and search for the debug string you wrote. Assuming you're trying to see what force-closes a mod apk (or a hack you created), in order to find and bypass the protection, if you find that string in the debug logs, it means it's being called before the function you're checking, right where you pasted it. If you instead can't find the debug string in your logcats, it means that code is not being called, and the app force-closes because of another function, called BEFORE the position you pasted your debug string in.

Q: Where to place it?
A: Well, if you're asking such question, you probably don't need this tutorial or you don't know what you're reading here, but anyways, for newbies purposes, I'll still answer this question. Basically you aim to write the debug info in a specific position in your decoded smali, right where you think the app is crashing or force-closing or anything else you want to find out about your app. A basic approach would be pasting it in what's being called by your OnCreate.

Q: I added that code and the app now crashes because of it. Why?
A: In my example, I used v0 and v1, if you're pasting that AFTER some code which already used v0 and/or v1, it might happen that the app crashes because you pass a different variable, so the solution is to change number, for example you could edit .locals at the top of the method to .locals 15 or even .locals 20 and then use v19 and v20, to be safer. If that still doesn't work, you probably just need to paste those three lines elsewhere.



I might probably add some more details from time to time, whenever I get a chance, but for the moment it should be clear enough. If not, feel free to ask below :)



This tutorial is written by Sbenny, only for sbenny.com.

Sbenny.com® - All Rights Reserved
 

Gourov

Dimitri Petrenko
✌️ Community Team
Member for 5 years
Thanks a lot Brother @Sbenny 😍 Though I don't use logs and have a very limited knowledge about it and I am very bad when it comes to smali or bypassing protection 😅😂 So I think I need more knowledge first. But it still very useful to find out which codes are getting called on certain functions or things like that 🤩

Thanks a lot again for your hard work and awesome tutorial Bro @Sbenny 💙
 

xXxmanoxXx

Apprentice Lv2️⃣
Member for 4 years
yes bro I get it now you mean with this simple code you can find out when/where the crash happens cuz if you just clicked on the game and it crashed immediately + it gives you an error like unfortunately closed,and then you open the logs and search for this string if you didn`t find it then that means that the crash isn`t in the Oncreate which means that the crash happens before Oncreate and so you can start investigating from this point and after launching the game if the game crashed at least after 2 seconds that means the crash is in Oncreate,and so you open the logs and search for this string to just ensure that the crash is in Oncreate,and that what you mean by (when/where a game/app crashes or is detected by some protections or things like that )brilliant idea,but i still don`t get it what do you mean by (why a game/app crashes or is detected by some protections or things like that) by the way, this code has also a small advantage,which is you can now reach to the crash faster in logs instead of reading and searching for the crash in the all log Good job bro
 
Last edited:

Daniel

Hunter of Sbennytopia
From the Hell
Verified 18+ user
The Cleaner 🧹
Member for 3 years
Sbenny maybe you should make a Smali guide too someday 🤔
 
Top