16

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
  • 37,241
  • 25
  • 195
  • 267
Ajay Kumar
  • 15,250
  • 14
  • 54
  • 53

3 Answers3

14

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
  • 966
  • 11
  • 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
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 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
0

With latest Flutter versions returning empty function does not work. Returning null is not recommended and should be avoided - if used, it is marked with tag that void functions should not have null return. Instead empty string will not cause any issue and works perfectly:

main() {
  debugPrint = (String? message, {int? wrapWidth}) => '';
}
Elmar
  • 2,235
  • 24
  • 22