0

I am having some code in my Android application that I use for debugging but don't want to have in production. Is there a way in Android, Java to exclude some code in production version? Like in C with define, ifdef, endif?

I was into assertion, which can be enabled in run configuration of the Android application, but that assert statement requires the assert code to return a boolean value so that doesn't work for me.

Thanks!

Robert
  • 4,602
  • 4
  • 22
  • 33
  • 1
    Do you want the debugging code to be omitted from the compiled bytecode or do you just don't want the debugging code to ever run? – condit Aug 01 '13 at 17:43
  • It's good enough for me if code doesn't run, but the best would be if it was omitted from the compiled byte code. – Robert Aug 01 '13 at 17:44
  • @Geobits That solution you link to is nice, thanks for that! – Robert Aug 01 '13 at 17:47

3 Answers3

0

I'm posting this answer for informational purposes and this is not a recommendation actually use assertion side effects to alter code.

That being said...

    boolean debug = false;
    assert debug = true;

    if(debug){
        /* Do debugging stuff */
    }
Dev
  • 11,919
  • 3
  • 40
  • 53
0

There is no native conditional compilation in Java. However, this article details a way to do it using ANT and comments such as //[ifdef] and //[enddef]

https://weblogs.java.net/blog/schaefa/archive/2005/01/how_to_do_condi.html

Good luck!

  • 1
    Interesting, I was quite sure that there would be some native conditional compilation in Java, but apparently not. I will look through your link but I think for my needs the link @Geobits posted holds a solution that works good enough for me – Robert Aug 01 '13 at 17:51
0

There is also this solution, which adds preprocessor directives to Java:

https://www.java.net//forum/topic/java-tools/java-development-tools/wwyt-conditional-compilation-pre-processor