Hacking

Tuesday, July 27, 2021

Android - Removing log statements before releasing

If your application is making requests to a server, you might be using some type of log to check whether or not your requests are successful. Unfortunately, those logs don’t get removed when you build the final APK (Android application package file). Removing logs is important to keep the logcat output as clean as possible. Leaving log statements in could also expose you to unintentional disclosure of sensitive information. In this hack, I’ll show you how easy it is to remove logs for your market release. Developers have their own technique preferences for removing logs from the final release. Some prefer doing something like the following:

if(BuildConfig.DEBUG)LOG.d(TAG,"Thelogmsg");

From my point of view, the best way to remove logs is to use the ProGuard tool. If you’ve never used ProGuard.

The ProGuard tool shrinks, optimizes, and obfuscates your code by removing unused code and renaming classes, fields, and methods with semantically obscure names. The result is a smaller sized .apk file that is more difficult to reverse engineer.

If you haven’t noticed yet, when we build an Android application we’ll find a pro-guard.cfg file in our project root directory. Its presence there doesn’t mean it’s on by default; we need to enable it. Fortunately, it’s simple: we need to add the following line in the default.properties file located in our project root directory:

proguard.config=proguard.cfg

Now ProGuard is enabled, but it’ll only be used when exporting a signed APK. We need to add the necessary lines to the proguard.cfg to get rid of those logs. Append the following lines to proguard.cfg:

-assumenosideeffectsclassandroid.util.Log{publicstatic***d(...);}

What we’re telling ProGuard is this: remove every use of a d() method with any amount of parameters that returns something and belongs to the android.util.Logclass. This will match with Log’s d() method and every debug log will be removed.

The ProGuard tool offers another way of polishing a release. Make sure you read the ProGuard manual and create a correct configuration for your project because Pro-Guard might remove essential code, thinking it’s not necessary for the application to work. If this happens, be sure to check that you’re telling ProGuard to keep every-thing you need. Notice that ProGuard isn’t only used to remove log statements. As I’m testing, I usually create methods in my Activity to populate forms. These methods are also something I use ProGuard to remove.

Refference :-

http://proguard.sourceforge.net/

http://developer.android.com/tools/help/proguard.html

http://mng.bz/ZR3

No comments:

List Of ADB Command For Android Developer

  Adb Server adb kill-server adb start-server  Adb Reboot adb reboot adb reboot recovery  adb reboot-bootloader adb root //restarts adb with...