Mastering Debug Mode in Android. When working on large projects, we… | by Andres Rubiano Del Chiaro | Feb, 2025

1. Display Logs Only in Debug Mode

To prevent debug messages from appearing in production:

if (BuildConfig.DEBUG) {
Log.d("DEBUG", "Message visible only in debug mode")
}

2. Enable Debugging Tools

You can enable tools like Stetho or LeakCanary only in debug mode:

if (BuildConfig.DEBUG) {
Stetho.initializeWithDefaults(this)
}

3. Use Test Servers

Avoid sending test data to production by selecting the appropriate server:

val baseUrl = if (BuildConfig.DEBUG) "https://api-staging.example.com" else "https://api.example.com"

4. Enable Hidden Developer Options

if (BuildConfig.DEBUG) {
showDebugOptions()
}

5. Prevent Dangerous Code from Running in Production

For example, preventing database deletion in production:

if (BuildConfig.DEBUG) {
deleteDatabase()
}

6. Apply Additional Security Measures in Production

if (!BuildConfig.DEBUG) {
enforceStrictSecurityPolicies()
}

7. Disable Analytics or Ads in Debug Mode

To prevent Firebase Analytics or AdMob from collecting test data:

if (!BuildConfig.DEBUG) {
FirebaseAnalytics.getInstance(this).setAnalyticsCollectionEnabled(true)
} else {
FirebaseAnalytics.getInstance(this).setAnalyticsCollectionEnabled(false)
}

🚀 Conclusion

Detecting whether an application is in debug mode helps optimize development, enhance security, and prevent production errors.

Use BuildConfig.DEBUG for compile-time optimizations and efficient checks.
Use isDebuggable() if you need a runtime check, especially to detect tampered APKs.

If you haven’t implemented this check in your app yet, now is the time! 🚀

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.