I was hoping for a way to comment out code on release versions.
What led me to Ant was that Proguard's way of removing Log.D (debug) calls was unsatisfactory as it was leaving the string literals in the dex files, even though the Log.D code was being removed by it's optimisation technique. As pointed out on this thread Removing unused strings during ProGuard optimisation
There someone one has suggested Ant could be used with a replace algorithm but this wipes out the code when it is run. I was hoping if there was a way of commenting out the code so it became //ant Log.d, then once it compiles the //ant could be removed.
I am new to ant, and I wasn't able to find any search results for commenting out code in Ant. Is it not a recommended practice? I feel copying all the files to another directory and then removing the lines and then copying it back is overkill. If the compile fails you are left with your code in another directory.
So at the moment I am using the below regex pattern to comment out the code.
<regexp pattern="(\s*)Log\.d\s*(\(.*\))\s*;"/>
<substitution expression="\1\/\/AntComment Log\.d\2;"/>
I was wondering if there is a better way i.e. a built in way of handling comments.
Also is there a way trial run ant regex statements to see what it picks up?
Listening to Jean Waghetti i tried a few bits of code with conditional compilation
I just tried a few variations, it seems it needs you to have if(DEBUG) in the same function. So this piece of code will end up having the string literal in the classes.dex file.
Logger.myLog("Sensitive Info" + c); // you call this
//in Logger class - myLog method
static void myLog(String msg){
if(DEBUG){
If you try to have a bit more sophisticated logging, where it needs figure out whether to log but with an and (&&) with DEBUG it ends up adding the string literal in the dex file eg:
public class SomeClass{
public final boolean log = DEBUG && figure_Log();
private boolean figure_Log(){
// some condition based on other settings
}
//in the code
if (log){
To achieve a more sophisticated logging you have to have this:
if(DEBUG && log){
before all logging calls, it creates too many unused code warnings and looks ugly for me.