Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I find a far easier solution is to forget all the <code>if</code> checks all over the place and just use <a href="http://proguard.sourceforge.net/" rel="noreferrer">ProGuard</a> to strip out any <code>Log.d()</code> or <code>Log.v()</code> method calls when we call our Ant <code>release</code> target.</p> <p>That way, we always have the debug info being output for regular builds and don't have to make any code changes for release builds. ProGuard can also do multiple passes over the bytecode to remove other undesired statements, empty blocks and can automatically inline short methods where appropriate.</p> <p>For example, here's a very basic ProGuard config for Android:</p> <pre><code>-dontskipnonpubliclibraryclasses -dontobfuscate -forceprocessing -optimizationpasses 5 -keep class * extends android.app.Activity -assumenosideeffects class android.util.Log { public static *** d(...); public static *** v(...); } </code></pre> <p>So you would save that to a file, then call ProGuard from Ant, passing in your just-compiled JAR and the Android platform JAR you're using.</p> <p>See also <a href="http://proguard.sourceforge.net/manual/examples.html#androidapplication" rel="noreferrer">the examples</a> in the ProGuard manual.</p> <hr> <p><strong>Update (4.5 years later):</strong> Nowadays I used <a href="https://github.com/JakeWharton/timber" rel="noreferrer">Timber</a> for Android logging.</p> <p>Not only is it a bit nicer than the default <code>Log</code> implementation — the log tag is set automatically, and it's easy to log formatted strings and exceptions — but you can also specify different logging behaviours at runtime.</p> <p>In this example, logging statements will only be written to logcat in debug builds of my app:</p> <p>Timber is set up in my <code>Application</code> <code>onCreate()</code> method:</p> <pre><code>if (BuildConfig.DEBUG) { Timber.plant(new Timber.DebugTree()); } </code></pre> <p>Then anywhere else in my code I can log easily:</p> <pre><code>Timber.d("Downloading URL: %s", url); try { // ... } catch (IOException ioe) { Timber.e(ioe, "Bad things happened!"); } </code></pre> <p>See the <a href="https://github.com/JakeWharton/timber/blob/dd94a22/timber-sample/src/main/java/com/example/timber/ExampleApp.java" rel="noreferrer">Timber sample app</a> for a more advanced example, where all log statements are sent to logcat during development and, in production, no debug statements are logged, but errors are silently reported to Crashlytics.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload