15

I have installed a release build apk in the android device but if I connect that device to Android studio then I am able to see all Logs/debugPrint statements.

Is there any way to disable the all the logs ?

Cœur
  • 34,719
  • 24
  • 185
  • 251
Ajay Kumar
  • 12,514
  • 11
  • 49
  • 51

2 Answers2

12

I combined the accepted answer with the idea from here and used it main.dart to silence all debugPrint everywhere.

const bool isProduction = bool.fromEnvironment('dart.vm.product');
void main() {
  if (isProduction) {      
      // analyser does not like empty function body
      // debugPrint = (String message, {int wrapWidth}) {};
      // so i changed it to this:
      debugPrint = (String? message, {int? wrapWidth}) => null;

  } 
  runApp(
    MyApp()
  );
}
Sebastian
  • 856
  • 10
  • 19
6

You can assign a dummy function to the global debugPrint variable:

import 'package:flutter/material.dart';

main() {
  debugPrint = (String message, {int wrapWidth}) {};
}
Günter Zöchbauer
  • 558,509
  • 191
  • 1,911
  • 1,506
  • And how could I check the build variant so that I can do it only when I compile for production? I don't like this solution: https://stackoverflow.com/questions/47438564/how-do-i-build-different-versions-of-my-flutter-app-for-qa-dev-prod and I don't wanna use package like simple_preprocessor. – shadowsheep Mar 25 '18 at 17:04
  • I think the linked SO question us the way to go. You can also create a build script that modifies a source file before it calls `flutter build ...` – Günter Zöchbauer Mar 25 '18 at 17:09
  • Dunno, but thanks. I’ll look forward this custom build script way. If you already have some links they’ll be appreciated ;) – shadowsheep Mar 25 '18 at 17:16
  • You can use a simple shell script or a bash script. I have a project where I copy a different Dart file that contains some config values that are imported somewhere into place before building. This way I don't need to modify a files content – Günter Zöchbauer Mar 25 '18 at 17:20
  • I’ll do some further research about this topic. Maybe I’ll post a specific question. For now upvoted your answer! ;) – shadowsheep Mar 25 '18 at 17:29